GridSpec with shared axes in Python - python

This solution to another thread suggests using gridspec.GridSpec instead of plt.subplots. However, when I share axes between subplots, I usually use a syntax like the following
fig, axes = plt.subplots(N, 1, sharex='col', sharey=True, figsize=(3,18))
How can I specify sharex and sharey when I use GridSpec ?

First off, there's an easier workaround for your original problem, as long as you're okay with being slightly imprecise. Just reset the top extent of the subplots to the default after calling tight_layout:
fig, axes = plt.subplots(ncols=2, sharey=True)
plt.setp(axes, title='Test')
fig.suptitle('An overall title', size=20)
fig.tight_layout()
fig.subplots_adjust(top=0.9)
plt.show()
However, to answer your question, you'll need to create the subplots at a slightly lower level to use gridspec. If you want to replicate the hiding of shared axes like subplots does, you'll need to do that manually, by using the sharey argument to Figure.add_subplot and hiding the duplicated ticks with plt.setp(ax.get_yticklabels(), visible=False).
As an example:
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure()
gs = gridspec.GridSpec(1,2)
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1], sharey=ax1)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp([ax1, ax2], title='Test')
fig.suptitle('An overall title', size=20)
gs.tight_layout(fig, rect=[0, 0, 1, 0.97])
plt.show()

Both Joe's choices gave me some problems: the former, related with direct use of figure.tight_layout instead of figure.set_tight_layout() and, the latter, with some backends (UserWarning: tight_layout : falling back to Agg renderer). But Joe's answer definitely cleared my way toward another compact alternative. This is the result for a problem close to the OP's one:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=1, sharex='col', sharey=True,
gridspec_kw={'height_ratios': [2, 1]},
figsize=(4, 7))
fig.set_tight_layout({'rect': [0, 0, 1, 0.95], 'pad': 1.5, 'h_pad': 1.5})
plt.setp(axes, title='Test')
fig.suptitle('An overall title', size=20)
plt.show()

I made a function where you input a list or array of axes and it shares x or y along the rows and cols as specified. Not fully tested but here's the gist of it:
def share_axes(subplot_array, sharex, sharey, delete_row_ticklabels = 1, delete_col_ticklabels = 1):
shape = np.array(subplot_array).shape
if len(shape) == 1:
for i, ax in enumerate(subplot_array):
if sharex:
ax.get_shared_x_axes().join(ax, subplot_array[0])
if delete_row_ticklabels and not(i==len(subplot_array)-1):
ax.set_xticklabels([])
if sharey:
ax.get_shared_x_axes().join(ax, subplot_array[0])
if delete_col_ticklabels and not(i==0):
ax.set_yticklabels([])
elif len(shape) == 2:
for i in range(shape[0]):
for j in range(shape[1]):
ax = subplot_array[i,j]
if sharex in ('rows', 'both'):
ax.get_shared_x_axes().join(ax, subplot_array[-1,j])
if delete_row_ticklabels and not(i==shape[0]-1):
ax.set_xticklabels([])
if sharey in ('rows', 'both'):
ax.get_shared_y_axes().join(ax, subplot_array[-1,j])
if sharex in ('cols', 'both'):
ax.get_shared_x_axes().join(ax, subplot_array[i,0])
if sharey in ('cols', 'both'):
if delete_col_ticklabels and not(j==0):
ax.set_yticklabels([])
ax.get_shared_y_axes().join(ax, subplot_array[i,0])

Related

Share y axes for subplots that are dynamically created

Working example
import matplotlib.pyplot as plt
names = ['one','two','three']
upper = [[79,85,88],[79,85,88],[79,85,88]]
lower = [[73,72,66],[73,72,66],[73,72,66]]
fig = plt.figure(1)
for idx,lane in enumerate(names):
ax = fig.add_subplot(1,len(names)+1,idx+1)
ax.plot(upper[idx], color='tab:blue', marker='x', linestyle="None")
ax.plot(lower[idx], color='tab:blue', marker='x', linestyle="None")
ax.set_title(lane)
plt.show()
This generates 3 plots dynamically. It works I could very well not be using the best practices for dynamically generating plots. The goal is to have all the plots generated share the Y-axis so that it will give it a cleaner look. All the examples I've looked up show that you can assign the shared axis to the previously used axis but in my case all the plots are created dynamically. Is there a way to just lump all the subplots in a figure into sharing the same y axis?
The common approach to creating a figure with multiple axes is plt.subplots, which accepts a sharey = True argument.
Example:
import numpy as np
import matplotlib.pyplot as plt
xdata = np.linspace(0, 10, 100)
ydata_1 = np.sin(xdata)
ydata_2 = np.cos(xdata)
fig, (ax1, ax2) = plt.subplots(1, 2, sharey = True, figsize = (8, 4))
ax1.plot(xdata, ydata_1)
ax2.plot(xdata, ydata_2)
This outputs:
For less space between the plots, you can also use a tight_layout = True argument.
Using your data, you could maybe rewrite it to something like
fig, axes = plt.subplots(1, len(names), sharey = True, tight_layout = True)
for idx, (ax, name) in enumerate(zip(axes, names)):
ax.plot(upper[idx], color='tab:blue', marker='x', linestyle="None")
ax.plot(lower[idx], color='tab:blue', marker='x', linestyle="None")
ax.set_title(name)
plt.show()

Hide the short lines of axis tickers in plt.subplots [duplicate]

I can remove the ticks with
ax.set_xticks([])
ax.set_yticks([])
but this removes the labels as well. Any way I can plot the tick labels but not the ticks and the spine
You can set the tick length to 0 using tick_params (http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1],[1])
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.show()
Thanks for your answers #julien-spronck and #cmidi.
As a note, I had to use both methods to make it work:
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(11, 3))
data = np.random.random((4, 4))
ax1.imshow(data)
ax1.set(title='Bad', ylabel='$A_y$')
# plt.setp(ax1.get_xticklabels(), visible=False)
# plt.setp(ax1.get_yticklabels(), visible=False)
ax1.tick_params(axis='both', which='both', length=0)
ax2.imshow(data)
ax2.set(title='Somewhat OK', ylabel='$B_y$')
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
# ax2.tick_params(axis='both', which='both', length=0)
ax3.imshow(data)
ax3.set(title='Nice', ylabel='$C_y$')
plt.setp(ax3.get_xticklabels(), visible=False)
plt.setp(ax3.get_yticklabels(), visible=False)
ax3.tick_params(axis='both', which='both', length=0)
plt.show()
While attending a coursera course on Python, this was a question.
Below is the given solution, which I think is more readable and intuitive.
ax.tick_params(top=False,
bottom=False,
left=False,
right=False,
labelleft=True,
labelbottom=True)
This worked for me:
plt.tick_params(axis='both', labelsize=0, length = 0)
matplotlib.pyplot.setp(*args, **kwargs) is used to set properties of an artist object. You can use this in addition to get_xticklabels() to make it invisible.
something on the lines of the following
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
ax.set_xlabel("X-Label",fontsize=10,color='red')
plt.setp(ax.get_xticklabels(),visible=False)
Below is the reference page
http://matplotlib.org/api/pyplot_api.html
You can set the yaxis and xaxis set_ticks_position properties so they just show on the left and bottom sides, respectively.
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
Furthermore, you can hide the spines as well by setting the set_visible property of the specific spine to False.
axes[i].spines['right'].set_visible(False)
axes[i].spines['top'].set_visible(False)
This Worked out pretty well for me! try it out
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
languages =['Python', 'SQL', 'Java', 'C++', 'JavaScript']
pos = np.arange(len(languages))
popularity = [56, 39, 34, 34, 29]
plt.bar(pos, popularity, align='center')
plt.xticks(pos, languages)
plt.ylabel('% Popularity')
plt.title('Top 5 Languages for Math & Data \nby % popularity on Stack Overflow',
alpha=0.8)
# remove all the ticks (both axes),
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='off',
labelbottom='on')
plt.show()
Currently came across the same issue, solved as follows on version 3.3.3:
# My matplotlib ver: 3.3.3
ax.tick_params(tick1On=False) # "for left and bottom ticks"
ax.tick_params(tick2On=False) # "for right and top ticks, which are off by default"
Example:
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])
ax.tick_params(tick1On=False)
plt.show()
Output:
Assuming that you want to remove some ticks on the Y axes and only show the yticks that correspond to the ticks that have values higher than 0 you can do the following:
from import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# yticks and yticks labels
yTicks = list(range(26))
yTicks = [yTick if yTick % 5 == 0 else 0 for yTick in yTicks]
yTickLabels = [str(yTick) if yTick % 5 == 0 else '' for yTick in yTicks]
Then you set up your axis object's Y axes as follow:
ax.yaxis.grid(True)
ax.set_yticks(yTicks)
ax.set_yticklabels(yTickLabels, fontsize=6)
fig.savefig('temp.png')
plt.close()
And you'll get a plot like this:

