I am trying to create a graph where the two subplots share the same x-axis. Below is my example code. I am having the following questions and issues:
In my example code, hiding the xticklabels for ax1 doesn’t do anything. Only the first tick label disappears but the remaining still stay.
I would like to close the gap between the two subplots. I do this with hspace=0. However, this setting is not respected when I use tight_layout().
What is the difference between method 1 and method 2 to create subplots with shared x-axis. Both seem to result in different behavior. For example, if I use method 1, there is no xticklabels on ax1, but the xlabel appears visible. If I use method 2, xticklabels appear, but xlabel disappears.
After I set hspace=0, how do I correct the almost overlap yticklabels? https://imgur.com/5w4xt9J.png
import matplotlib.pyplot as plt import numpy as np from matplotlib import gridspec %matplotlib notebook # Method 1 # fig, (ax1, ax2) = plt.subplots(2,1, sharex=True,) # Method 2 fig = plt.figure() gs = gridspec.GridSpec(2, 1,) ax1 = plt.subplot(gs) ax2 = plt.subplot(gs, sharex = ax1) # First subplot ax1.plot([1,2,3,4,5], [10,20,30,40,50], color='b') ax1.set_yscale('linear') ax1.set_ylabel( 'Temp' ) ax1.set_xscale('log') ax1.set_xlabel( 'Input Power' ) # Second subplot ax2.plot([1,2,3,4,5], [250,500,100,400,300], color='r') ax2.set_yscale('linear') ax2.set_ylabel( 'Speed' ) ax2.set_xscale('log') ax2.set_xlabel( 'Input Power' ) plt.setp(ax1.get_xticklabels(), visible=False) # plt.setp(ax2.get_xticklabels(), visible=False) # plt.subplots_adjust(hspace=0) # plt.tight_layout()