我有以下代码,并在jupyter上运行它。
# Make the 4 plots:
import matplotlib.pyplot as plt
# Dwell Time
ax = hour_17['Average Dwell Time'].plot(kind='bar', figsize=(15, 10),
legend=True, fontsize=12)
ax.set_xlabel("5-minutes interval between 17:00-18:00", fontsize=12)
ax.set_ylabel("Time (sec)", fontsize=12)
plt.savefig('name1.jpeg')
# Waiting Time
ax = hour_17['Average Waiting Time'].plot(kind='bar', figsize=(15, 10),
legend=True, fontsize=12)
ax.set_xlabel("5-minutes interval between 17:00-18:00", fontsize=12)
ax.set_ylabel("Time (sec)", fontsize=12)
plt.savefig('name2.jpeg')存在以下问题:第一个图显示指定的列和图例,而第二个图包含两个图例:平均等待时间和平均驻留时间,并显示与第一个图相同的信息。实际上,我必须从4列中绘制数据,所以最后一幅图包含了4个图例。
知道这是怎么回事吗?谢谢!
发布于 2018-10-27 07:09:01
您在代码中所做的是将这两幅图的绘图数据存储到ax中,因此产生了额外的图例。理想情况下,您希望使用plt.subplots()编码样式来防止这种情况发生。你有两个选择:
第三种选择是根本不使用无花果、ax样式,而是直接使用plt.plot方法。关于为什么这是个坏主意,有很多讨论。这个职位解释了方法上的差异。
如果您想要进行两个单独的绘图,请使用下面的单个绘图方法,只需使用两次。如果你想组合这些情节,你可以使用第二种方法。这来自matplotlib文档这里。
#Creates just a figure and only one subplot
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
#Creates two subplots and unpacks the output array immediately
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)https://stackoverflow.com/questions/53018687
复制相似问题