Matplotlib-共享x轴图形的垂直高度 [英] Matplotlib - vertical height of a shared x-axis figure

查看:79
本文介绍了Matplotlib-共享x轴图形的垂直高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让两个图表底部的高度小得多.我尝试了 set_yscale(1,.5),但是它没有成功,正在寻找如何执行此操作.在文档中找不到任何内容.

I would like to made the bottom of the two charts have a much smaller height. I tried set_yscale(1,.5) but it was unsucessful, looking for how to do this. Couldn't find anything in the documentation.

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)



# Two subplots, the axes array is 1-d
f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(x, y)
axarr[0].set_title('Sharing X axis')
axarr[1].scatter(x, y)
axarr[1].set_yscale(1,.5)

plt.show()

推荐答案

您可以实现,例如通过使用 GridSpec 来定位图中的子图.这会给您的代码增加一点开销,但可以让您在绘图位置及其相对宽度和高度上具有完全的灵活性.

You can achieve that e.g. by using GridSpec to position the subplots in your figure. This adds a little overhead to your code, but gives you full flexibility over the plot positions and their relative widths and heights.

%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# create subplots' axes
fig = plt.figure()
top_pos, bot_pos = gridspec.GridSpec(2, 1, height_ratios=[4, 1])
top_ax = fig.add_subplot(top_pos)
bot_ax = fig.add_subplot(bot_pos, sharex=top_ax)

# do the plotting
top_ax.set_title('Sharing X axis')
top_ax.plot(x, y)
bot_ax.scatter(x, y)

这篇关于Matplotlib-共享x轴图形的垂直高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