简体   繁体   English

如何旋转简单的matplotlib轴

[英]How to rotate a simple matplotlib Axes

is it possible to rotate matplotlib.axes.Axes as it is for matplotlib.text.Text 是否可以像旋转matplotlib.text.Text一样旋转matplotlib.axes.Axes

# text and Axes instance
t = figure.text(0.5,0.5,"some text")
a = figure.add_axes([0.1,0.1,0.8,0.8])
# rotation
t.set_rotation(angle)
a.set_rotation()???

a simple set_rotation on a text instance will rotate the text by the angle value about its coordinates axes. 在文本实例上使用简单的set_rotation会将文本绕其坐标轴旋转角度值。 Is there any way to do to same for the axes instance ? 有没有办法对轴实例执行相同的操作?

Are you asking how to rotate the entire axes (and not just the text)? 您是否在问如何旋转整个轴(而不仅仅是文本)?

If so, yes, it's possible, but you have to know the extents of the plot beforehand. 如果是这样,是的,这是可能的,但是您必须事先知道绘图的范围。

You'll have to use axisartist , which allows more complex relationships like this, but is a bit more complex and not meant for interactive visualization. 您将不得不使用axisartist ,它允许像这样的更复杂的关系,但是稍微复杂一点,并不意味着交互式可视化。 If you try to zoom, etc, you'll run into problems. 如果尝试缩放等,则会遇到问题。

import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes

fig = plt.figure()

plot_extents = 0, 10, 0, 10
transform = Affine2D().rotate_deg(45)
helper = floating_axes.GridHelperCurveLinear(transform, plot_extents)
ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=helper)

fig.add_subplot(ax)
plt.show()

在此处输入图片说明

Yes, it is possible. 对的,这是可能的。 But you have to rotate each label separately. 但是您必须分别旋转每个标签。 Therefore, you can try using an iteration: 因此,您可以尝试使用迭代:

from matplotlib import pyplot as plt
figure = plt.figure()
ax = figure.add_subplot(111)
t = figure.text(0.5,0.5,"some text")
t.set_rotation(90)
labels = ax.get_xticklabels()
for label in labels:
    label.set_rotation(45)
plt.show()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM