Adding charts to a matplotlib subgrid - python

I have the following Matplotlib figure, with 2 charts:
That i created with the following code:
fig = plt.figure(facecolor='#131722',dpi=155, figsize=(8, 4))
ax1 = plt.subplot2grid((1,2), (0,0), facecolor='#131722')
ax2 = plt.subplot2grid((1,2), (0,1), facecolor='#131722')
Now i would like to add two charts, so ax3 and ax4, each needs to be below the two charts, they should have the same width of the two charts but half the height of the two bigger charts. How can i do that? I tried various solutions from here here but i'm struggling to get the expected output

You can achieve this using the gridspec_kw argument of plt.subplots. This allows you to specify the dimensions of the grid (in this case 2x2) and the ratio of the heights:
f, ax = plt.subplots(2, 2 , gridspec_kw={"height_ratios": [2, 1]})
for cAx in ax.flatten():
cAx.set_facecolor('#131722')
f.savefig("test.png", facecolor='#131722')
Alternatively, you can also create a 3x2 grid and specify that the first two subplots need to span two rows:
fig = plt.figure(facecolor='#131722',dpi=155, figsize=(8, 6))
ax1 = plt.subplot2grid((3,2), (0,0), facecolor='#131722', rowspan=2)
ax2 = plt.subplot2grid((3,2), (0,1), facecolor='#131722', rowspan=2)
ax3 = plt.subplot2grid((3,2), (2,0), facecolor='#131722')
ax4 = plt.subplot2grid((3,2), (2,1), facecolor='#131722')

Related

Python Subplot2Grid - controlling axis labels

I am using the Subplot2Grid functionality within Matplotlib to combine two figures with different orientations, 4 bar plots (full width) and then 3 scatter plots splitting the full width into 3 columns, plus an extra space for a legend. The different sections have axes that need to align so I have used sharex = ax1 and sharey = ax1 within Subplot2Grid to implement this successfully.
However, I now cannot seem to control the axis labels the same as I would just using regular subplots function, having the x-axis tick labels showing only on the final bar plot and the y-axis tick labels showing only on the left-most scatter plot.
Plotting using Subplot2Grid, extra axis labels showing
I have tried the ax.set_xticklabels('') to try and switch them off, but the sharex/sharey seems to override them? I have also put the ax.set_xticklabels('') at the end of the code (after they are defined in ax4) and it switches them all off, not just the axis the one that is called (ax1, ax2 or ax3)
Relevant parts of the code are below:
# figure setup
fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(9)
ax1 = plt.subplot2grid(shape=(6, 3), loc=(0, 0), colspan=3)
ax2 = plt.subplot2grid(shape=(6, 3), loc=(1, 0), colspan=3,sharex=ax1)
ax3 = plt.subplot2grid(shape=(6, 3), loc=(2, 0), colspan=3,sharex=ax1)
ax4 = plt.subplot2grid(shape=(6, 3), loc=(3, 0), colspan=3,sharex=ax1)
ax5 = plt.subplot2grid(shape=(6, 3), loc=(5, 0))
ax6 = plt.subplot2grid(shape=(6, 3), loc=(5, 1),sharex=ax5,sharey=ax5)
ax7 = plt.subplot2grid(shape=(6, 3), loc=(5, 2),sharex=ax5,sharey=ax5)
# plotting bars here
# first bar plot
ax1.set_title('Inundation area')
ax1.set_xticklabels('')
ylbl0 = 'Inundation area \n' + r'$(km^2)$'
ax1.set_ylabel(ylbl0)
# repeat for ax2 & ax3
# last bar plot
ax4.set_title(r'$\Delta$ Shear Stress')
ax4.set_xticks(np.arange(len(df_bars)))
ax4.set_xticklabels(df_bars['Reach Number'])
ax4.invert_xaxis()
ax4.axhline(y=0,c='k',lw = 0.5)
ax4.set_xlabel('Reach number')
ax4.set_ylabel('% change \n (2019-2020)')
Same occurs when using sharey for the 3 scatter plots and ax.set_yticklabels('')
ax1.tick_params(labelbottom=False)
does what you want.
This example works for me:
fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(9)
ax1 = plt.subplot2grid(shape=(2, 1), loc=(0, 0), colspan=3)
ax1.plot(np.random.rand(10))
ax2 = plt.subplot2grid(shape=(2, 1), loc=(1, 0), colspan=3,sharex=ax1)
ax2.plot(np.random.rand(10))
ax1.tick_params(labelbottom=False)
plt.show()

How to create three subplots where the height of the upper plot is lower?

I would like to create a plot that consists of three subplots, where the upper left plot has the same width as the lower left plot but 1/3 of the height. Besides, I'd also like to plot the legend in the upper right area from the lower left plot. Is this even possible?
fig, ax = plt.subplots(2, figsize = (16,9))
ax1 = plt.subplot2grid((2,3), (1,0), colspan=2)
ax2 = plt.subplot2grid((2,3), (1,2), colspan=1)
ax3 = plt.subplot2grid((2,3), (0,0), colspan=2)
fig.suptitle('Title')
fig.tight_layout()
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np
x = np.linspace(0, 2*np.pi)
y1 = np.cos(x)
y2 = np.sin(x)
fig = plt.figure()
gs = GridSpec(2, 2, width_ratios=[2, 1], height_ratios=[1, 3])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
ax3 = fig.add_subplot(gs[2])
ax4 = fig.add_subplot(gs[3])
ax3.plot(x, y1, label="cos")
ax3.plot(x, y2, label="sin")
handles, labels = ax3.get_legend_handles_labels()
# hide axis on the top left subplot
ax2.axis("off")
# adding two legends
legend1 = ax2.legend([handles[0]], [labels[0]], loc="upper left")
legend2 = ax2.legend([handles[1]], [labels[1]], loc="lower right")
ax2.add_artist(legend1)
plt.tight_layout()

