Python 合并具有共享 x 轴的 matplotlib 子图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37737538/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:52:49  来源:igfitidea点击:

Merge matplotlib subplots with shared x-axis

pythonmatplotlibsubplot

提问by iron2man

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

我有两个图表,它们都具有相同的 x 轴,但具有不同的 y 轴缩放比例。

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.

带有规则轴的图是带有描述衰减的趋势线的数据,而 y 半对数缩放描述了拟合的准确性。

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 hereenter 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

现在,我试图找出像此链接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:

在查看示例代码时,我对如何植入 3 件事感到有些困惑:

1) Scaling the axes differently

1)以不同的方式缩放轴

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.

2) 保持指数衰减图的图形大小相同,但折线图具有较小的 y 大小和相同的 x 大小。

For example:

例如:

enter image description here

在此处输入图片说明

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

3) 保持函数的标签只出现在衰减图中。

Any help would be most appreciated.

非常感激任何的帮助。

回答by Serenity

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 sublots
gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1]) 

# the fisrt 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 lened 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

在此处输入图片说明

回答by Aray Karjauv

Here is my solution:

这是我的解决方案:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, subplot_kw=dict(frameon=False)) # frameon=False removes frames

plt.subplots_adjust(hspace=.0)
ax1.grid()
ax2.grid()

ax1.plot(x, y, color='r')
ax1.set_yscale("log")
ax2.plot(x, y, color='b', linestyle='--')

enter image description here

在此处输入图片说明