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)
我在下面提供了一个示例(图例有点偏离,所以忽略它们) 我想要一个像顶行或第二行这样的输出。
谢谢
最佳答案
我相信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()
关于python - 如何缩放饼图和条形图以同时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56298327/