Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot string values in matplotlib

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?

like image 666
user299582 Avatar asked Mar 23 '10 03:03

user299582


People also ask

Can matplotlib plot strings?

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.

How do I show text in matplotlib?

The matplotlib. pyplot. text() function is used to add text inside the plot. The syntax adds text at an arbitrary location of the axes.

How do you show a value in a plot in Python?

Just call the plot() function and provide your x and y values. Calling the show() function outputs the plot visually.


2 Answers

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()
like image 51
user664225 Avatar answered Oct 11 '22 21:10

user664225


Why not just make the x value some auto-incrementing number and then change the label?

--jed

like image 35
Jed Daniels Avatar answered Oct 11 '22 21:10

Jed Daniels