简体   繁体   English

如何用字符串作为 x 轴值绘制两个图

[英]How to plot two plots with strings as x axis values

I want to plot two figures in one image using matplotlib.我想使用 matplotlib 在一张图像中绘制两个数字。 Data which I want to plot is:我要绘制的数据是:

x1 = ['sale','pseudo','test_mode']
y1 = [2374064, 515, 13]

x2 = ['ready','void']
y2 = [2373078, 1514]

I want to plot the bar plot for both the figure in one image.我想为一个图像中的两个图形绘制条形图。 I used the code given below:我使用了下面给出的代码:

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x1, y1)
ax1.set_title('Two plots')
ax2.plot(x2, y2)

but its giving error:但它给出的错误:

ValueError: could not convert string to float: PSEUDO

How I can plot them in one image using matplotlib?如何使用 matplotlib 在一张图像中绘制它们?

Try this:尝试这个:

x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]

x2 = ['ready','void']
y2 = [78, 1514]

y = y1+y2
x = x1+x2
pos = np.arange(len(y))
plt.bar(pos,y)
ticks = plt.xticks(pos, x)

Separate figures in one image:在一张图像中分离数字:

x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]

x2 = ['ready','void']
y2 = [78, 1514]

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)

pos1 = np.arange(len(y1))
ax1.bar(pos1,y1)
plt.sca(ax1)
plt.xticks(pos1,x1)

pos2 = np.arange(len(y2))
ax2.bar(pos,y2)
plt.sca(ax2)
plt.xticks(pos2,x2)

The problem is that your x values are not numbers, but text.问题是您的x值不是数字,而是文本。 Instead, plot the y values and then change the name of the xticks (see this answer ):相反,绘制y值,然后更改 xticks 的名称(请参阅此答案):

import matplotlib.pyplot as plt

x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]

x2 = ['ready','void']
y2 = [78, 1514]

f, axes = plt.subplots(1, 2, sharey=True)
for (x, y, ax) in zip((x1, x2), (y1, y2), axes):
    ax.plot(y)
    ax.set_xticks(range(len(x))) # make sure there is only 1 tick per value
    ax.set_xticklabels(x)
plt.show()

This produces:这产生:

线形图

For a bar graph, switch out ax.plot(y) with ax.bar(range(len(x)), y) .对于条形图,用ax.bar(range(len(x)), y)切换出ax.plot(y) ax.bar(range(len(x)), y) This will produce the following:这将产生以下内容:

条状图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中使用不同的 X 轴 plot 两个值? - How to plot two values in python with different X axis? 如何在散点 plot 中对轴上的字符串值进行排序 - How to sort values with strings on the axis in scatter plot Matplotlib:如何 plot 两个条形图具有相同的 x/y 轴,但一个沿 y 轴从另一个开始 - Matplotlib : How to plot two bar plots with the same x/y axes but one start over the other along y axis 如何在 x 轴上绘制所有值 - How to plot all values on x axis 如何为直方图选择 x 和 y 轴的值? - How are the values for x and y axis chosen for histogram plots? 如何在其中一个图上具有一个共享的X轴和多个Y轴的堆叠图? - How can I have a stacked plot with a shared X axis and multiple Y axis on one of the plots? 如何绘制x轴的熊猫数据框列,而x轴由另外两个提供起始值和终止值的列定义? - How to plot pandas dataframe column with x-axis defined by two other columns giving start and end values? 如何使用 matplotlib 使用多行 plot 修复 x 轴显示两个值 - How to fix x-axis showing two values with multi-line plot using matplotlib 如何在python中用长字符串绘制X轴? - how to plot X-axis with long Strings in python? 如何通过在 matplotlib 中的 x 轴上重复字符串来制作 plot? - how to make a plot by repeating strings on x-axis in matplotlib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM