使用误差条图旋转 matplotlib 标记

我正在遵循这个答案来旋转预先存在的 matplotlib 标记。

但是,那里的解决方案不适用于误差条图。这是一个简单的example.py

#!/usr/bin/env python3


import matplotlib.pyplot as plt

from matplotlib.markers import MarkerStyle


# I want to use this marker in an errorbar plot

m = MarkerStyle("D")

m._transform.scale(1.0, 0.6)


# I can use it in a scatter plot

fig, ax = plt.subplots()

plt.scatter([1,2,3],[1,2,3], s=225, marker="d")

plt.scatter([1,2,3],[2,3,4], s=225, marker=m, color="crimson")

plt.show()

# which produces a figure similar to https://stackoverflow.com/a/49662571/


# But when I use it in errorbar

fix, ax = plt.subplots()

plt.errorbar([1,2,3],[3,4,5], yerr=[0.5,0.5,0.5], marker=m, color='green')

plt.show()


# I get

#>Traceback (most recent call last):

#>  File "./example.py", line 19, in <module>

#>    plt.errorbar([1,2,3],[3,4,5], yerr=[0.5,0.5,0.5], marker=m, color='green')

#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2524, in errorbar

#>    return gca().errorbar(

#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/__init__.py", line 1565, in inner

#>    return func(ax, *map(sanitize_sequence, args), **kwargs)

#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 3265, in errorbar

#>    data_line = mlines.Line2D(x, y, **plot_line_style)

#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/lines.py", line 378, in __init__

#>    self._marker = MarkerStyle(marker, fillstyle)

#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/markers.py", line 225, in __init__

#>    self.set_marker(marker)

#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/markers.py", line 289, in set_marker


问:如何在误差条图中使用旋转的预先存在的 matplotlib 标记(或者可能是任何 MarkerStyle 标记)? (我真的想要 90° 旋转thin_diamond。)


慕慕森
浏览 59回答 2
2回答

慕码人8056858

有趣的是,我不知道为什么要这样设计。解决方法是绘制单独的散点图:fix, ax = plt.subplots()plt.errorbar([1,2,3],[3,4,5], yerr=[0.5,0.5,0.5], marker=None, color='green')plt.scatter([1,2,3],[3,4,5], s=225, marker=m, color="green")plt.show()输出:

精慕HU

事实证明这是两个错误。PR #16692中修复了一个问题,该问题已合并到 matplotlib 3.3.0 中(我使用的是 3.2.2)。 另一个我必须修复,将在 3.4.0 中修复。如果您现在需要此功能,您可能需要matplotlib/markers.py根据此 diff来修补您的功能。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python