简体   繁体   中英

Plotting two graphs that share an x-axis in matplotlib

I have to plot 2 graphs in a single screen. The x-axis remains the same but the y-axis should be different.

How can I do that in 'matplotlib'?

twinx is the function you're looking for; here's an example of how to use it.

双胞胎的例子

subplot will let you plot more than one figure on the same canvas. See the example on the linked documentation page.

There is an example of a shared axis plot in the examples directory, called shared_axis_demo.py :

from pylab import *

t = arange(0.01, 5.0, 0.01)
s1 = sin(2*pi*t)
s2 = exp(-t)
s3 = sin(4*pi*t)
ax1 = subplot(311)
plot(t,s1)
setp( ax1.get_xticklabels(), fontsize=6)

## share x only
ax2 = subplot(312, sharex=ax1)
plot(t, s2)
# make these tick labels invisible
setp( ax2.get_xticklabels(), visible=False)

# share x and y
ax3 = subplot(313,  sharex=ax1, sharey=ax1)
plot(t, s3)
xlim(0.01,5.0)
show() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM