【发布时间】:2019-02-10 23:40:52
【问题描述】:
我正在尝试创建一个四面板图,其中左下面板包含散点图,其他三个面板包含直方图。左上角将是散点图 x 维度的标准直方图,右下角将是 y 维度旋转 90° 的直方图。这两个在 matplotlib 中都很容易做到。
我遇到了第三个直方图的问题,它是图右上方的 45° 旋转图,给出了 x 和 y 点之间差异的分布。我以前通过在 Illustrator 中手动旋转和重新缩放轴来制作此类图形,但似乎 matplotlib 应该能够生成已经使用子图轴上的转换方法旋转的图形。
我认为类似以下的方法可能会起作用:
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
fig, ax = plt.subplots(nrows=2, ncols=2, squeeze=True, sharex=False,
sharey=False, figsize=(8,8))
ax[0,1].text(0.5,0.5,'I should be rotated',ha='center',va='center')
t = ax[0,1].get_transform()
ax[0,1].set_transform(t.transform(Affine2D().rotate_deg(45)))
plt.show()
在这里,我试图从轴获取变换,对其进行修改,然后将其替换回轴对象。但是,此代码无效。任何帮助将不胜感激。
根据ImportanceOfBeingErnest in cmets 的建议编辑:
我看过 Floating Axes 演示,现在有了这个:
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import matplotlib.pyplot as plt
def setup_axes(fig, rect, rotation, axisScale):
tr = Affine2D().scale(axisScale[0], axisScale[1]).rotate_deg(rotation)
grid_helper = floating_axes.GridHelperCurveLinear(tr, extremes=(-0.5, 3.5, 0, 4))
ax = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
fig.add_subplot(ax)
aux_ax = ax.get_aux_axes(tr)
return ax, aux_ax
fig = plt.figure(1, figsize=(8, 8))
axes = []
axisOrientation = [0, 0, 270, -45]
axisScale = [[1,1],[2,1],[2,1],[2,1]]
axisPosition = [223,221,224,222]
for i in range(0, len(axisOrientation)):
ax, aux_ax = setup_axes(fig, axisPosition[i], axisOrientation[i], axisScale[i])
axes.append(aux_ax)
fig.subplots_adjust(wspace=-0.2, hspace=-0.2, left=0.00, right=0.99, top=0.99, bottom=0.0)
plt.show()
这让我更接近我想要的:
我将尝试在这些轴上添加散点图和直方图。
【问题讨论】:
-
This 可能会有所帮助。
-
看起来就像设置轴的变换一样简单,但不幸的是不是。变换链有点复杂,并且由于独立的 x 和 y 比例不能简单地旋转。在Floating Axes Demo 中展示了一个相对简单的方法,它使用
mpl_toolkits.axisartist辅助函数来实现。 -
你能从 Illustrator 中展示一些想要的输出吗?
-
由于编辑,这看起来不再像一个问题了。您确定“编辑”实际上不应该是答案吗?
标签: python matplotlib plot graph