繁体   English   中英

Matplotlib:如何添加另一个 X 轴,其中刻度对应于带有自定义标签的图形上的点

[英]Matplotlib: How to add another X-axis where the ticks correspond to the points on the graph with custom labels

我想从一个脚本中获取 plot 数据。 我的代码是这样的:

f, ax = plt.subplots()

a = [10, 20, 30] # N Total Items
b = [1, 5, 10] # Time (s)
c = [2, 6, 7] # N items of lenght 1

ax.plot(a, b, '-m^')
ax2 = ax.twiny()
ax2.set_xticks(ax.get_xticks())
ax2.set_xticklabels(c)
ax.set_xlabel("N Total Items")
ax.set_ylabel("Time (s)")
ax2.set_xlabel("N items of lenght 1")

plot

我希望 C 值表示对应于图表上绘制的每个点的刻度。 因此,“2”对应点 (10, 1),“6”对应点 (20, 5),依此类推。

有什么方法可以通过 matplotlib 实现这一目标? 先感谢您。

这是通过对齐轴限制然后替换刻度标签来做到这一点的一种方法

ax.plot(a, b, '-m^')

ax2 = ax.twiny()
ax2.set_xlim(ax.get_xlim())
ax2.set_xticks(a)
ax2.set_xticklabels(c)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM