Show label labels when sharing axis in matplotlib

I run the following function:

def plot_variance_analysis(indices, stat_frames, legend_labels, shape): x = np.linspace(1, 5, 500) fig, axes = plt.subplots(shape[0], shape[1], sharex=True sharey=True) questions_and_axes = zip(indices, axes.ravel()) frames_and_labels = zip(stat_frames, legend_labels) for qa in questions_and_axes: q = qa[0] ax = qa[1] for fl in frames_and_labels: frame = fl[0] label = fl[1] ax.plot(x, stats.norm.pdf(x, frame['mean'][q], frame['std'][q]), label=label) ax.set_xlabel(q) ax.legend(loc='best') plt.xticks([1,2,3,4,5]) return fig, axes 

Here is what I get with some of my own data examples:

enter image description here

I am trying to maintain a common state between the axes, but at the same time display label labels for the x axis on all subheadings (including the top two). I can not find any means to disable this in the documentation. Any suggestions? Or should I just set the caption axis x to the axis?

I am running matplotlib 1.4.0 if this is important.

+8
source share
2 answers

Missing ticks have the visible property set to False . This is indicated in the documentation for plt.subplot . The simplest way to fix this is probably to do:

 for ax in axes.flatten(): for tk in ax.get_yticklabels(): tk.set_visible(True) for tk in ax.get_xticklabels(): tk.set_visible(True) 

Here I am fixated on all the axes that you don't need to do, but this code is simpler. You can also do this with a list in an ugly liner if you want:

 [([tk.set_visible(True) for tk in ax.get_yticklabels()], [tk.set_visible(True) for tk in ax.get_yticklabels()]) for ax in axes.flatten()] 
+17
source

In Matplotlib 2.2, tag labels can be returned back using:

 ax.xaxis.set_tick_params(labelbottom=True) 
+9
source

Source: https://habr.com/ru/post/984254/


All Articles