# Remove spaces within scatter graph

Hi all

I’m trying to plot a graph using matplotlib.pyplot. I have two list, one containing my x values and the other my y values. When I plot them normally, both axes are not in ascending order. I have tried all ways to make the two axes in ascending order. If I convert both list to float values, then the axes are in ascending order but the white spaces with plots (coordinates without plot) is too much and the graph is consuming too many white space. Please let me know how to resolve this. I want only the plot with the respective values on both axes in ascending order.

I don’t understand how the graphs correspond to the problem description. Are they both failed results? Is one of them a wrong result and the other a desired result? Something else? It looks to me like the axes are numerically labelled in order (but it’s hard to read the numbers at this resolution).

If you mean that sorting the values in each list separately gives you the wrong result, then yes - that breaks the correspondence between the points. You need to sort the x-values, and rearrange the y-values in the same way that the x-values were rearranged. See here:

But if it’s a scatter graph, then there should be no reason to care about the order of the points. Normally this matters when someone is trying to draw a line graph, because the lines go back and forth instead of connecting the data properly.

Hi thank you for your response.

They are both wrong. I’m not able to get the desired result. Yes I’m trying to get a scatter graph with both axes in ascending order. Sorting one list does not solve the problem as the other corresponding values in the other list will be disordered by default. Even if I try to put it in a dataframe with the idea that they will be treated as coordinates and the axes will be sorted in a scatter plot, this is not the case.

I have only been able to do so by converting the values as float but then the graph obtained gives the whole range from smallest to largest (as shown in the first graph) but here the blank spaces are enormous and consuming too much space. Please let me know how to remove the unused coordinates/ blank spaces within the graph. Thank you.

If you want your axis to comprise an explicitly defined set of values in an arbitrary order set by you, then you are (need to be) using categorical coordinates of some sort, not numeric values (even though the categorical factors may “look like” numbers). I’m not a matplotlib user, so I can’t offer any further details specific to matplotlib usage.

I did not say to do that. Please carefully read the italicized portion of the sentence, and check the link I gave you.

There’s no clear way to explain how to do this, because what you want doesn’t make sense. The blank space is large because your actual data values are far apart, relative to the distance within each “group”. In other words, it accurately represents the data you actually have.

Imagine for example if you only had two (distinct) data points; would you be surprised if you tried to plot the scatter graph, and saw that they were on opposite sides of the plot? If we scale the axes so that the whole range of the axis is used for the two points, then of course they’ll be at opposite ends; if they were nearer the middle, we wouldn’t be using the whole range, and we’d “waste space” outside.

It might help if you could show some exact input data (it looks like you are plotting only nine points in the example, so this shouldn’t be difficult) and draw what you want the result to look like.

Thank you for your response. I managed to find a way to do it using R (R Graphics - Scatter Plot) but I need to learn how to use R now or I will see if R can be used in python. I tried every possible way using matplotlib and it did not work.

Again, we can only possibly help you by actually understanding what you want the result to look like.

No issues. Thank you so much for trying to help.