python - 如何缩放饼图和条形图以同时显示

标签 python matplotlib

import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [2,3,4,5,6]
z = [10, 9, 8, 7, 6]
a = [10, 12, 14, 3]
b = [1, 2, 3,4]
c = [1,2,3,4,5]


scatter = plt.scatter(x,y,marker="o", label="Label 1",color="r")
line = plt.plot(x,z,label="Label 2",color="g")
bar = plt.bar(a,b)
pie = plt.pie(c,labels=["Tom", "Dick", "Harry", "And", "Nervous"])
plt.title("Scatter and line")
plt.xlabel("X-label")
plt.ylabel("Y-label")
plt.legend()
plt.show()

上面的代码打印了我想要的所有内容,但是饼图与图形数据不对齐。有没有办法让 MatPlotLib 将它们作为两个单独的并排图表打开,或者让它们将饼图覆盖在条形图上的指定位置(例如:x=15,y=15)

我在下面提供了一个示例(图例有点偏离,所以忽略它们) 我想要一个像顶行或第二行这样的输出。

谢谢

enter image description here

最佳答案

我相信matplotlib.pyplot.subplot可以并排绘制它们。

我稍微重新安排了你的代码以适应这两个子图:

# create first subplot on the left (1 row, 2 columns, position 1)
plt.subplot(121)
pie = plt.pie(c,labels=["Tom", "Dick", "Harry", "And", "Nervous"])

# create second subplot on the right (1 row, 2 columns, position 2)
plt.subplot(122)
scatter = plt.scatter(x,y,marker="o", label="Label 1",color="r")
line = plt.plot(x,z,label="Label 2",color="g")
bar = plt.bar(a,b)
plt.title("Scatter and line")
plt.xlabel("X-label")
plt.ylabel("Y-label")

plt.legend()
plt.show()

结果: result

关于python - 如何缩放饼图和条形图以同时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56298327/

相关文章:

python - 如何在 .py 文件中使用 Matplotlib 在绘图的图例中编写 Latex 公式?

python - Matplotlib 通过颜色图绘制带有颜色的线条

python - pip:找不到激活的 virtualenv(必需)

python - 为什么我的一些文件被复制了?

python - 导入错误 : No module named 'pymongo'

python - pandas numpy matplotlib 溢出错误 : date value out of range

python - 如何修改pandas中某些列的全文

python - 适用于 Netscaler 的 Citrix Nitro API - 启用和禁用服务组中的服务器节点

python - Matplotlib:散点图中的垂直线

python - Matplotlib 不在 X 轴上绘制水平线