简体   繁体   中英

How to scale a pie-chart and bar-chart to both display

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

The above code prints everything that I want however the pie chart does not align with the graphed data. Is there any way to have MatPlotLib either open them as two separate side-by-side charts or have them overlay the pie chart over the bar chart at a specified position (eg: x=15,y=15)

I've included a sample below (the legends are a bit off, so ignore them) I want an output either like the top row OR the second row.

Thanks

在此处输入图片说明

I believe that matplotlib.pyplot.subplot can plot them side by side.

I rearranged your code a little bit to fit these two subplots:

# 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: 结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM