I’m plotting the same data in two different formats: log scale and linear scale.
Basically I want to have exactly the same plot, but with different scales, one on the top of the other.
What I have right now is this:
import matplotlib.pyplot as plt
# These are the plot 'settings'
plt.xlabel('Size')
plt.ylabel('Time(s)');
plt.title('Matrix multiplication')
plt.xticks(xl, rotation=30, size='small')
plt.grid(True)
# Settings are ignored when using two subplots
plt.subplot(211)
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
plt.subplot(212)
plt.yscale('log')
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
All ‘settings’ before plt.subplot are ignored.
I can get this to work the way I want, but I have to duplicate all the settings after each subplot declaration.
Is there a way to do configure both subplots at once?
The
plt.*settings usually apply to matplotlib’s current plot; withplt.subplot, you’re starting a new plot, hence the settings no longer apply to it. You can share labels, ticks, etc., by going through theAxesobjects associated with the plots (see examples here), but IMHO this would be overkill here. Instead, I would propose putting the common “styling” into one function and call that per plot:On a side note, you could root out more duplication by extracting your plot commands into such a function:
On another side note, it might suffice to put the title at the top of the figure (instead of over each plot), e.g., using
suptitle. Similary, it might be sufficient for thexlabelto only appear beneath the second plot: