如何使用matplotlib在python中为子图设置相同的比例 [英] How to set same scale for subplots in python using matplotlib

查看:42
本文介绍了如何使用matplotlib在python中为子图设置相同的比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想轻松地在视觉上比较子图.为此,我想为所有子图设置相同的比例.

我的代码运行良好,我能够绘制子图,但具有自己的比例.我想保持 x 轴上的比例.

解决方案

如果要使用相同的xaxis创建两个子图,则可以使用

I want to compare subplots visually with ease. To do this, I want to set the same scale for all subplots.

My code works fine, and I'm able to plot subplots, but with their own scales. I want to maintain the scale on the x axis.

解决方案

If you want to have two subplots with the same xaxis, you can use the sharex-keyword when you create the second axes:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2, sharex=ax1)


t = np.linspace(0, 1, 1000)

ax1.plot(t, np.sin(2 * np.pi * t))
ax2.plot(t, np.cos(2 * np.pi * t))

plt.show()

Result:

这篇关于如何使用matplotlib在python中为子图设置相同的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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