在 matplotlib 中绘制字符串值

发布于 2024-08-26 00:46:57 字数 115 浏览 3 评论 0原文

我正在使用 matplotlib 进行绘图应用程序。我正在尝试创建一个以字符串作为 X 值的图表。但是,using plot 函数需要 X 的数值。

如何使用字符串 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.

How can I use string X values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倒带 2024-09-02 00:46:57

从 matplotlib 2.1 开始,您可以在绘图函数中使用字符串。

import matplotlib.pyplot as plt
x = ["Apple", "Banana", "Cherry"]
y = [5,2,3]

plt.plot(x, y)

plt.show()

输入图像描述这里

请注意,为了保留绘图上输入字符串的顺序,您需要使用 matplotlib >=2.2。

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()

enter image description here

Note that in order to preserve the order of the input strings on the plot you need to use matplotlib >=2.2.

青瓷清茶倾城歌 2024-09-02 00:46:57

你应该尝试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()

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()
赴月观长安 2024-09-02 00:46:57

为什么不直接将 x 值设为某个自动递增的数字,然后更改标签呢?

--杰德

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

--jed

情释 2024-09-02 00:46:57

这是我知道的一种可行的方法,尽管我认为创建自定义符号是实现此目的的更自然的方法。

from matplotlib import pyplot as PLT

# make up some data for this example
t = range(8)
s = 7 * t + 5
# make up some data labels which we want to appear in place of the symbols
x = 8 * "dp".split()
y = map(str, range(8))
data_labels = [ i+j for i, j in zip(x, y)]
fig = PLT.figure()
ax1 = fig.add_subplot(111)
ax1.plot(t, s, "o", mfc="#FFFFFF")     # set the symbol color so they are invisible
for a, b, c in zip(t, s, data_labels) :
    ax1.text(a, b, c, color="green")

PLT.show()

因此,这将“dp1”,“dp2”,...代替每个原始数据符号 - 本质上创建自定义“文本符号”,尽管我再次相信有一种更直接的方法可以在 matplotlib 中做到这一点(不使用艺术家)。

Here's one way which i know works, though i would think creating custom symbols is a more natural way accomplish this.

from matplotlib import pyplot as PLT

# make up some data for this example
t = range(8)
s = 7 * t + 5
# make up some data labels which we want to appear in place of the symbols
x = 8 * "dp".split()
y = map(str, range(8))
data_labels = [ i+j for i, j in zip(x, y)]
fig = PLT.figure()
ax1 = fig.add_subplot(111)
ax1.plot(t, s, "o", mfc="#FFFFFF")     # set the symbol color so they are invisible
for a, b, c in zip(t, s, data_labels) :
    ax1.text(a, b, c, color="green")

PLT.show()

So this puts "dp1", "dp2",... in place of each of the original data symbols--in essence creating custom "text symbols" though again i have to believe there's a more direct way to do this in matplotlib (without using Artists).

他夏了夏天 2024-09-02 00:46:57

我找不到一个方便的方法来完成这个任务,所以我求助于这个小辅助函数。

import matplotlib.pyplot as p
def plot_classes(x, y, plotfun=p.scatter, **kwargs):
    from itertools import count
    import numpy as np
    classes = sorted(set(x))
    class_dict = dict(zip(classes, count()))
    class_map = lambda x: class_dict[x]
    plotfun(map(class_map, x), y, **kwargs)
    p.xticks(np.arange(len(classes)), classes)

然后,调用plot_classes(data [“class”],data [“y”],marker =“+”)应该按预期工作。

I couldn't find a convenient way to accomplish that, so I resorted to this little helper function.

import matplotlib.pyplot as p
def plot_classes(x, y, plotfun=p.scatter, **kwargs):
    from itertools import count
    import numpy as np
    classes = sorted(set(x))
    class_dict = dict(zip(classes, count()))
    class_map = lambda x: class_dict[x]
    plotfun(map(class_map, x), y, **kwargs)
    p.xticks(np.arange(len(classes)), classes)

Then, calling plot_classes(data["class"], data["y"], marker="+") should work as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文