pylab 中的顶部和底部轴(例如带有不同的单位)(或左轴和右轴)

发布于 2024-07-17 06:53:33 字数 263 浏览 5 评论 0原文

我正在尝试使用 pylab/matplotlib 绘制绘图,并且我有两组不同的 x 轴单位。 所以我希望该图有两个具有不同刻度的轴,一个在顶部,一个在底部。 (例如,一个带有英里,一个带有公里左右。)

如下图所示(但我想要多个 X 轴,但这并不重要。)

Multiple axis with JpGraph

有人知道 pylab 是否可以实现这一点吗?

I'm trying to make a plot with pylab/matplotlib, and I have two different sets of units for the x axis. So what I would like the plot to have is two axis with different ticks, one on the top and one on the bottom. (E.g. one with miles and one with km or so.)

Something like the graph below (but than I would like multiple X axes, but that doesn't really matter.)

Multiple axis with JpGraph

Anyone an idea whether this is possible with pylab?

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

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

发布评论

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

评论(2

倒带 2024-07-24 06:53:33

这可能有点晚了,但看看这样的东西是否有帮助:

http://matplotlib。 sourceforge.net/examples/axes_grid/simple_axisline4.html

This might be a little late but see if something like this would help:

http://matplotlib.sourceforge.net/examples/axes_grid/simple_axisline4.html

ゝ偶尔ゞ 2024-07-24 06:53:33

在“原始”框架旁边添加多个轴(如示例图片所示)可以通过使用 twinx 添加附加轴并配置其 spines 属性来实现。

我认为这看起来与示例图片非常相似:

图 1

import matplotlib.pyplot as plt
import numpy as np

# Set font family
plt.rcParams["font.family"] = "monospace"

# Get figure, axis and additional axes
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twinx()
ax4 = ax1.twinx()

# Make space on the right for the additional axes
fig.subplots_adjust(right=0.6)

# Move additional axes to free space on the right
ax3.spines["right"].set_position(("axes", 1.2))
ax4.spines["right"].set_position(("axes", 1.4))

# Set axes limits
ax1.set_xlim(0, 7)
ax1.set_ylim(0, 10)
ax2.set_ylim(15, 55)
ax3.set_ylim(200, 600)
ax4.set_ylim(500, 750)

# Add some random example lines
line1, = ax1.plot(np.random.randint(*ax1.get_ylim(), 8), color="black")
line2, = ax2.plot(np.random.randint(*ax2.get_ylim(), 8), color="green")
line3, = ax3.plot(np.random.randint(*ax3.get_ylim(), 8), color="red")
line4, = ax4.plot(np.random.randint(*ax4.get_ylim(), 8), color="blue")

# Set axes colors
ax1.spines["left"].set_color(line1.get_color())
ax2.spines["right"].set_color(line2.get_color())
ax3.spines["right"].set_color(line3.get_color())
ax4.spines["right"].set_color(line4.get_color())

# Set up ticks and grid lines
ax1.minorticks_on()
ax2.minorticks_on()
ax3.minorticks_on()
ax4.minorticks_on()
ax1.tick_params(direction="in", which="both", colors=line1.get_color())
ax2.tick_params(direction="in", which="both", colors=line2.get_color())
ax3.tick_params(direction="in", which="both", colors=line3.get_color())
ax4.tick_params(direction="in", which="both", colors=line4.get_color())
ax1.grid(axis='y', which='major')

plt.show()

Adding multiple axes next to the "original" frame, as seen in the example picture, can be achieved by adding additional axes with twinx and configuring their spines attribute.

I think this looks quite similar to the example picture:

Figure 1

import matplotlib.pyplot as plt
import numpy as np

# Set font family
plt.rcParams["font.family"] = "monospace"

# Get figure, axis and additional axes
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twinx()
ax4 = ax1.twinx()

# Make space on the right for the additional axes
fig.subplots_adjust(right=0.6)

# Move additional axes to free space on the right
ax3.spines["right"].set_position(("axes", 1.2))
ax4.spines["right"].set_position(("axes", 1.4))

# Set axes limits
ax1.set_xlim(0, 7)
ax1.set_ylim(0, 10)
ax2.set_ylim(15, 55)
ax3.set_ylim(200, 600)
ax4.set_ylim(500, 750)

# Add some random example lines
line1, = ax1.plot(np.random.randint(*ax1.get_ylim(), 8), color="black")
line2, = ax2.plot(np.random.randint(*ax2.get_ylim(), 8), color="green")
line3, = ax3.plot(np.random.randint(*ax3.get_ylim(), 8), color="red")
line4, = ax4.plot(np.random.randint(*ax4.get_ylim(), 8), color="blue")

# Set axes colors
ax1.spines["left"].set_color(line1.get_color())
ax2.spines["right"].set_color(line2.get_color())
ax3.spines["right"].set_color(line3.get_color())
ax4.spines["right"].set_color(line4.get_color())

# Set up ticks and grid lines
ax1.minorticks_on()
ax2.minorticks_on()
ax3.minorticks_on()
ax4.minorticks_on()
ax1.tick_params(direction="in", which="both", colors=line1.get_color())
ax2.tick_params(direction="in", which="both", colors=line2.get_color())
ax3.tick_params(direction="in", which="both", colors=line3.get_color())
ax4.tick_params(direction="in", which="both", colors=line4.get_color())
ax1.grid(axis='y', which='major')

plt.show()

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