如何使用不同比例轴的2D数据栏创建条形图

发布于 2025-01-27 11:28:11 字数 643 浏览 3 评论 0原文

我想创建这样的图:

”在此处输入图像描述”

其中只有图表的前半部分。我尝试使用两个刻度y轴,但是我无法将新轴从0开始。这可能是移动新轴吗?还是有其他方法,如何获得此图?

# dummy data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [10000, 200000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 100001, 12000]
y2 = [1, 2, 45, 4, 5, 6, 2, 8, 9, 10, 11, 1]

fig, ax1 = plt.subplots(figsize=(5, 5), dpi=80)
ax2 = ax1.twinx()

# plot data
ax1.bar(x, y, color=color, log=True)
ax2.bar(x, y2,color=color)
ax2.invert_yaxis()

I would like to create graph like this:

enter image description here

Where only first half of the graph is in log scale. I tried to use two scale y axis, but I was not able to shift new axis to start from 0. It is possible shift new axis ? Or is there some other way, how to obtain this graph ?

# dummy data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [10000, 200000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 100001, 12000]
y2 = [1, 2, 45, 4, 5, 6, 2, 8, 9, 10, 11, 1]

fig, ax1 = plt.subplots(figsize=(5, 5), dpi=80)
ax2 = ax1.twinx()

# plot data
ax1.bar(x, y, color=color, log=True)
ax2.bar(x, y2,color=color)
ax2.invert_yaxis()

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

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

发布评论

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

评论(1

风渺 2025-02-03 11:28:11

您可以通过组合两个图来绘制它,这似乎是提供的输出所做的:

# dummy data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [10000, 200000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 100001, 12000]
y2 = [1, 2, 45, 4, 5, 6, 2, 8, 9, 10, 11, 1]

fig = plt.figure()
gs = fig.add_gridspec(2, hspace=0)
axs = gs.subplots(sharex=True, sharey=False)
fig.suptitle('Your title')
axs[0].bar(x, y, log=True)
axs[1].bar(x, y2)
axs[1].invert_yaxis()

“在此处输入图像说明”

You could plot it by combining two plots, which seems to be what the provided output is doing:

# dummy data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [10000, 200000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 100001, 12000]
y2 = [1, 2, 45, 4, 5, 6, 2, 8, 9, 10, 11, 1]

fig = plt.figure()
gs = fig.add_gridspec(2, hspace=0)
axs = gs.subplots(sharex=True, sharey=False)
fig.suptitle('Your title')
axs[0].bar(x, y, log=True)
axs[1].bar(x, y2)
axs[1].invert_yaxis()

enter image description here

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