如何在 python 中使用 Matplotlib 或其他库设置轴间隔范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18551633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:02:33  来源:igfitidea点击:

How to set an axis interval range using Matplotlib or other libraries in python

pythonmatplotlibplotrange

提问by Lorenz Lo Sauer

I would like to show only the relevant parts on the x-axis in a matplotlib generated plot - using the pyplotwrapper library.

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

Question: How can the plot be forced to cutoff at certain axis-positions, by passing tuples of interval-ranges, that define the intervals for the x-axis to be plotted. Ideally the cutoff should be signified with a vertical double-waved sign superimposed on the x-axis.

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

Using xlimand axiswas of no use, as it only allows the beginning and end of the x-axis to be set but no intervals in-between:

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

enter image description hereSpecifically, for the plot above, the x-axis region between 60 to 90should not be shown, and cutoff/ discontinuous-plot marks should be added.

在此处输入图片说明具体来说,对于上面的绘图,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])

Using matplotlib is not a requirement.

使用 matplotlib 不是必需的。

Update/Summary:

更新/总结:

  • Viktor points to this source, using subplots-splitting and two plots to emulate a broken-line plot in matplotlib/pyplot.
  • Viktor 指向这个 source,使用subplots-splitting 和两个图来模拟 matplotlib/pyplot 中的折线图。

enter image description here

在此处输入图片说明

回答by Mike Vella

If you use the matplotlib.pyplot.xticksyou can control the location and value of all the marks.

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

This answershould show you how to do it.

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

回答by Viktor Kerkez

You have an example of the broken axis in the matplotlib examples: Broken Axis

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

In your example, subplots would just share the y axis instead of the x axis, and limits would be set on the x axis.

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

Example with a bar plot:

条形图示例:

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)