Matplotlib: Subplot heights with squared imshow - python

I want to plot a three-panel figure, with an imshow on the top and two other plots below. My imshow is has the same x and y dimensions, ie. is squared. The two other plots have the same x dimension, they can therefore share the x axis. However, how do I specify that all three panels have the same width?
This is my code
import matplotlib.pyplot as plt
x = np.linspace(0,100,20)
y = np.sin(x)
z = np.random.rand(100,100)
fig, (ax, ax2, ax3) = plt.subplots(nrows=3, sharex=True, figsize=(10,10), gridspec_kw={"height_ratios": [2,1,1]})
# "main" plot
ax.imshow(z)
# accessory plot #1
ax2.plot(x, y)
ax2.set_xlim(0,99)
# accessory plot #2
ax3.plot(x*2, y)
plt.show()
I don't understand the behavior here, why does my imshow not have the correct width like the two panels below?
I found a solution here, but as far as I understand this can only create one additional axis per side?
This is my current output:
And I would like it to look like this:

Layout is defnitely something matplotlib could use some work on. What is happening is that you have a square figure, with height_ratios=[2, 1, 1]: the first axis (where you plot the image) has double the height of the other two. But the image you are plotting is also square, so it is using all the available height, while it has "white" space on the horizontal direction.
Try to play with figsize and maybe the height_ratios, something like this:
fig, (ax, ax2, ax3) = plt.subplots(nrows=3, sharex=True, figsize=(6,12), gridspec_kw={"height_ratios": [4,1,1]})

Related

How to change length of one plot in subplot?

How to change the length of one plot in a subplot?
It may be a simple problem but I have difficulty solving this.
To represent the result of signal analysis, I represented three plots in a subplot.
But, because the third graph had a colorbar, only this is short.
How can I solve this problem?
I added some parts that draw each plot in a subplot in my code except detail.
To avoid misunderstanding, I added figure.
In the below figure, the length of the spectrogram plot in the python figure(left) is shorter than the above two plots. But the length of the spectrogram plot in the Matlab figure(right) is equal to the above plots. How can make the length of the third plot be equal with the above plots, like the result of Matlab?
import matplotlib.pyplot as plt
fig, (ax1, ax2, ax3, cbar) = plt.subplots(3, 2)
ax1.plot(sb['Seconds'], sb['Real'], 'dodgerblue', linewidth = 0.5)
ax2.plot(f2, np.log(P3), 'k', linewidth = 0.5)
s, freqs, bins, im = ax3.specgram(y, NFFT = N, Fs=Fs1, cmap='jet')
cbar = plt.colorbar(im, ax=ax3, orientation = 'vertical', pad = 0.009)
If you already have the figure object use:
f.set_figheight(15)
f.set_figwidth(15)
But if you use the .subplots() command (as in the examples you're showing) to create a new figure you can also use:
f, axs = plt.subplots(2,2,figsize=(15,15))
For example: -
Alternatively, create a figure() object using the figsize argument and then use add_subplot to add your subplots. E.g.
import matplotlib.pyplot as plt
import numpy as np
f = plt.figure(figsize=(10,3))
ax = f.add_subplot(121)
ax2 = f.add_subplot(122)
x = np.linspace(0,4,1000)
ax.plot(x, np.sin(x))
ax2.plot(x, np.cos(x), 'r:')
Benefits of this method are that the syntax is closer to calls of subplot() instead of subplots(). E.g. subplots doesn't seem to support using a GridSpec for controlling the spacing of the subplots, but both subplot() and add_subplot() do.

How to set equal number of ticks for two subplots?

I have two subplots of horizontal bars done in matplotlib. For the first subplot, the number of y-axis ticks is appropriate, but I'm unable to figure out why specifying number of ticks for the second subplot is coming out to be wrong. This is the code:
import matplotlib.pyplot as plt
import numpy as np
# Plot separate subplots for genders
fig, (axes1, axes2) = plt.subplots(nrows=1, ncols=2,
sharex=False,
sharey=False,
figsize=(15,10))
labels = list(out.index)
x = ["20%", "40%", "60%", "80%", "100%"]
y = np.arange(len(out))
width = 0.5
axes1.barh(y, female_distr, width, color="olive",
align="center", alpha=0.8)
axes1.ticks_params(nbins=6)
axes1.set_yticks(y)
axes1.set_yticklabels(labels)
axes1.set_xticklabels(x)
axes1.yaxis.grid(False)
axes1.set_xlabel("Occurence (%)")
axes1.set_ylabel("Language")
axes1.set_title("Language Distribution (Women)")
axes2.barh(y, male_distr, width, color="chocolate",
align="center", alpha=0.8)
axes2.locator_params(nbins=6)
axes2.set_yticks(y)
axes2.set_yticklabels(labels)
axes2.set_xticklabels(x)
axes2.yaxis.grid(False)
axes2.set_xlabel("Occurence (%)")
axes2.set_ylabel("Language")
axes2.set_title("Language Distribution (Men)")
The rest of the objects like out are simple data frames that I don't think need to be described here. The above code returns the following plot:
I would like the second subplot to have equal number of ticks but experimenting with nbins always results in either more or fewer ticks than the first subplot.
First, if you want your two plots to have the same x-axis, why not use sharex=True?
x_ticks = [0,20,40,60,80,100]
fig, (ax1,ax2) = plt.subplots(1,2, sharex=True)
ax1.set_xticks(x_ticks)
ax1.set_xticklabels(['{:.0f}%'.format(x) for x in x_ticks])
ax1.set_xlim(0,100)
ax1.grid(True, axis='x')
ax2.grid(True, axis='x')

Prevent grid lines from twin axis to be drawn on top of artists from original axis

I have an axis on which I plot some data and I have another twin axis which I use to draw grid lines at specific tick positions (other than the ticks of the original axis):
import matplotlib.pyplot as plt
import numpy as np
f, ax = plt.subplots()
ax.set_xlim([0, 1])
ax2 = ax.twiny()
ax2.set_xlim([0, 1])
ax2.set_xticks(np.linspace(0, 1, 11))
ax2.xaxis.grid()
x = np.linspace(0, 1, 100)
ax.plot(x, np.sin(x), label='sin(x)')
ax.legend()
plt.show()
Now this has the undesirable effect that the grid lines of the twin axes are drawn on top of the legend and line plot of the original axis. As far as I understand this is because matplotlib draws the axes in the order they were created and for that reason zorder won't help (because zorder only specifies the order among the artists of a single axis).
I know I could plot the data on the twin axis ax2 instead (followed by ax2.legend()) but I'd prefer to have the setup as is. Instead changing the order in which the two axes are drawn should solve the problem, but I couldn't figure out how to do that. There is f.get_axes() which seems to return the axes in the order they were created but no option to revert it.
Or maybe there exists even another solution?
You can change the zorder of the axes themselves.
ax.set_zorder(2)
ax2.set_zorder(1)
ax.patch.set_visible(False)

matplotlib share xaxis with yaxis from another plot

If I want to tie the x and y axis of two separate axes together so that they zoom together I usually do something like this:
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122,sharex=ax1, sharey=ax1)
But I don't know how to share the xaxis of one plot with the yaxis of another plot. For example the xaxis of one plot is 'time' and I want to share that with the yaxis of another plot which also represents 'time'. Something like this (which doesn't work...):
ax2 = fig.add_subplot(122,sharex=ax1.yaxis, sharey=ax1.xaxis)
Thanks
I would be doing something like this,
fig, axs = plt.subplots(3, sharex=True, sharey=True) //Here
fig.suptitle('Sharing both axes')
axs[0].plot(x, y ** 2)
axs[1].plot(x, 0.3 * y, 'o')
axs[2].plot(x, y, '+')
##As per matplotlib docs and works fine for me too
Another way is you can superimpose too.

matplotlib: reduce axes width in subplots

I have a matplotlib bar chart, which bars are colored according to some rules through a colormap. I need a colorbar on the right of the main axes, so I added a new axes with
fig, (ax, ax_cbar) = plt.subplots(1,2)
and managed to draw my color bar in the ax_bar axes, while I have my data displayed in the ax axes. Now I need to reduce the width of the ax_bar, because it looks like this:
How can I do?
Using subplots will always divide your figure equally. You can manually divide up your figure in a number of ways. My preferred method is using subplot2grid.
In this example, we are setting the figure to have 1 row and 10 columns. We then set ax to be the start at row,column = (0,0) and have a width of 9 columns. Then set ax_cbar to start at (0,9) and has by default a width of 1 column.
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,6))
num_columns = 10
ax = plt.subplot2grid((1,num_columns), (0,0), colspan=num_columns-1)
ax_cbar = plt.subplot2grid((1,num_columns), (0,num_columns-1))
The ususal way to add a colorbar is by simply putting it next to the axes:
fig.colorbar(sm)
where fig is the figure and sm is the scalar mappable to which the colormap refers. In the case of the bars, you need to create this ScalarMappable yourself. Apart from that there is no need for complex creation of multiple axes.
import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
fig , ax = plt.subplots()
x = [0,1,2,3]
y = np.array([34,40,38,50])*1e3
norm = matplotlib.colors.Normalize(30e3, 60e3)
ax.bar(x,y, color=plt.cm.plasma_r(norm(y)) )
ax.axhline(4.2e4, color="gray")
ax.text(0.02, 4.2e4, "42000", va='center', ha="left", bbox=dict(facecolor="w",alpha=1),
transform=ax.get_yaxis_transform())
sm = plt.cm.ScalarMappable(cmap=plt.cm.plasma_r, norm=norm)
sm.set_array([])
fig.colorbar(sm)
plt.show()
If you do want to create a special axes for the colorbar yourself, the easiest method would be to set the width already inside the call to subplots:
fig , (ax, cax) = plt.subplots(ncols=2, gridspec_kw={"width_ratios" : [10,1]})
and later put the colorbar to the cax axes,
fig.colorbar(sm, cax=cax)
Note that the following questions have been asked for this homework assignment already:
Point picker event_handler drawing line and displaying coordinates in matplotlib
Matplotlib's widget to select y-axis value and change barplot
Display y axis value horizontal line drawn In bar chart
How to change colors automatically once a parameter is changed
Interactively Re-color Bars in Matplotlib Bar Chart using Confidence Intervals

Categories