如何在matplotlib中为子图设置相同的宽高比 [英] How to set same aspect ratio for subplots in matplotlib

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

问题描述

我想要一行具有相同纵横比的子图.假设我要在y轴上绘制两个具有不同程度的函数.

I want to have a row of subplots with the same aspect ratio. Let's say I want to plot two different functions that have different extents on the y axis.

对此似乎有很多问题,但是对于所有子图来说,示例似乎都具有相同的轴.无论如何,我发现没有一个对我有帮助.

There seem to be lots of questions on this but the examples seem to conveniently have the same axes for all subplots. In any case none that I found were helpful to me.

我已经尝试了以下

import numpy as np
import matplotlib.pyplot as plt

xdata = np.arange(0,2,.01)
ydata1 = xdata
ydata2 = xdata ** 2

fig, ax = plt.subplots(1,2)

ax[0].plot(xdata,ydata1)
ax[1].plot(xdata,ydata2)

ax[0].set_aspect(.5)
ax[1].set_aspect(.5)
plt.show(fig)

然而,matplotlib 似乎使用 x 和 y 轴数据来确定纵横比,并且因为 y 轴具有不同的范围,子图出现不同.这对我来说很奇怪.如何使它们具有相同的物理纵横比?

However, matplotlib appears to be using the x and y axis data to determine the aspect ratio and because the y axes have different extents the subplots come out different. This is super weird to me. How do I make them the same physical aspect ratio?

我想人们可以尝试提取每个子图的限制并单独计算正确的纵横比,但这似乎是一种非常迂回的方式,我希望内置这样的基本功能,例如使所有子图看起来都相同.

I guess one can try extracting the the limits for each subplot and calculate the right aspect ratio individually but this seems to be a very roundabout way and I would expect such a basic functionality as making all subplots look the same to be built in.

编辑因此,我想要的是两个子图在人类意义上具有相同的宽高比,即图形的宽度与高度的比率对于两个都相同.例如在 Mathematica 中,我只需设置 AspectRatio 属性即可实现这一点:

EDIT So what I want is for both subplots to have the same aspect ratio in the human sense, that is that the ratio of the width of the figure to the height is the same for both. For example in Mathematica I would achieve this by simply setting the AspectRatio attribute:

xdata = Range[0, 2, .01];
ydata1 = {#, #} & /@ xdata;
ydata2 = {#, #^2} & /@ xdata;
fig = GraphicsRow[
   ListLinePlot[#, AspectRatio -> 1/GoldenRatio, Frame -> True, 
      ImageSize -> 300] & /@ {ydata1, ydata2}];
Export["fig.png", fig]

推荐答案

简短回答:在制作子图时使用 figsize 关键字参数:

Short answer: use the figsize keyword argument when making subplots:

import numpy as np
import matplotlib.pyplot as plt

xdata = np.arange(0,2,.01)
ydata1 = xdata
ydata2 = xdata ** 2

fig = plt.figure(figsize=(10,6))
ax = fig.subplots(1,2)

ax[0].plot(xdata,ydata1)
ax[1].plot(xdata,ydata2)

# Squares
ax[0].plot([0,1,1,0],[0,0,1,1])
ax[1].plot([0,1,1,0],[0,0,1,1])

plt.show(fig)

figsize 是一个以英寸为单位的元组,第一个元素为 x 方向的宽度,第二个元素为 y 方向的宽度.

figsize is a tuple in inches, with the first element as the width in the x-direction, the second element as the width in the y-direction.

它适用于整个数字;您需要调整它以获得适合您的子图的形状.

It applies to the whole figure; you will need to tweak it to get a suitable shape for your subplots.

更长的答案: .set_aspect(num) 设置每个图的 y 轴与 x 轴的比率.

Longer answer: .set_aspect(num) sets the y-axis to x-axis ratio for each plot.

因此,当您在每个轴上使用 .set_aspect(.5) 时,您告诉 matplotlib 重新配置每个轴,使 y 轴为 0.5 倍作为x轴.这意味着 1x1 的正方形在每个图上实际上看起来像一个矩形:

So, when you used .set_aspect(.5) on each axis, you told matplotlib to reconfigure each axis such that the y-axis is 0.5 times as big as the x-axis. This means that a 1x1 square actually looks like a rectangle on each plot:

import numpy as np
import matplotlib.pyplot as plt

xdata = np.arange(0,2,.01)
ydata1 = xdata
ydata2 = xdata ** 2

fig, ax = plt.subplots(1,2)

ax[0].plot(xdata,ydata1)
ax[1].plot(xdata,ydata2)

# Squares
ax[0].plot([0,1,1,0],[0,0,1,1])
ax[1].plot([0,1,1,0],[0,0,1,1])

ax[0].set_aspect(.5)
ax[1].set_aspect(.5)
plt.show(fig)

这就是为什么你的情节是不同的形状;轴范围不同,但您已将两个图上的形状大小设置为相同.

That's why your plots are different shapes; the axis limits are different, but you've set the size of shapes on both plots to be the same.

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

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