在matplotlib中,如何在图形的两侧显示轴?

发布于 2024-11-28 13:41:14 字数 221 浏览 2 评论 0原文

我想用 matplotlib 绘制一个图,图两侧都有轴,类似于此图(颜色与此问题无关):

plot

如何使用 matplotlib 做到这一点?

注意:与示例图中显示的相反,我希望两个轴完全相同,并且只想显示一张图。添加两个轴只是为了使图表更容易阅读。

I want to draw a plot with matplotlib with axis on both sides of the plot, similar to this plot (the color is irrelevant to this question):

plot

How can I do this with matplotlib?

Note: contrary to what is shown in the example graph, I want the two axis to be exactly the same, and want to show only one graph. Adding the two axis is only to make reading the graph easier.

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

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

发布评论

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

评论(4

奈何桥上唱咆哮 2024-12-05 13:41:14

您可以使用 tick_params() (这是我在 Jupyter 笔记本中所做的):

import matplotlib.pyplot as plt

bar(range(10), range(10))
tick_params(labeltop=True, labelright=True)

生成此图像:

条形图,x 轴和 y 轴标记相同

UPD: 添加了一个简单的子图示例。您应该将 tick_params() 与 axis 对象一起使用。

此代码设置为仅显示顶部子图的顶部标签和底部子图的底部标签(带有相应的刻度):

import matplotlib.pyplot as plt

f, axarr = plt.subplots(2)

axarr[0].bar(range(10), range(10))
axarr[0].tick_params(labelbottom=False, labeltop=True, labelleft=False, labelright=False,
                     bottom=False, top=True, left=False, right=False)

axarr[1].bar(range(10), range(10, 0, -1))
axarr[1].tick_params(labelbottom=True, labeltop=False, labelleft=False, labelright=False,
                     bottom=True, top=False, left=False, right=False)

如下所示:

“子图勾选配置示例”

You can use tick_params() (this I did in Jupyter notebook):

import matplotlib.pyplot as plt

bar(range(10), range(10))
tick_params(labeltop=True, labelright=True)

Generates this image:

Bar plot with both x and y axis labeled the same

UPD: added a simple example for subplots. You should use tick_params() with axis object.

This code sets to display only top labels for the top subplot and bottom labels for the bottom subplot (with corresponding ticks):

import matplotlib.pyplot as plt

f, axarr = plt.subplots(2)

axarr[0].bar(range(10), range(10))
axarr[0].tick_params(labelbottom=False, labeltop=True, labelleft=False, labelright=False,
                     bottom=False, top=True, left=False, right=False)

axarr[1].bar(range(10), range(10, 0, -1))
axarr[1].tick_params(labelbottom=True, labeltop=False, labelleft=False, labelright=False,
                     bottom=True, top=False, left=False, right=False)

Looks like this:

Subplots ticks config example

堇色安年 2024-12-05 13:41:14

在线文档中有几个相关示例:

There are a couple of relevant examples in the online documentation:

瞳孔里扚悲伤 2024-12-05 13:41:14

我之前已经使用以下方法完成了此操作:

# Create figure and initial axis    
fig, ax0 = plt.subplots()
# Create a duplicate of the original xaxis, giving you an additional axis object
ax1 = ax.twinx()
# Set the limits of the new axis from the original axis limits
ax1.set_ylim(ax0.get_ylim())

这将完全复制原始 y 轴。

例如:

ax = plt.gca()

plt.bar(range(3), range(1, 4))
plt.axhline(1.75, color="gray", ls=":")

twin_ax = ax.twinx()
twin_ax.set_yticks([1.75])
twin_ax.set_ylim(ax.get_ylim())

在此输入图像描述

I've done this previously using the following:

# Create figure and initial axis    
fig, ax0 = plt.subplots()
# Create a duplicate of the original xaxis, giving you an additional axis object
ax1 = ax.twinx()
# Set the limits of the new axis from the original axis limits
ax1.set_ylim(ax0.get_ylim())

This will exactly duplicate the original y-axis.

Eg:

ax = plt.gca()

plt.bar(range(3), range(1, 4))
plt.axhline(1.75, color="gray", ls=":")

twin_ax = ax.twinx()
twin_ax.set_yticks([1.75])
twin_ax.set_ylim(ax.get_ylim())

enter image description here

我很坚强 2024-12-05 13:41:14

文档非常有帮助。为了在图的左侧和右侧显示相同的 y 轴刻度和刻度标签,我得到了与以下内容一致的良好结果:

ax0 = ax.twinx()
ax0.set_ylim(ax.get_ylim())
ax0.set_yticks(ax.get_yticks(), ax.get_yticklabels(),
               fontsize= 8)

其中 ax 是默认显示,y 的刻度和刻度标签位于左侧。第一行代码创建 ax0 与 ax (twinx()) 共享 x 轴。第二行设置 ax0 的 y 轴的最小值和最大值以匹配 ax 的 y 轴。最后一行设置 ax0 的 y 刻度和刻度标签以匹配 ax。

对于两个独立的 y 轴,请参阅上面 John Bartholomew 的评论和参考文献。

The documentation is very helpful. For showing the same y axis ticks and tick labels on both the left side and the right side of a plot, I have had good, consistent results with the following:

ax0 = ax.twinx()
ax0.set_ylim(ax.get_ylim())
ax0.set_yticks(ax.get_yticks(), ax.get_yticklabels(),
               fontsize= 8)

where ax is the default display with y's ticks and tick labels on the left. This first line of code creates ax0 to share the x axis with ax (twinx()). The second line sets the min and max values for ax0's y axis to match those of ax's y axis. The last line sets ax0's y ticks and tick labels to match those of ax.

For two, separate y axes, see John Bartholomew's remarks and references above.

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