Python 如何使用matplotlib并排绘制两个图(无熊猫)

Python 如何使用matplotlib并排绘制两个图(无熊猫),python,matplotlib,jupyter-notebook,data-science,Python,Matplotlib,Jupyter Notebook,Data Science,我完全超出了我的舒适区,但我被要求将一些数据并排绘制出来。我设法将数据绘制成图表,但我不知道如何将它们并排绘制。我读过关于使用plt.subplot的文章,但是我没有成功地使用它,它只给了我空白的图表。以下是我对这两个图的代码: def autolabel(rects): for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height),

我完全超出了我的舒适区,但我被要求将一些数据并排绘制出来。我设法将数据绘制成图表,但我不知道如何将它们并排绘制。我读过关于使用plt.subplot的文章,但是我没有成功地使用它,它只给了我空白的图表。以下是我对这两个图的代码:


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)


现在是这样输出的:

每次调用子图时,都会创建一个新的图形,这不是您想要的。要获得并排绘图,请执行fig、ax1、ax2=plt。子绘图行=1、ncols=2,然后使用ax1在左侧绘图上绘制所需内容,使用ax2在右侧绘图上绘制所需内容。

每次调用子绘图时,都会创建一个新的图形,而这不是您所需的图形。要获得并排绘图,请执行fig、ax1、ax2=plt。子绘图行数=1、ncols=2,然后使用ax1在左侧绘图上绘制所需内容,使用ax2在右侧绘图上绘制所需内容。

绘制第一幅图时,请编写以下内容

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

1,2,2表示该图是一行两列图形和第二个图形的一部分

绘制第一个图形时,请写下以下内容

plt.subplot(1, 2, 1)
<code for first plot>
plt.subplot(1, 2, 2)
<code for second plot>
1,2,2表示该图是一行两列图形和第二个图形的一部分