Matplotlib subplot y-scale scale overlaps with the chart above

I am trying to build 3 subtitles with no spaces between them. By default, the y-axis label uses the scale displayed in the upper right corner of the y-axis (1e-8 in the example below), which would be nice, except for the two lower graphs that overlap with the graph above. Does anyone know how to fix this? Below is a small example.

import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from matplotlib.ticker import MaxNLocator x = np.arange(0,200) y = np.random.rand(200) * 10e-8 fig = plt.figure(figsize=(10,15)) gs1 = gridspec.GridSpec(3, 3) gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0) ax0a = plt.subplot(gs1[0, :]) ax0b = plt.subplot(gs1[1, :]) ax0c = plt.subplot(gs1[2, :]) ax0a.set_xticklabels([]) ax0b.set_xticklabels([]) ax0a.plot(x,y) nbins = len(ax0a.get_xticklabels()) ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) ax0b.plot(x,y) ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) ax0c.plot(x,y) ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) 

plot

so one solution is to use mtick,

 import matplotlib.ticker as mtick ax0a.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e')) ax0b.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e')) ax0c.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1e')) 

but I would rather be able to shift the scale to the left so that it is off-axis, if possible.

+5
source share
2 answers

I have two options that you can see.

First set the location and size of the axis as such:

 # your imports and data above fig = plt.figure() ax0a = fig.add_axes([0.1, 0.1, 0.8, 0.25]) ax0b = fig.add_axes([0.1, 0.39, 0.8, 0.25], sharex=ax0a) ax0c = fig.add_axes([0.1, 0.68, 0.8, 0.25], sharex=ax0a) ax0a.set_xticklabels([]) ax0b.set_xticklabels([]) ax0a.plot(x,y) nbins = len(ax0a.get_xticklabels()) ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) ax0b.plot(x,y) ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) ax0c.plot(x,y) ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) plt.show() 

enter image description here

The second option is to manually adjust the location and possibly the font size of the offset text:

 # your original code minus data and imports fig = plt.figure() gs1 = gridspec.GridSpec(3, 3) gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0) ax0a = plt.subplot(gs1[0, :]) ax0b = plt.subplot(gs1[1, :]) ax0c = plt.subplot(gs1[2, :]) ax0a.set_xticklabels([]) ax0b.set_xticklabels([]) ax0a.plot(x,y) nbins = len(ax0a.get_xticklabels()) ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) ax0b.plot(x,y) ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) ax0c.plot(x,y) ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper')) # play around with location and font of offset text here ax0a.get_yaxis().get_offset_text().set_x(-0.075) ax0a.get_yaxis().get_offset_text().set_size(10) ax0b.get_yaxis().get_offset_text().set_x(-0.075) ax0b.get_yaxis().get_offset_text().set_size(10) ax0c.get_yaxis().get_offset_text().set_x(-0.075) ax0c.get_yaxis().get_offset_text().set_size(10) plt.show() 

enter image description here

+5
source

Well, this is probably an ugly solution, but you could just simply increase your data in the lower graphs with a power range, i.e. ax0b.plot(x, y*1e8) . This works for your example, at least.

0
source

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


All Articles