Python 如何更改matlibplot中x轴和y轴的范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29337123/
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 04:26:25  来源:igfitidea点击:

How to change the range of the x-axis and y-axis in matlibplot?

pythonmatplotlib

提问by busebd12

I am new to matlibplot and I'm trying to draw a circle with radius 1 but have both my x and y axis go from 0 to 3 with an increment of 0.25. Right now, I have drawn the graph and the circle but my x and y axis only go from 0 to 1, so there is little room left either above, below, to the left, or to the right of the circle. Here's the code so far:

我是 matlibplot 的新手,我试图画一个半径为 1 的圆,但我的 x 和 y 轴都从 0 到 3,增量为 0.25。现在,我已经绘制了图形和圆圈,但我的 x 和 y 轴仅从 0 到 1,因此圆圈的上方、下方、左侧或右侧几乎没有剩余空间。这是到目前为止的代码:

    import numpy as np
    import matplotlib.pyplot as plt
    import scipy, pylab

    plt.axes()
    circle=plt.Circle((0, 0), radius=1, fc='w')
    plt.gca().add_patch(circle)
    plt.yticks(np.arange(0, 3, 0.25))
    plt.xticks(np.arange(0, 3, 0.25))
    plt.axis('scaled')
    plt.show()

I've looked at the following questions, but found them either to be a little too advanced for what I'm trying to accomplish or just a tad bit off-topic:

我查看了以下问题,但发现它们对于我要完成的任务来说有点太高级了,或者有点偏离主题:

    http://stackoverflow.com/questions/27170139/how-to-change-the-range-of-my-x-axis-in-matplotlib

    http://stackoverflow.com/questions/22642511/change-y-range-to-start-from-0-with-matplotlib

   http://stackoverflow.com/questions/27456185/scaling-axis-for-a-scatter-plot-in-matlibplot-in-python

   http://stackoverflow.com/questions/22642511/change-y-range-to-start-from-0-with-matplotlib

What I want to do now is, while keeping my circle in the same place on the graph, increase the range of my x and y axis from 0-1 to 0-3 while keeping the increment of 0.25 on each axis, allowing me to plot points all around the edge of the circle without having to worry about the top, bottom, or either side of the circle touching either of the two axis. I've looked through the matlibplot documentation, but can't seem to find a simple step-by-step explanation of how to change the spacing on my axis. Any insight on this would be brilliant! Thanks in advance!

我现在想要做的是,在将我的圆圈保持在图形上的同一位置的同时,将 x 和 y 轴的范围从 0-1 增加到 0-3,同时保持每个轴上的增量为 0.25,允许我围绕圆的边缘绘制点,而不必担心圆的顶部、底部或任一侧接触两个轴中的任何一个。我已经浏览了 matlibplot 文档,但似乎无法找到有关如何更改轴间距的简单分步说明。对此的任何见解都会很棒!提前致谢!

采纳答案by Julien Spronck

To change the axes range, you can use

要更改轴范围,您可以使用

plt.xlim([-3, 3])
plt.ylim([-3, 3])

You will then have to remove the line plt.axis('scaled')for this to work.

然后,您必须删除该行plt.axis('scaled')才能使其工作。

import numpy as np
import matplotlib.pyplot as plt
import scipy, pylab

plt.axes()
circle=plt.Circle((0, 0), radius=1, fc='w')
plt.gca().add_patch(circle)
plt.xlim([-3, 3])
plt.ylim([-3, 3])
plt.yticks(np.arange(-3, 3, 0.25))
plt.xticks(np.arange(-3, 3, 0.25))
plt.show()