Matplotlib:一条线,针对不同单位的两个相关 x 轴绘制?

发布于 2024-09-07 01:01:09 字数 443 浏览 3 评论 0原文

我有一个 y 变量,我试图在图的顶部和底部针对两个相关的 x 轴绘制该变量(例如 y="立方体中的事物数量",x1="立方体的边长",x2="立方体的体积”)。我在 numpy 数组中有 y、x1、x2。我的x1和x2之间的关系是一对一且单调的,但并不简单,它们在不同的方向上增加,就像“边长”和“反体积”。我尝试过使用 twiny() 和 twin(),但这些似乎是为绘制不同的 y 变量而设计的。有什么想法吗?谢谢大家!

下面是我想要做的事情的一个例子,除了使用单行而不是符号。这个想法是,例如,sigma=0.4 和 M=2e15 对于一个点来说是等效且可互换的标签。

替代文本http://img580.imageshack.us/img580/4554/screenshotuy.png

I have one y variable, which I am trying to plot against two related x axes, on the top and bottom of the figure (e.g. y="number of things in cube", x1="side length of cube", x2="volume of cube"). I have y, x1, x2 in numpy arrays. The relationship between my x1 and x2 is one-to-one and monotonic, but not simple, and they increase in different directions, like "side length" and "inverse volume". I've tried using twiny() and twin(), but these seem to be designed for plotting different y variables. Any ideas? Thanks everyone!

Below is an example of the kind of thing I'm trying to do, except with a single line rather than symbols. The idea is that, say, sigma=0.4 and M=2e15 are equivalent and interchangeable labels for one point.

alt text http://img580.imageshack.us/img580/4554/screenshotuy.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2024-09-14 01:01:09

对于不同的 x 尺度,请使用 twiny()(将其视为“共享 y 轴”)。稍微改编自 matplotlib 文档 的示例:

import numpy as np
import matplotlib.pyplot as plt

# plot f(x)=x for two different x ranges
x1 = np.linspace(0, 1, 50)
x2 = np.linspace(0, 2, 50)
fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x1, x1,'b--')

ax2 = ax1.twiny()
ax2.plot(x2, x2, 'go')

plt.show()

如果您只想要第二个轴图第二个数据集不可见。

ax2.plot(x2, x2, alpha=0)

For different x-scales use twiny() (think of this as "shared y-axes"). An example slightly adapted from the matplotlib documentation:

import numpy as np
import matplotlib.pyplot as plt

# plot f(x)=x for two different x ranges
x1 = np.linspace(0, 1, 50)
x2 = np.linspace(0, 2, 50)
fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x1, x1,'b--')

ax2 = ax1.twiny()
ax2.plot(x2, x2, 'go')

plt.show()

If you just wanted a second axis plot the second data set as invisible.

ax2.plot(x2, x2, alpha=0)
仙气飘飘 2024-09-14 01:01:09

只是为了完整性:
存在“辅助轴”(matplotlib docs):

ax.secondary_xaxis('top', functions=(to_new_axis, from_new_axis))

两个函数

to_new_axis(), from_new_axis()

需要在当前单位和不同单位之间进行适当的缩放。

文档中的代码:

fig, ax = plt.subplots(constrained_layout=True)
x = np.arange(0, 360, 1)
y = np.sin(2 * x * np.pi / 180)
ax.plot(x, y)
ax.set_xlabel('angle [degrees]')
ax.set_ylabel('signal')
ax.set_title('Sine wave')

def deg2rad(x):
    return x * np.pi / 180

def rad2deg(x):
    return x * 180 / np.pi

secax = ax.secondary_xaxis('top', functions=(deg2rad, rad2deg))
secax.set_xlabel('angle [rad]')
plt.show()

这里,它们以度和弧度为单位显示正弦函数。
如果您没有明确定义的函数来在比例之间切换,您可以使用 numpy 插值:

def forward(x):
    return np.interp(x, xold, xnew)

def inverse(x):
    return np.interp(x, xnew, xold)

Just for completeness:
There exists 'secondary axes' (matplotlib docs):

ax.secondary_xaxis('top', functions=(to_new_axis, from_new_axis))

the two functions

to_new_axis(), from_new_axis()

need to give proper scaling between say, your current units, and different units.

Code from the documentation:

fig, ax = plt.subplots(constrained_layout=True)
x = np.arange(0, 360, 1)
y = np.sin(2 * x * np.pi / 180)
ax.plot(x, y)
ax.set_xlabel('angle [degrees]')
ax.set_ylabel('signal')
ax.set_title('Sine wave')

def deg2rad(x):
    return x * np.pi / 180

def rad2deg(x):
    return x * 180 / np.pi

secax = ax.secondary_xaxis('top', functions=(deg2rad, rad2deg))
secax.set_xlabel('angle [rad]')
plt.show()

Here, they show the sinus function, in units of degrees and radians.
In case you do not have well defined functions to switch between scales, you can use numpy interpolation:

def forward(x):
    return np.interp(x, xold, xnew)

def inverse(x):
    return np.interp(x, xnew, xold)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文