python 在 matplotlib 中绘制共享 x 轴的两个图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1404502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting two graphs that share an x-axis in matplotlib
提问by Mark Rushakoff
I have to plot 2 graphs in a single screen. The x-axis remains the same but the y-axis should be different.
我必须在一个屏幕中绘制 2 个图形。x 轴保持不变,但 y 轴应该不同。
How can I do that in 'matplotlib'?
我怎么能在'matplotlib'中做到这一点?
回答by Mark Rushakoff
twinx
is the function you're looking for; here's an exampleof how to use it.
回答by ire_and_curses
subplot
will let you plot more than one figure on the same canvas. See the example on the linked documentation page.
subplot
将让您在同一画布上绘制多个图形。请参阅链接文档页面上的示例。
There is an example of a shared axis plot in the examples directory, called shared_axis_demo.py
:
示例目录中有一个共享轴图的示例,名为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()