etnie1031
etnie1031

Reputation: 101

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). 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: Vertically stacked :(

Upvotes: 1

Views: 4180

Answers (2)

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

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

Upvotes: 1

afb
afb

Reputation: 253

Every time you call subplots(), a new figure is created, which is not what you want. 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.

Upvotes: 3

Related Questions