How to manage subplots in Pandas?

My DataFrame is:
df = pd.DataFrame({'A': range(0,-10,-1), 'B': range(10,20), 'C': range(10,30,2)})
and plot:
df[['A','B','C']].plot(subplots=True, sharex=True)
I get one column with 3 subplots, each even height.
How to plot it this way that I have only two subplots where 'A' is in upper one and 'B' and 'C' are in lower and lower graph's height is different than height of graph 'A' (x axis is shared)?
Use subplots with gridspec_kw parmater to setup your grid then use the ax paramter in pandas plot to use those axes defined in your subplots statement:
f, ax = plt.subplots(2,2, gridspec_kw={'height_ratios':[1,2]})
df[['A','B','C']].plot(subplots=True, sharex=True, ax=[ax[0,0],ax[0,1],ax[1,0]])
ax[1,1].set_visible(False)
Output:
For clarity I post my modified code here:
f, ax = plt.subplots(2,1, sharex=True, gridspec_kw={'height_ratios':[1,3]})
f.subplots_adjust(hspace=0)
df[['A','B','C']].plot(subplots=True, ax=[ax[0],ax[1],ax[1]])
That will do it. Thanks.
I was able to do it with .subplot2grid(). Which only creates 3 plots as needed.
ax1 = plt.subplot2grid((3, 2), (0, 0), colspan=1)
ax2 = plt.subplot2grid((3, 2), (0, 1), colspan=1)
ax3 = plt.subplot2grid((3, 2), (1, 0), rowspan=2, sharex=ax1)
plt.setp(ax1.get_xticklabels(), visible=False)
ax1.plot(df['A'])
ax2.plot(df['B'], color='darkorange')
ax3.plot(df['C'], color='green')
Output:

How to adjust the plot size in Matplotlib?

I'm trying to remove the white space from the plot that I created:
As it is possible to see, there a big white spot on the right and also on the bottom, how to fix it? Here is my script:
fig = plt.figure(figsize=(7,7))
ax1 = plt.subplot2grid((4,3), (0,0),)
ax2 = plt.subplot2grid((4,3), (1,0),)
ax3 = plt.subplot2grid((4,3), (0,1),)
ax4 = plt.subplot2grid((4,3), (1,1),)
data = self.dframe[i]
tes = print_data(data, self.issues, self.color, self.type_user)
tes.print_top(data=data, top=10, ax=ax1, typegraph="hbar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax2, typegraph="prod_bar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax3, typegraph="reg_hbar", problem=self.issues[i], tone=self.color[i])
tes.print_top(data=data, top=10, ax=ax4, typegraph=self.type_user, problem=self.issues[i], tone=self.color[i])
problem = self.issues[i]
plt.tight_layout()
name = problem + str('.PNG')
plt.close(fig)
fig.savefig(name)
You are creating too many subplots!
If we look at this line:
ax1 = plt.subplot2grid((4,3), (0,0),)
We can see the first argument given to subplot2grid are the dimensions of the subplot grid to be made, in this case 4 rows, and 3 columns. You are then plotting in the subplots in the top left of your figure (the second argument given) which leaves a lot of space that's not used.
So to solve this, reduce the number of subplots by using:
ax1 = plt.subplot2grid((2,2), (0,0),)
Full example:
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(25)
fig = plt.figure(figsize=(7,7))
ax1 = plt.subplot2grid((2,2), (0,0),)
ax2 = plt.subplot2grid((2,2), (1,0),)
ax3 = plt.subplot2grid((2,2), (0,1),)
ax4 = plt.subplot2grid((2,2), (1,1),)
ax1.plot(data)
ax2.plot(data)
ax3.plot(data)
ax4.plot(data)
plt.show()
Giving:
you can use
plt.subplots_adjust(left=0.09, bottom=0.07, right=0.98, top=0.97, wspace=0.2 , hspace=0.17 ) to adjust the window.
But the issue is that a lot of the space in your plot is empty
maybe you should change
plt.subplot2grid((4,3)... to plt.subplot2grid((2,2)

Display two bar charts one by one in MATPLOTLIB

I am plotting bar charts using pivot tables. I have two independent pivot tables and need to produce two bar charts side-by-side and save it as a PNG image. Using this code, the chart is generated but it won't display as expected.
What I tried:
# Plotting Activity Begins
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(2, 2, 1)
vig = task_frame.plot(kind="bar", figsize=(8, 6), stacked=True, width=0.3, rot=20)
print "ax1",ax1
print "vig",vig
vicky = issue_frame.plot(kind="bar", figsize=(8, 6), stacked=True, width=0.3, rot=90)
print "ax2",ax2
print "vicky",vicky
plt.ylim((0, 10))
plt.rcParams.update({'font.size': 10})
plt.savefig("/tmp/" + str(current_date) + ".png")
My print statement values:
ax1 Axes(0.125,0.1;0.352273x0.8)
vig Axes(0.125,0.1;0.775x0.8)
ax2 Axes(0.125,0.536364;0.352273x0.363636)
vicky Axes(0.125,0.1;0.775x0.8)
How can I display the charts as side-by-side pictures in a single image? Where should I assign the ax1 and ax2 value?
When you plot try to add axes instance to plot function, like here:
...
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(2, 2, 1)
task_frame.plot(..., ax=ax1)
issue_frame.plot(..., ax=ax2)
...

Categories

Resources