I am using matplotlib for a graphing application. I am trying to create a graph which has strings as the X values. However, the using plot
function expects a numeric value for X.
How can I use string X values?
From matplotlib 2.1 on you can use strings in plotting functions. Note that in order to preserve the order of the input strings on the plot you need to use matplotlib >=2.2. This was downvoted, but I upvoted it since it is by far the most compact solution and, in most cases, won't break anything else.
The matplotlib. pyplot. text() function is used to add text inside the plot. The syntax adds text at an arbitrary location of the axes.
Just call the plot() function and provide your x and y values. Calling the show() function outputs the plot visually.
You should try xticks
import pylab
names = ['anne','barbara','cathy']
counts = [3230,2002,5456]
pylab.figure(1)
x = range(3)
pylab.xticks(x, names)
pylab.plot(x,counts,"g")
pylab.show()
Why not just make the x value some auto-incrementing number and then change the label?
--jed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With