如何旋转一个简单的matplotlib Axes

2024-04-28 17:47:55 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以像旋转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()???

文本实例上的简单set_旋转将围绕其坐标轴按角度值旋转文本。对于axes实例,有什么方法可以这样做吗?


Tags: and实例instancetext文本addmatplotlibsome
2条回答

是的,这是可能的。但是你必须分别旋转每个标签。因此,可以尝试使用迭代:

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()

你在问如何旋转整个轴(而不仅仅是文本)?

如果是,是的,这是可能的,但你必须事先知道情节的范围。

您将不得不使用axisartist,它允许像这样更复杂的关系,但更复杂一些,不用于交互式可视化。如果你想变焦等,就会遇到麻烦。

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()

enter image description here

相关问题 更多 >