如何反转 x 或 y 轴

发布于 2024-08-17 14:24:07 字数 246 浏览 3 评论 0原文

我有一个带有一堆随机 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

这样的小城市 2024-08-24 14:24:07

有一个新的 API 使这一切变得更加简单。

plt.gca().invert_xaxis()

和/或

plt.gca().invert_yaxis()

There is a new API that makes this even simpler.

plt.gca().invert_xaxis()

and/or

plt.gca().invert_yaxis()
苦笑流年记忆 2024-08-24 14:24:07

DisplacedAussie的答案是正确的,但通常更短的方法就是反转有问题的单轴:

plt.scatter(x_arr, y_arr)
ax = plt.gca()
ax.set_ylim(ax.get_ylim()[::-1])

其中 gca() 函数返回当前 Axes 实例,而 [::-1] 反转列表。

DisplacedAussie's answer is correct, but usually a shorter method is just to reverse the single axis in question:

plt.scatter(x_arr, y_arr)
ax = plt.gca()
ax.set_ylim(ax.get_ylim()[::-1])

where the gca() function returns the current Axes instance and the [::-1] reverses the list.

心不设防 2024-08-24 14:24:07

您还可以使用散点图的轴对象公开的函数

scatter = plt.scatter(x, y)
ax = scatter.axes
ax.invert_xaxis()
ax.invert_yaxis()

You could also use function exposed by the axes object of the scatter plot

scatter = plt.scatter(x, y)
ax = scatter.axes
ax.invert_xaxis()
ax.invert_yaxis()
不即不离 2024-08-24 14:24:07

使用 matplotlib.pyplot.axis()

axis( [xmin, xmax, ymin, ymax])

因此,您可以在末尾添加类似的内容:

plt.axis([min(x_arr), max(x_arr), max(y_arr), 0])

尽管您可能希望在每一端进行填充,以便极值点不会位于边界上。

Use matplotlib.pyplot.axis()

axis([xmin, xmax, ymin, ymax])

So you could add something like this at the end:

plt.axis([min(x_arr), max(x_arr), max(y_arr), 0])

Although you might want padding at each end so that the extreme points don't sit on the border.

最美的太阳 2024-08-24 14:24:07

如果您在 pylab 模式下使用 ipython,则

plt.gca().invert_yaxis()
show()

需要使用 show() 来更新当前图形。

If you're in ipython in pylab mode, then

plt.gca().invert_yaxis()
show()

the show() is required to make it update the current figure.

我一直都在从未离去 2024-08-24 14:24:07

与上述方法类似的另一种方法是使用 plt.ylim 例如:

plt.ylim(max(y_array), min(y_array))

当我尝试在 Y1 和/或 Y2 上复合多个数据集时,此方法适用于我

Another similar method to those described above is to use plt.ylim for example:

plt.ylim(max(y_array), min(y_array))

This method works for me when I'm attempting to compound multiple datasets on Y1 and/or Y2

永不分离 2024-08-24 14:24:07

使用 ylim() 可能是适合您目的的最佳方法:

xValues = list(range(10))
quads = [x** 2 for x in xValues]
plt.ylim(max(quads), 0)
plt.plot(xValues, quads)

结果:在此处输入图像描述

using ylim() might be the best approach for your purpose:

xValues = list(range(10))
quads = [x** 2 for x in xValues]
plt.ylim(max(quads), 0)
plt.plot(xValues, quads)

will result:enter image description here

ゃ人海孤独症 2024-08-24 14:24:07

Matplotlib v3.1 轴上还有 set_inverted 方法。与接受的答案中使用的 invert_[xy]axis 相比,它的优点是,无论调用多少次,您都会得到相同的结果。即 invert_[xy]axis 反转当前方向,而 set_inverted(False) 使其增加,set_inverted(True) 使其减少。

ax = plt.gca()
ax.xaxis.set_inverted(True)
ax.yaxis.set_inverted(True)

Since Matplotlib v3.1 there is also the set_inverted method on an axis. The advantage of this over the invert_[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 whereas set_inverted(False) makes it increasing and set_inverted(True) makes it decreasing.

ax = plt.gca()
ax.xaxis.set_inverted(True)
ax.yaxis.set_inverted(True)
那一片橙海, 2024-08-24 14:24:07

或者,您可以使用 matplotlib.pyplot.axis()函数,它允许您反转任何绘图轴

ax = matplotlib.pyplot.axis()
matplotlib.pyplot.axis((ax[0],ax[1],ax[3],ax[2]))

或者如果您只想反转 X 轴,那么

matplotlib.pyplot.axis((ax[1],ax[0],ax[2],ax[3]))

实际上,您可以反转两个轴:

matplotlib.pyplot.axis((ax[1],ax[0],ax[3],ax[2]))

Alternatively, you can use the matplotlib.pyplot.axis() function, which allows you inverting any of the plot axis

ax = matplotlib.pyplot.axis()
matplotlib.pyplot.axis((ax[0],ax[1],ax[3],ax[2]))

Or if you prefer to only reverse the X-axis, then

matplotlib.pyplot.axis((ax[1],ax[0],ax[2],ax[3]))

Indeed, you can invert both axis:

matplotlib.pyplot.axis((ax[1],ax[0],ax[3],ax[2]))
旧故 2024-08-24 14:24:07

如果您在子图中执行此操作,这是我发现的解决方案

fig, ax = plt.subplots(1,2, sharex = True)
for i in range(2):
    ax[i].plot(xdata, ydata)
axs = plt.gca()
axs.invert_xaxis()

虽然使用 sharex,最后两行需要位于 for 循环之外

if you are doing this in a subplot, here is the solution i found

fig, ax = plt.subplots(1,2, sharex = True)
for i in range(2):
    ax[i].plot(xdata, ydata)
axs = plt.gca()
axs.invert_xaxis()

While using sharex, last two lines needs to be outside the for loop

长发绾君心 2024-08-24 14:24:07

如果使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文