Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge matplotlib subplots with shared x-axis

Tags:

I have two graphs to where both have the same x-axis, but with different y-axis scalings.

The plot with regular axes is the data with a trend line depicting a decay while the y semi-log scaling depicts the accuracy of the fit.

fig1 = plt.figure(figsize=(15,6)) ax1 = fig1.add_subplot(111)  # Plot of the decay model  ax1.plot(FreqTime1,DecayCount1, '.', color='mediumaquamarine')  # Plot of the optimized fit ax1.plot(x1, y1M, '-k', label='Fitting Function: $f(t) = %.3f e^{%.3f\t} \          %+.3f$' % (aR1,kR1,bR1))  ax1.set_xlabel('Time (sec)') ax1.set_ylabel('Count') ax1.set_title('Run 1 of Cesium-137 Decay')  # Allows me to change scales # ax1.set_yscale('log') ax1.legend(bbox_to_anchor=(1.0, 1.0), prop={'size':15}, fancybox=True, shadow=True) 

enter image description here enter image description here

Now, i'm trying to figure out to implement both close together like the examples supplied by this link http://matplotlib.org/examples/pylab_examples/subplots_demo.html

In particular, this one

enter image description here

When looking at the code for the example, i'm a bit confused on how to implant 3 things:

1) Scaling the axes differently

2) Keeping the figure size the same for the exponential decay graph but having a the line graph have a smaller y size and same x size.

For example:

enter image description here

3) Keeping the label of the function to appear in just only the decay graph.

Any help would be most appreciated.

like image 929
iron2man Avatar asked Jun 09 '16 23:06

iron2man


People also ask

How do I share X-axis in matplotlib?

You can share the x or y axis limits for one axis with another by passing an axes instance as a sharex or sharey keyword argument.

How do I merge two graphs with different axis in Python?

The way to make a plot with two different y-axis is to use two different axes objects with the help of twinx() function. We first create figure and axis objects and make a first plot. In this example, we plot year vs lifeExp. And we also set the x and y-axis labels by updating the axis object.


1 Answers

Look at the code and comments in it:

import matplotlib.pyplot as plt import numpy as np from matplotlib import gridspec  # Simple data to display in various forms x = np.linspace(0, 2 * np.pi, 400) y = np.sin(x ** 2)  fig = plt.figure() # set height ratios for subplots gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1])   # the first subplot ax0 = plt.subplot(gs[0]) # log scale for axis Y of the first subplot ax0.set_yscale("log") line0, = ax0.plot(x, y, color='r')  # the second subplot # shared axis X ax1 = plt.subplot(gs[1], sharex = ax0) line1, = ax1.plot(x, y, color='b', linestyle='--') plt.setp(ax0.get_xticklabels(), visible=False) # remove last tick label for the second subplot yticks = ax1.yaxis.get_major_ticks() yticks[-1].label1.set_visible(False)  # put legend on first subplot ax0.legend((line0, line1), ('red line', 'blue line'), loc='lower left')  # remove vertical gap between subplots plt.subplots_adjust(hspace=.0) plt.show() 

enter image description here

like image 59
Serenity Avatar answered Sep 21 '22 03:09

Serenity