How to stop xlim updating when subplots share x-axis

If I have
import matplotlib.pyplot as plt
plt.plot([0,1], [0,1])
plt.plot([0,2], [0,1], scalex=False)
plotting the second line does not update the axes xlim:
However, if I create subplots with a shared x-axis, the scalex kwarg appears to have no effect:
fig, ax_arr = plt.subplots(2, 1, sharex=True)
for ax in ax_arr.flat:
ax.plot([0,1], [0,1])
ax.plot([0,2], [0,1], scalex=False)
Is there another kwarg or setting somewhere that can be used to stop a plotted line affecting the axes xlim in this example?
scalex affects the autoscaling in the moment the plot is created. It will not be stored to take effect in further calls to autoscale.
An option is to turn autoscaling off in general for all but the first axes.
import matplotlib.pyplot as plt
fig, ax_arr = plt.subplots(2, 1, sharex=True)
ax_arr[1].set_autoscalex_on(False)
for ax in ax_arr.flat:
ax.plot([0,1], [0,1])
ax.plot([0,2], [0,1], scalex=False)
plt.show()
I've accepted ImportanceOfBeingErnest's answer as it does address my specific minimal example above. As my "real" example involves subplots where the first plot on each axes will not be the same, I include this further answer in case it's of use to anyone else:
fig, ax_arr = plt.subplots(2, 1, sharex=True)
ax_arr.flat[0].plot([0,1], [0,1])
ax_arr.flat[1].plot([-1,0], [0,1])
for ax in ax_arr.flat:
ax.set_autoscalex_on(False)
ax.plot([0,2], [0,1])

How to sharex and sharey axis in for loop

I'm trying to share the x-axis and y-axis of my sumplots, I've tried using the sharey and sharex several different ways but haven't gotten the correct result.
ax0 = plt.subplot(4,1,1)
for i in range(4):
plt.subplot(4,1,i+1,sharex = ax0)
plt.plot(wavelength[i],flux)
plt.xlim([-1000,1000])
plt.ylim([0,1.5])
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
If I understood you correctly, want to have four stacked plots, sharing the x-axis and the y-axis. This you can do with plt.subplots and the keywords sharex=True and sharey=True. See example below:
import numpy as np
import matplotlib.pyplot as plt
fig, axlist = plt.subplots(4, 1, sharex=True, sharey=True)
for ax in axlist:
ax.plot(np.random.random(100))
plt.show()

How to add a title to each subplot

I have one figure which contains many subplots.
fig = plt.figure(num=None, figsize=(26, 12), dpi=80, facecolor='w', edgecolor='k')
fig.canvas.set_window_title('Window Title')
# Returns the Axes instance
ax = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax3 = fig.add_subplot(313)
How do I add titles to the subplots?
fig.suptitle adds a title to all graphs and although ax.set_title() exists, the latter does not add any title to my subplots.
Thank you for your help.
Edit:
Corrected typo about set_title(). Thanks Rutger Kassies
ax.title.set_text('My Plot Title') seems to work too.
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.title.set_text('First Plot')
ax2.title.set_text('Second Plot')
ax3.title.set_text('Third Plot')
ax4.title.set_text('Fourth Plot')
plt.show()
ax.set_title() should set the titles for separate subplots:
import matplotlib.pyplot as plt
if __name__ == "__main__":
data = [1, 2, 3, 4, 5]
fig = plt.figure()
fig.suptitle("Title for whole figure", fontsize=16)
ax = plt.subplot("211")
ax.set_title("Title for first plot")
ax.plot(data)
ax = plt.subplot("212")
ax.set_title("Title for second plot")
ax.plot(data)
plt.show()
Can you check if this code works for you? Maybe something overwrites them later?
A shorthand answer assuming
import matplotlib.pyplot as plt:
plt.gca().set_title('title')
as in:
plt.subplot(221)
plt.gca().set_title('title')
plt.subplot(222)
etc...
Then there is no need for superfluous variables.
If you want to make it shorter, you could write :
import matplotlib.pyplot as plt
for i in range(4):
plt.subplot(2,2,i+1).set_title(f'Subplot n°{i+1}')
plt.show()
It makes it maybe less clear but you don't need more lines or variables
A solution I tend to use more and more is this one:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2) # 1
for i, ax in enumerate(axs.ravel()): # 2
ax.set_title("Plot #{}".format(i)) # 3
Create your arbitrary number of axes
axs.ravel() converts your 2-dim object to a 1-dim vector in row-major style
assigns the title to the current axis-object
fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=1, ncols=4,figsize=(11, 7))
grid = plt.GridSpec(2, 2, wspace=0.2, hspace=0.5)
ax1 = plt.subplot(grid[0, 0])
ax2 = plt.subplot(grid[0, 1:])
ax3 = plt.subplot(grid[1, :1])
ax4 = plt.subplot(grid[1, 1:])
ax1.title.set_text('First Plot')
ax2.title.set_text('Second Plot')
ax3.title.set_text('Third Plot')
ax4.title.set_text('Fourth Plot')
plt.show()
In case you have multiple images and you want to loop though them and show them 1 by 1 along with titles - this is what you can do. No need to explicitly define ax1, ax2, etc.
The catch is you can define dynamic axes(ax) as in Line 1 of code
and you can set its title inside a loop.
The rows of 2D array is length (len) of axis(ax)
Each row has 2 items i.e. It is list within a list (Point No.2)
set_title can be used to set title, once the proper axes(ax) or subplot is selected.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, figsize=(6, 8))
for i in range(len(ax)):
for j in range(len(ax[i])):
## ax[i,j].imshow(test_images_gr[0].reshape(28,28))
ax[i,j].set_title('Title-' + str(i) + str(j))
You are able to give every graph a different title and label by Iteration only.
titles = {221: 'First Plot', 222: 'Second Plot', 223: 'Third Plot', 224: 'Fourth Plot'}
fig = plt.figure()
for x in range(221,225):
ax = fig.add_subplot(x)
ax.title.set_text(titles.get(x))
plt.subplots_adjust(left=0.1,
bottom=0.1,
right=0.9,
top=0.9,
wspace=0.4,
hspace=0.4)
plt.show()
Output:
As of matplotlib 3.4.3, the Figure.add_subplot function supports kwargs with title as:
fig.add_subplot(311, title="first")
fig.add_subplot(312, title="second")
For completeness, the requested result can also be achieve without explicit reference to the figure axes as follows:
import matplotlib.pyplot as plt
plt.subplot(221)
plt.title("Title 1")
plt.subplot(222)
plt.title("Title 2")
plt.subplot(223)
plt.title("Title 3")
plt.subplot(224)
plt.title("Title 4")
Use plt.tight_layout() after the last plot if you have issues with overlapping labels.

Categories