简体   繁体   English

如何使用pyplot旋转图的x轴和y轴

[英]How can I rotate a plot x axis and y axis using pyplot

import matplotlib.pyplot as plt
import numpy as np

x1 = [0, 0.02, 0.04, 0.08, 0.12, 0.16, 0.2]
y1 = [0.0005, 0.052, 0.0905, 0.1675, 0.2485, 0.3225, 0.4035]

plt.scatter(x1, y1)
plt.title("y-x scatter")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

It works fun as follows: 它的工作原理如下: 在此处输入图片说明

but what my goal is an image like this: 但是我的目标是这样的图像:

在此处输入图片说明

How can I rotate the Image like this using matplotlib? 如何使用matplotlib这样旋转图像?

If you don't care about having negative numbers on the scale you can simply: plt.scatter([-y for y in y1], x1) 如果您不关心刻度上是否为负数,则可以简单地执行以下操作: plt.scatter([-y for y in y1], x1)

You'll also need to change the labels on the axes, but that would plot x on the vertical axis and y on horizontal, negative values to go from right to left. 您还需要更改轴上的标签,但这将在垂直轴上绘制x,在水平负值上绘制y,以便从右到左移动。

If on the other hand you want to rotate the image, you can save it and rotate to file, rotate with another tool and read that file. 另一方面,如果要旋转图像,则可以将其保存并旋转到文件,使用其他工具旋转并读取该文件。

You cannot rotate the toolbar on the plot without completely rewriting it. 如果不完全重写,则无法旋转图上的工具栏。 That seems a bit too much work so I'll leave that out. 这似乎工作量太大,所以我省去了。

Apart from that there is no big problem rotating all the elements in the plot and exchanging the role of x and y axis. 除此之外,旋转图中的所有元素并交换x和y轴的角色没有大问题。 Specifically, 特别,

  • all texts can get a rotation argument. 所有文本都可以获取rotation参数。
  • The data can simply be exchanged, ie scatter(y,x) . 可以简单地交换数据,即scatter(y,x)
  • The axis can be inverted, ax.invert_xaxis() 轴可以反转ax.invert_xaxis()
  • The location of the ticks can be set to the right side ax.yaxis.tick_right() 刻度线的位置可以设置在右侧ax.yaxis.tick_right()
  • The "title" can be emulated with a usual text element. 可以使用普通的文本元素来模拟“标题”。

Compelte code: 强制代码:

import matplotlib.pyplot as plt

x1 = [0, 0.02, 0.04, 0.08, 0.12, 0.16, 0.2]
y1 = [0.0005, 0.052, 0.0905, 0.1675, 0.2485, 0.3225, 0.4035]

fig,ax=plt.subplots(figsize=plt.rcParams["figure.figsize"][::-1])
fig.subplots_adjust(left=0.1, right=0.875, top=0.9,bottom=0.125)

ax.scatter(y1, x1)

ax.set_ylabel("x", rotation=90)
ax.yaxis.tick_right()
ax.yaxis.set_label_position("right")
ax.set_xlabel("y", rotation=180)
ax.invert_xaxis()

plt.setp(ax.get_xticklabels(), rotation=90, va="top", ha="center")
plt.setp(ax.get_yticklabels(), rotation=90, va="center", ha="left")

ax.text(-0.05,0.5,"y-x scatter", ha="center", va="center",
        transform=ax.transAxes, rotation=90)


plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在pyplot中为每个x值在y轴上绘制平均值 - How to plot average in y axis for each x value in pyplot 如何通过使用pyplot降低y轴的值来对散点图plot进行排序? - How to sort scatter plot by decreasing values of y axis using pyplot? 如何用pyplot在相同的x轴(日期时间)但不同的y轴上绘制线形图和条形图? - How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot? 如何在 yellowbrick plot 中标记 X 轴和 Y 轴? 我使用的代码如下所示 - How can I labeled the X axis and Y axis in yellowbrick plot? The code is I used is show below Dash/Plotly:如何使用相同的 x 轴在 y 轴上绘制两列? - Dash/Plotly: How can I plot two columns on the y-axis with the same x axis? 如何使plot的x轴原点和y轴原点在matplotlib中重叠? - How can I make the origin of the x-axis and the origin of the y-axis of a plot overlap in matplotlib? 如何在其中一个图上具有一个共享的X轴和多个Y轴的堆叠图? - How can I have a stacked plot with a shared X axis and multiple Y axis on one of the plots? 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python? 如何使用 plot 在 x 轴上循环变量 i 和在 y 轴上循环中的局部变量使用 matplotlib - How to plot loop variable i on x-axis and local variable in the loop on y-axis using matplotlib 如何绘制2d平面,x轴范围从0到1,y轴范围从0到1,z轴0 - how can I plot a 2d plane x axis range from 0 to 1, y axis range from 0 to 1, and z axis 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM