Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign equal scaling on the x-axis in Matplotlib?

What I currently have is this:

x = [3.0, 4.0, 5.0, 5.0, 6.0, 7.0, 9.0, 9.0, 9.0, 11.0]
y = [6.0, 5.0, 4.0, 2.5, 3.0, 2.0, 1.0, 2.0, 2.5, 2.5]

Which produces the following graph:

enter image description here

What I'd like is to have equal scaling on my axis. Therefore, instead of having such a large gap between 7 and 9 and also 9 and 11, it'd be a space equal like all others. It's look like this:

enter image description here

To eliminate the 8 and the 10 from the graph I used ticks. Here is the relevant code:

ax=fig.add_subplot(111, ylabel="speed")
ax.plot(x, y, 'bo')
ax.set_xticks(x) 

None of the examples on the matplotlib page have anything that I desire. I've been looking through the documentation, but everything 'scaling' related doesn't do what I want it to do.

Can this be done?

like image 636
SaiyanGirl Avatar asked Aug 20 '12 21:08

SaiyanGirl


1 Answers

Further to my comments to the OP, you can plot against the natural numbers 1 to n, where n is the number of unqiue abscissa values in your data set. Then you can set the x ticklabels to these unique values. The only trouble I had in implementing this is handling repeated abscissa values. To try and keep this general I came up with the following

from collections import Counter # Requires Python > 2.7

# Test abscissa values
x = [3.0, 4.0, 5.0, 5.0, 6.0, 7.0, 9.0, 9.0, 9.0, 11.0]

# Count of the number of occurances of each unique `x` value
xcount = Counter(x)

# Generate a list of unique x values in the range [0..len(set(x))]

nonRepetitive_x = list(set(x)) #making a set eliminates duplicates
nonRepetitive_x.sort()         #sets aren't ordered, so a sort must be made
x_normalised = [_ for i, xx in enumerate(set(nonRepetitive_x)) for _ in xcount[xx]*[i]]    

At this point we have that print x_normalised gives

[0, 1, 2, 2, 3, 4, 5, 5, 5, 6]

So plotting y against x_normalised with

from matplotlib.figure import Figure
fig=Figure()
ax=fig.add_subplot(111)

y = [6.0, 5.0, 4.0, 2.5, 3.0, 2.0, 1.0, 2.0, 2.5, 2.5]

ax.plot(x_normalised, y, 'bo')

Gives

Result of solution presented as plotted using matplotlib

Finally, we can change the x-axis tick labels to reflect the actual values of our original x-data using set_xticklabels using

ax.set_xticklabels(nonRepetitive_x)

Edit To get the final plot looking like the desired output in the OP one can use

x1,x2,y1,y2 = ax.axis()
x1 = min(x_normalised) - 1 
x2 = max(x_normalised) + 1
ax.axis((x1,x2,(y1-1),(y2+1))) 

#If the above is done, then before set_xticklabels, 
#one has to add a first and last value. eg:

nonRepetitive_x.insert(0,x[0]-1) #for the first tick on the left of the graph
nonRepetitive_x.append(x[-1]+1) #for the last tick on the right of the graph 
like image 173
Chris Avatar answered Oct 04 '22 17:10

Chris