简体   繁体   English

如何在matplotlib.axes.Axes.arrow Python 2.7中使用kwargs

[英]How to use kwargs in matplotlib.axes.Axes.arrow python 2.7

I'm working from arrow_simple_demo.py here which I have already modified to be: 我正在从arrow_simple_demo.py工作, 这里我已经将其修改为:

import matplotlib.pyplot as plt

ax = plt.axes()
ax.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1)

however, I want to change the line style of the arrow to dashed using a kwarg. 但是,我想使用kwarg将箭头的线条样式更改为虚线。 The arrow docs suggest it is possible. 箭头文档表明这是可能的。 arrow docs 箭头文档

So I tried to give arrow the **kwargs argument to import: 所以我试图给箭头** kwargs参数来导入:

kwargs = {linestyle:'--'}

Now my code looks like this: 现在我的代码如下:

import matplotlib.pyplot as plt

ax = plt.axes()
kwargs={linestyle:'--'}
ax.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, **kwargs)

But the result is: 但是结果是:

NameError: name 'linestyle' is not defined

I'm wondering if anyone can tell me if I am using the kwargs correctly, and whether I need to import Patch from matplotlib's patches class to make this work. 我想知道是否有人可以告诉我我是否正确使用了kwargs,以及是否需要从matplotlib的补丁程序类导入Patch才能使其正常工作。 The statement "Other valid kwargs (inherited from :class: Patch )" which is in the arrow docs above the listing of kwargs makes me think it might be necessary. 在kwargs列表上方的箭头文档中,“其他有效kwargs(继承自:class: Patch )”声明使我认为​​这很有必要。 I have also been looking at the patches docs to figure that question out. 我也一直在寻找补丁文档来解决这个问题。 here 这里

EDIT: 编辑:

Code finished when I passed linestyle key as a string, but I am not getting the dashed arrow line that I was hoping for. 当我将linestyle键作为字符串传递时,代码完成了,但是我没有得到想要的虚线箭头。

import matplotlib.pyplot as plt
ax = plt.axes()
kwargs={'linestyle':'--'}
ax.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, **kwargs)

see picture: 看图片:

arrow plot solid line 箭头图实线

The key of your kwargs dictionary should be a string. 您的kwargs词典的关键字应该是字符串。 In you code, python looks for an object called linestyle which does not exist. 在您的代码中,python查找不存在的名为linestyle的对象。

kwargs = {'linestyle':'--'}

unfortunately, doing is not enough to produce the desired effect. 不幸的是,这样做不足以产生期望的效果。 The line is dashed, but the problem is that the arrow is drawn with a closed path and the dashes are overlaid on top of one another, which cancels the effect. 这条线虚线,但是问题在于箭头是用封闭的路径绘制的,并且虚线彼此重叠,从而抵消了效果。 You can see the dashed line by plotting a thicker arrow. 通过绘制较粗的箭头可以看到虚线。

ax = plt.axes()
kwargs={'linestyle':'--', 'lw':2, 'width':0.05}
ax.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, **kwargs)

在此处输入图片说明

If you want a simple dashed arrow, you have to use a simpler arrow, using annotate 如果要使用简单的虚线箭头,则必须使用更简单的箭头,并使用annotate

ax = plt.axes()
kwargs={'linestyle':'--', 'lw':2}
ax.annotate("", xy=(0.5, 0.5), xytext=(0, 0),
    arrowprops=dict(arrowstyle="->", **kwargs))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM