如何在matplotlib注释中更改箭头样式? [英] How to change the arrow head style in matplotlib annotate?

查看:93
本文介绍了如何在matplotlib注释中更改箭头样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改注释(matplotlib)中的箭头的头部,但是与其他属性(如shrink)一起使用时,它不起作用.通过查看参数集,似乎可以更改创建的对象的类型.

I want to change the head of an arrow in an annotation (matplotlib), but it doesn't work when used together with other properties, like shrink. It seems to change the type of object created, by looking at the parameters set.

示例

以下代码显示两种类型的注释箭头.

The following code shows two types of annotation arrows.

import matplotlib.pyplot as plt

import numpy as np

xx = np.linspace(0,8)
yy = np.sin(xx)
fig, ax = plt.subplots(1,1, figsize=(8,5))
ax.plot(xx,yy)
ax.set_ylim([-2,2])

ax.annotate( 'local\nmax', xy=(np.pi/2, 1), xytext=(1,1.5), ha='center', \
            arrowprops={'shrink':0.05})
ax.annotate( 'local\nmin', xy=(np.pi*3/2, -1), xytext=(5,0), ha='center', \
            arrowprops={'arrowstyle':'->'})

问题

我试图像这样在第一个注释中设置箭头类型以及其他属性:

I tried to set the arrow type together with the other properties in the first annotation like this:

ax.annotate( 'other\nmax', xy=(np.pi*5/2, 1), xytext=(7,1.5), ha='center', \
            arrowprops={'arrowstyle':'->', 'shrink':0.05}) 

但是,该行会引发错误:

However, that line throws an error:

AttributeError: Unknown property shrink


为什么不起作用?

如何更改注释的箭头样式?

相关问题

我正在使用:

python:3.4.3 + numpy:1.11.0 + matplotlib:1.5.1

python: 3.4.3 + numpy: 1.11.0 + matplotlib: 1.5.1

推荐答案

来自 matplotlib文档,如果字典具有键arrowstyle,则使用给定的字典创建FancyArrowPatch实例并绘制它.否则,创建并绘制YAArrow补丁实例."

From the matplotlib documentation, "if the dictionary has a key arrowstyle, a FancyArrowPatch instance is created with the given dictionary and is drawn. Otherwise, a YAArrow patch instance is created and drawn."

正如您在同一链接中看到的那样,shrinkYAArrow的有效密钥,而不是FancyArrowPatch的有效密钥.

As you can see in the same link, shrink is a valid key for YAArrow but not for FancyArrowPatch.

这篇关于如何在matplotlib注释中更改箭头样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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