6

我想在 matplotlib 生成的图中仅显示 x 轴上的相关部分 - 使用pyplot包装库。

问题:如何通过传递区间范围的元组来强制绘图在某些轴位置截止,这些元组定义了要绘制的 x 轴的区间。理想情况下,应使用叠加在 x 轴上的垂直双波符号来表示截止。

使用xlimandaxis没有用,因为它只允许设置 x 轴的开始和结束,但没有中间的间隔:

在此处输入图像描述60 to 90具体来说,对于上面的图,不应显示 之间的 x 轴区域,并且应添加截止/不连续图标记。

import matplotlib.pyplot as pyplot
x1, x2 = 0, 50
y1, y2 = 0, 100
pyplot.xlim([x1, x2])
#alternatively
pyplot.axis([x1, x2, y1, y2])

不需要使用 matplotlib。

更新/总结:

  • Viktor 指向这个源,使用subplots-splitting 和两个图来模拟 matplotlib/pyplot 中的折线图。

在此处输入图像描述

4

2 回答 2

4

您在 matplotlib 示例中有一个断轴示例:断轴

在您的示例中,子图将仅共享 y 轴而不是 x 轴,并且将在 x 轴上设置限制。

条形图示例:

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
x = [1, 2, 3, 4, 5, 51, 52, 53, 54, 55]
y = [4, 3, 4, 5, 4, 3, 4, 5, 6, 4]
ax1.bar(x, y)
ax2.bar(x, y)

# Fix the axis
ax1.spines['right'].set_visible(False)
ax1.yaxis.tick_left()
ax2.spines['left'].set_visible(False)
ax2.yaxis.tick_right()
ax2.tick_params(labelleft='off')
ax1.set_xlim(1, 6)
ax2.set_xlim(51, 56)
于 2013-08-31T19:01:51.420 回答
-1

如果您使用 ,matplotlib.pyplot.xticks您可以控制所有标记的位置和值。

这个答案应该告诉你如何去做。

于 2013-08-31T18:52:49.657 回答