旋转带有错误栏图的matplotlib标记 [英] Rotate matplotlib markers with errorbar plot

查看:96
本文介绍了旋转带有错误栏图的matplotlib标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪此答案,以旋转预先存在的matplotlib标记.

I was following this answer for rotating pre-existing matplotlib markers.

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

However, the solution there doesn't work with errorbar plots. Here's a simple 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
#>    Path(marker)
#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/path.py", line 127, in __init__
#>    vertices = _to_unmasked_float_array(vertices)
#>  File "/usr/local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 1317, in _to_unmasked_float_array
#>    return np.asarray(x, float)
#>  File "/usr/local/lib/python3.8/site-packages/numpy/core/_asarray.py", line 83, in asarray
#>    return array(a, dtype, copy=False, order=order)
#>TypeError: float() argument must be a string or a number, not 'MarkerStyle'

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

推荐答案

原来是两个错误.一个已在 PR#16692 已合并到matplotlib 3.3.0中(我当时是使用3.2.2).其他必须修复 并将在 3.4.0 中.如果您现在需要此功能,则可能要根据

This turned out to be two bugs. One was fixed in PR #16692 was was incorporated into matplotlib 3.3.0 (I was using 3.2.2). The other I had to fix and will be in 3.4.0. If you need this functionality now you may want to patch your matplotlib/markers.py to according to this diff.

这篇关于旋转带有错误栏图的matplotlib标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