简体   繁体   English

如何使用matplotlib(没有大熊猫)并排绘制两个图

[英]How to graph two plots side by side using matplotlib (no pandas)

I am totally out of my comfort zone, but I have been asked to graph some data side by side. 我完全不在我的舒适区,但我被要求并排绘制一些数据。 I managed to graph my data into charts, but I can't figure out how to get them to be graphed side by side. 我设法将我的数据绘制成图表,但我无法弄清楚如何将它们并排绘制。 I have read about using plt.subplot but I have not been able to use it successfully (It just gives me blank graphs). 我已经阅读了有关使用plt.subplot但我无法成功使用它(它只是给我空白图)。 Here is my code for the two graphs: 这是我的两个图的代码:


def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')


plt.rcParams['figure.figsize']=(7,6)
labels = monthNames

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, financeMonthlyDefects, width, label='Defects', color = 'r')
rects2 = ax.bar(x + width/2, financeMonthlyDeployments, width, label='Deployments',)

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_title('Finance Systems Deployments/Defects')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
ax.set_xlabel('Iteration Month & Year')
plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment

autolabel(rects1)
autolabel(rects2)

### BEGIN GLOBAL FINANCE ###
plt.rcParams['figure.figsize']=(7,6)
labels = monthNames

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, globalFinancingMonthlyDefects, width, label='Defects', color = 'r')
rects2 = ax.bar(x + width/2, globalFinancingMonthlyDeployments, width, label='Deployments',)

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_title('Global Finance Deployments/Defects')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
ax.set_xlabel('Iteration Month & Year')
plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment

autolabel(rects1)
autolabel(rects2)

This is how it is output now: 这就是它现在的输出方式: 垂直堆叠:(

Every time you call subplots() , a new figure is created, which is not what you want. 每次调用subplots() ,都会创建一个新图形,这不是您想要的。 To get the side-by-side plots, do fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2) and then use ax1 for plotting whatever you want on the left plot, ax2 for the right plot. 要获得并排图,请执行fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)然后使用ax1绘制左边图中所需的任何内容, ax2绘制右图。

When plotting the first figure, write this 在绘制第一个图时,写下这个

plt.subplot(1, 2, 1)
<code for first plot>

(1,2,1) indicates this plot is a part of a 1-row, 2-column figure and the 1st figure (1,2,1)表示该图是1行2列图和第1图的一部分

When plotting the second figure, write this 在绘制第二个数字时,写下这个

plt.subplot(1, 2, 2)
<code for second plot>

(1,2,2) indicates this plot is a part of a 1-row, 2-column figure and the 2nd figure (1,2,2)表示该图是1行2列图和第2图的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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