import numpy as np import matplotlib.pyplot as plt plt.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams["axes.unicode_minus"] = False month_x = np.arange(1, 13, 1) data_A= np.array([47,64,31,23,29,42,25,33,65,63,63,61]) data_B= np.array([28,66,183,207,83,176,165,174,377,288,168,203]) fig, ax = plt.subplots() bar_A = ax.bar(month_x, data_A, color='b', tick_label=['5日','6日','7日','8日','9日','10日','11日','12日','13日','14日','15日','16日'],width=bar_width) bar_B = ax.bar(month_x+bar_width, data_B, color='r',width=bar_width) ax.set_ylabel('在售数量(个)') ax_right = ax.twinx() ax_right.set_ylabel('求购数量(个)') ax.set_title('在售数量与求购数量对比图') plt.legend([bar_A, bar_B], ['在售数量', '求购数量'], shadow=True, fancybox=True) plt.xticks(month_x+bar_width/2) plt.ylim([0,400]) plt.grid(axis='y', linestyle='--') plt.show()修改为饼图或圆环图
时间: 2023-11-21 21:06:18 浏览: 300
import numpy as np import matplotlib.pyplot as plt import math
以下是将柱状图修改为圆环图的代码:
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
data_A = np.array([47,64,31,23,29,42,25,33,65,63,63,61])
data_B = np.array([28,66,183,207,83,176,165,174,377,288,168,203])
labels = ['5日','6日','7日','8日','9日','10日','11日','12日','13日','14日','15日','16日']
total = data_A + data_B
sizes_A = data_A / total
sizes_B = data_B / total
fig, ax = plt.subplots()
ax.pie(sizes_A, labels=labels, radius=1.2, colors=['b']*12,
startangle=90, counterclock=False, wedgeprops=dict(width=0.3, edgecolor='w'))
ax.pie(sizes_B, radius=0.9, colors=['r']*12,
startangle=90, counterclock=False, wedgeprops=dict(width=0.3, edgecolor='w'))
ax.set_title('在售数量与求购数量对比图')
plt.legend(['在售数量', '求购数量'], shadow=True, fancybox=True)
plt.show()
注意,饼图和圆环图的使用需要谨慎,因为它们可能不太适合展示大量数据或数据差异较小的情况。在这种情况下,柱状图可能更适合。
阅读全文