Python: Matplotlib: Vertically aligned plots in matplotlib -
i read data multiple sources , plot them above each other. way need them plotted having single x-axis
labeled @ bottom, , others should aligned same x-axis
, no matter points available.
the following example of problem:
import matplotlib.pylab plt import random import matplotlib.gridspec gridspec random.seed(20) #create x-axis of data x1 = range(0,10) #different range next 1 x2 = range(1,9) #create data (just random data corresponding x1,x2) data1 = [random.random() in x1] data2 = [random.random()*1000 in x2] gs = gridspec.gridspec(2,1) fig = plt.figure() #first plot ax = fig.add_subplot(gs[0]) ax.plot(x1,data1) ax.set_ylabel(r'label one', size =16) ax.get_yaxis().set_label_coords(-0.1,0.5) plt.tick_params( axis='x', # changes apply x-axis labelbottom='off') # labels along bottom edge off #second plot ax = fig.add_subplot(gs[1]) ax.plot(x2,data2) ax.set_ylabel(r'label two', size =16) ax.get_yaxis().set_label_coords(-0.1,0.5) plt.show()
notice upper plot's x-axis
doesn't match lower plot's x-axis
.
i need plots match each other, , leave regions no data in smaller plots empty. can achieved?
if require additional information, please ask.
use sharex
argument add_subplot()
import matplotlib.pylab plt import random import matplotlib.gridspec gridspec random.seed(20) #create x-axis of data x1 = range(0,10) #different range next 1 x2 = range(1,9) #create data (just random data corresponding x1,x2) data1 = [random.random() in x1] data2 = [random.random()*1000 in x2] gs = gridspec.gridspec(2,1) fig = plt.figure() #first plot ax = fig.add_subplot(gs[0]) ax.plot(x1,data1) ax.set_ylabel(r'label one', size =16) ax.get_yaxis().set_label_coords(-0.1,0.5) plt.tick_params( axis='x', # changes apply x-axis labelbottom='off') # labels along bottom edge off #second plot ax = fig.add_subplot(gs[1], sharex=ax) ax.plot(x2,data2) ax.set_ylabel(r'label two', size =16) ax.get_yaxis().set_label_coords(-0.1,0.5) plt.show()
Comments
Post a Comment