【问题标题】:transform entire axes (or scatter plot) in matplotlib在 matplotlib 中转换整个轴(或散点图)
【发布时间】:2017-10-09 03:09:30
【问题描述】:

我正在使用以下代码绘制一些数据的均值和方差的变化

import matplotlib.pyplot as pyplot
import numpy

vis_mv(data, ax = None):
    if ax is None: ax = pyplot.gca()
    cmap = pyplot.get_cmap()
    colors = cmap(numpy.linspace(0, 1, len(data)))

    xs = numpy.arange(len(data)) + 1
    means = numpy.array([ numpy.mean(x) for x in data ])
    varis = numpy.array([ numpy.var(x) for x in data ])
    vlim = max(1, numpy.amax(varis))

    # variance
    ax.imshow([[0.,1.],[0.,1.]],
        cmap = cmap, interpolation = 'bicubic',
        extent = (1, len(data), -vlim, vlim), aspect = 'auto'
    )
    ax.fill_between(xs, -vlim, -varis, color = 'white')
    ax.fill_between(xs, varis, vlim, color = 'white')

    # mean
    ax.plot(xs, means, color = 'white', zorder = 1)
    ax.scatter(xs, means, color = colors, edgecolor = 'white', zorder = 2)

    return ax

这工作得很好: 但现在我希望能够以垂直方式使用这种可视化作为某种高级彩条,类似于另一个情节旁边的东西。我希望可以旋转整个轴及其所有内容, 但我只能找到this question,它也没有一个可靠的答案。因此,我尝试自己做如下:

from matplotlib.transforms import Affine2D

ax = vis_mv()
r = Affine2D().rotate_deg(90) + ax.transData

for x in ax.images + ax.lines + ax.collections:
    x.set_transform(r)

old = ax.axis()
ax.axis(old[2:4] + old[0:2])

几乎可以解决问题(请注意,过去沿着白线分布的散点如何被炸毁且未按预期旋转)。 不幸的是,持有scattering 结果的PathCollection 没有按预期运行。尝试了一些东西后,我发现scatter有某种offset transform,这似乎相当于其他集合中的regular transform

x = numpy.arange(5)
ax = pyplot.gca()
p0, = ax.plot(x)
p1 = ax.scatter(x,x)

ax.transData == p0.get_transform()           # True
ax.transData == p1.get_offset_transform()    # True

似乎我可能想更改散点图的偏移变换,但我没有找到任何方法可以让我在PathCollection 上更改该变换。此外,做我真正想做的事情会变得更加不方便。

有人知道是否有可能改变偏移变换吗?

提前致谢

【问题讨论】:

  • 你能发布一张你所拥有的和(大约)你想要的样本图片吗?
  • @VBB 现在清楚了吗?
  • 一开始就垂直绘制所有内容怎么样? imshowscatterplot 将按原样工作,您可以使用 ax.fill_betweenx 进行着色。 (PS:阴影的好技巧)
  • @VBB 我最初是垂直做所有事情的(除了我不知道ax.fill_betweenx),但为了更仔细地观察,我将我的代码重写为水平的,希望我能做到能够旋转整个东西。我显然可以有两个功能,但我希望有一个更优雅的解决方案......
  • @ImportanceOfBeingErnest 我将我的问题更新为 1)提及另一个问题的存在 2)关注散点图

标签: python matplotlib transform


【解决方案1】:

不幸的是PathCollection 没有.set_offset_transform() 方法,但可以访问私有_transOffset 属性并将旋转变换设置为它。

import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
from matplotlib.collections import PathCollection
import numpy as np; np.random.seed(3)

def vis_mv(data, ax = None):
    if ax is None: ax = plt.gca()
    cmap = plt.get_cmap()
    colors = cmap(np.linspace(0, 1, len(data)))

    xs = np.arange(len(data)) + 1
    means = np.array([ np.mean(x) for x in data ])
    varis = np.array([ np.var(x) for x in data ])
    vlim = max(1, np.amax(varis))

    # variance
    ax.imshow([[0.,1.],[0.,1.]],
        cmap = cmap, interpolation = 'bicubic',
        extent = (1, len(data), -vlim, vlim), aspect = 'auto'  )
    ax.fill_between(xs, -vlim, -varis, color = 'white')
    ax.fill_between(xs, varis, vlim, color = 'white')

    # mean
    ax.plot(xs, means, color = 'white', zorder = 1)
    ax.scatter(xs, means, color = colors, edgecolor = 'white', zorder = 2)

    return ax

data = np.random.normal(size=(9, 9))
ax  = vis_mv(data)


r = Affine2D().rotate_deg(90)

for x in ax.images + ax.lines + ax.collections:
    trans = x.get_transform()
    x.set_transform(r+trans)
    if isinstance(x, PathCollection):
        transoff = x.get_offset_transform()
        x._transOffset = r+transoff

old = ax.axis()
ax.axis(old[2:4] + old[0:2])


plt.show()

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2014-09-18
    • 2017-08-24
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多