如何使用matplotlib重新调整坐标轴?

huangapple go评论66阅读模式
英文:

How to rescale axes with matplotlib?

问题

我正在使用 pcolormesh 绘制图形:

fig, ax = plt.subplots()
ax.pcolormesh(Omega_re, Omega_im, brightness, cmap="gray", shading="gouraud")
ax.set_xlabel(r"$Re(\omega)$")
ax.set_ylabel(r"$Im(\omega)$")
ax.set_aspect(1)
fig.show()

其中

Omega_re = array([[2.  , 2.75, 3.5 , 4.25, 5.  ], [2.  , 2.75, 3.5 , 4.25, 5.  ],
       [2.  , 2.75, 3.5 , 4.25, 5.  ], [2.  , 2.75, 3.5 , 4.25, 5.  ],
       [2.  , 2.75, 3.5 , 4.25, 5.  ]])

Omega_im = array([[0. , 0. , 0. , 0. , 0. ], [0.0005, 0.0005, 0.0005, 0.0005, 0.0005],
       [0.001 , 0.001 , 0.001 , 0.001 , 0.001 ], [0.0015, 0.0015, 0.0015, 0.0015, 0.0015],
       [0.002 , 0.002 , 0.002 , 0.002 , 0.002 ]])

brightness = array([[4.05649772, 3.7299948 , 2.61262439, 4.2841897 , 3.40368949],
                    [4.05649076, 3.72997624, 2.61258381, 4.2841071 , 3.40353591],
                    [4.05648399, 3.72995772, 2.61254052, 4.28402084, 3.40338229],
                    [4.05647716, 3.72993901, 2.6124984 , 4.28393373, 3.40322865],
                    [4.05647025, 3.72992049, 2.61245428, 4.2838476 , 3.40307499]])

但由于 x 轴范围从 0 到 5,y 轴范围从 0 到 0.002,结果看起来“扁平”:

如何使用matplotlib重新调整坐标轴?

有没有一种方法可以重新缩放 y 轴,使图形看起来更好?下面是当前显示的图片。

英文:

I am plotting a graph using pcolormesh:

fig, ax = plt.subplots()
ax.pcolormesh(Omega_re, Omega_im, brightness, cmap="gray", shading="gouraud")
ax.set_xlabel(r"$Re(\omega)$")
ax.set_ylabel(r"$Im(\omega)$")
ax.set_aspect(1)
fig.show()

where

Omega_re = array([[2.  , 2.75, 3.5 , 4.25, 5.  ], [2.  , 2.75, 3.5 , 4.25, 5.  ],
       [2.  , 2.75, 3.5 , 4.25, 5.  ], [2.  , 2.75, 3.5 , 4.25, 5.  ],
       [2.  , 2.75, 3.5 , 4.25, 5.  ]])

Omega_im = array([[0. , 0. , 0. , 0. , 0. ], [0.0005, 0.0005, 0.0005, 0.0005, 0.0005],
       [0.001 , 0.001 , 0.001 , 0.001 , 0.001 ], [0.0015, 0.0015, 0.0015, 0.0015, 0.0015],
       [0.002 , 0.002 , 0.002 , 0.002 , 0.002 ]])

brightness = array([[4.05649772, 3.7299948 , 2.61262439, 4.2841897 , 3.40368949],
                    [4.05649076, 3.72997624, 2.61258381, 4.2841071 , 3.40353591],
                    [4.05648399, 3.72995772, 2.61254052, 4.28402084, 3.40338229],
                    [4.05647716, 3.72993901, 2.6124984 , 4.28393373, 3.40322865],
                    [4.05647025, 3.72992049, 2.61245428, 4.2838476 , 3.40307499]])

But since the x-axis ranges from 0 to 5 and the y-axis ranges from 0 to 0.002, the result appears "flattened.":

如何使用matplotlib重新调整坐标轴?

Is there a way to rescale the y-axis so that the graph looks visually better? Below is a picture of how it is currently being displayed.

答案1

得分: 0

轴线是“平的”,因为 ax.set_aspect(1),它将轴线的纵横比设置为相等。换句话说,1英寸(例如)在两个轴上的长度相同。如果删除这行代码,那么将会得到一个自动生成的纵横比,可以显示更多细节。

英文:

The axes are "flat" because of ax.set_aspect(1), which sets the aspect ratio of the axes to be equal. In other words, 1 inch (for example) is the same length on both axes. If you remove that line then you'll get an automatically-generated aspect ratio that will show more detail.

答案2

得分: 0

将以下内容中的代码部分翻译如下:

ax.set_aspect(1)

替换为

ax.set_aspect("auto")

或者直接删除这行代码。

set_aspect(1)(或等效地set_aspect("equal"))会强制坐标轴在x和y方向上使用相同的比例。使用set_aspect("auto")(默认值)允许matplotlib自动确定适合填充图形的比例。

结果

如何使用matplotlib重新调整坐标轴?

英文:

Replace

ax.set_aspect(1)

with

ax.set_aspect("auto")

or simply remove the line entirely.

set_aspect(1) (or equivalently set_aspect("equal")) forces the axes to use the same scaling for x and y. Using set_aspect("auto") (the default) allows matplotlib to infer a good scaling to fill the plot.

Result

如何使用matplotlib重新调整坐标轴?

huangapple
  • 本文由 发表于 2023年7月12日 21:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定