如何反转 x 或 y 轴
我有一个带有一堆随机 x、y 坐标的散点图。目前,Y 轴从 0 开始,一直到最大值。我希望 Y 轴从最大值开始一直到 0。
points = [(10,5), (5,11), (24,13), (7,8)]
x_arr = []
y_arr = []
for x,y in points:
x_arr.append(x)
y_arr.append(y)
plt.scatter(x_arr,y_arr)
I have a scatter plot graph with a bunch of random x, y coordinates. Currently the Y-Axis starts at 0 and goes up to the max value. I would like the Y-Axis to start at the max value and go up to 0.
points = [(10,5), (5,11), (24,13), (7,8)]
x_arr = []
y_arr = []
for x,y in points:
x_arr.append(x)
y_arr.append(y)
plt.scatter(x_arr,y_arr)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
有一个新的 API 使这一切变得更加简单。
和/或
There is a new API that makes this even simpler.
and/or
DisplacedAussie的答案是正确的,但通常更短的方法就是反转有问题的单轴:
其中
gca()
函数返回当前 Axes 实例,而[::-1]
反转列表。DisplacedAussie's answer is correct, but usually a shorter method is just to reverse the single axis in question:
where the
gca()
function returns the current Axes instance and the[::-1]
reverses the list.您还可以使用散点图的轴对象公开的函数
You could also use function exposed by the axes object of the scatter plot
使用 matplotlib.pyplot.axis()
axis( [xmin, xmax, ymin, ymax])
因此,您可以在末尾添加类似的内容:
尽管您可能希望在每一端进行填充,以便极值点不会位于边界上。
Use matplotlib.pyplot.axis()
axis([xmin, xmax, ymin, ymax])
So you could add something like this at the end:
Although you might want padding at each end so that the extreme points don't sit on the border.
如果您在
pylab
模式下使用 ipython,则需要使用
show()
来更新当前图形。If you're in ipython in
pylab
mode, thenthe
show()
is required to make it update the current figure.与上述方法类似的另一种方法是使用
plt.ylim
例如:当我尝试在 Y1 和/或 Y2 上复合多个数据集时,此方法适用于我
Another similar method to those described above is to use
plt.ylim
for example:This method works for me when I'm attempting to compound multiple datasets on Y1 and/or Y2
使用 ylim() 可能是适合您目的的最佳方法:
结果:
using ylim() might be the best approach for your purpose:
will result:
自 Matplotlib v3.1 轴上还有
set_inverted
方法。与接受的答案中使用的invert_[xy]axis
相比,它的优点是,无论调用多少次,您都会得到相同的结果。即invert_[xy]axis
反转当前方向,而set_inverted(False)
使其增加,set_inverted(True)
使其减少。Since Matplotlib v3.1 there is also the
set_inverted
method on an axis. The advantage of this over theinvert_[xy]axis
used in the accepted answer is that you get the same result regardless of how many times you call it. I.e.invert_[xy]axis
reverses the current direction whereasset_inverted(False)
makes it increasing andset_inverted(True)
makes it decreasing.或者,您可以使用 matplotlib.pyplot.axis()函数,它允许您反转任何绘图轴
或者如果您只想反转 X 轴,那么
实际上,您可以反转两个轴:
Alternatively, you can use the matplotlib.pyplot.axis() function, which allows you inverting any of the plot axis
Or if you prefer to only reverse the X-axis, then
Indeed, you can invert both axis:
如果您在子图中执行此操作,这是我发现的解决方案
虽然使用 sharex,最后两行需要位于 for 循环之外
if you are doing this in a subplot, here is the solution i found
While using sharex, last two lines needs to be outside the for loop
如果使用 matplotlib 你可以尝试:
<代码>
matplotlib.pyplot.xlim(l, r)
matplotlib.pyplot.ylim(b, t)
这两行分别设置 x 轴和 y 轴的限制。对于 x 轴,第一个参数 l 设置最左边的值,第二个参数 r 设置最右边的值。对于 y 轴,第一个参数 b 设置最底部的值,第二个参数 t 设置最顶部的值。
If using matplotlib you can try:
matplotlib.pyplot.xlim(l, r)
matplotlib.pyplot.ylim(b, t)
These two lines set the limits of the x and y axes respectively. For the x axis, the first argument l sets the left most value, and the second argument r sets the right most value. For the y axis, the first argument b sets the bottom most value, and the second argument t sets the top most value.