Python Matplotlib: Drawing 2d line without sorting

2019-02-28 03:41发布

问题:

How can I tell matplotlib to not sort xdata given as input when drawing a matplotlib.lines.Line2D ?

I want to create custom xticks which I create as strings, for example:

import matplotlib.pyplot as plt
x = ['1','2','10']
y = [ 1 , 2 ,  3 ]
fig,ax = plt.subplots()
ax.plot(x,y)
plt.show(block=False)

and I get x values drawn in the order ['1','10','2'], i.e. they get lexicographically sorted. How can I get them drawn in the original order?

Thank you for your help!

回答1:

You need to upgrade your matplotlib to version 2.2.0 or higher. (Current version is 2.2.2)

This will solve your problem automatically because it respects the order of the input.