在matplotlib中绘制字符串值 [英] Plot string values in matplotlib
本文介绍了在matplotlib中绘制字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将matplotlib用于绘图应用程序.我正在尝试创建一个具有字符串作为X值的图形.但是,使用plot
函数需要X的数字值.
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.
如何使用字符串X的值?
How can I use string X values?
推荐答案
从matplotlib 2.1开始,您可以在绘图函数中使用字符串.
From matplotlib 2.1 on you can use strings in plotting functions.
import matplotlib.pyplot as plt
x = ["Apple", "Banana", "Cherry"]
y = [5,2,3]
plt.plot(x, y)
plt.show()
请注意,为了保留绘图上输入字符串的顺序,您需要使用matplotlib> = 2.2.
Note that in order to preserve the order of the input strings on the plot you need to use matplotlib >=2.2.
这篇关于在matplotlib中绘制字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文