【问题标题】:Arrow properties in matplotlib annotatematplotlib 中的箭头属性注释
【发布时间】:2017-06-12 00:23:12
【问题描述】:

我有 Matplotlib 1.5.1 版,我正面临一个有趣的问题。我想在我的绘图中添加一个箭头,它的每一端都有头并指定颜色和宽度。然而,研究Matplotlib documentation on this matter,我意识到我可能不能两者兼得。我可以有一个朝向两端的箭头,但是这个箭头将有默认颜色和线宽 - 如果我在我的 arrowprops 中包含 arrowstyle,这是选项,或者我可以省略 arrowstyle 并设置颜色和宽度箭头属性,但我只有默认箭头。 有没有办法两者兼得?

我有这个代码:

plt.annotate('', xy=(p[0][0]-p[0][2], 0), xycoords='data', xytext=(p[0][0], 0), textcoords='data', arrowprops=dict(arrowstyle: '<|-|>',color='k',lw=2.5))

导致SyntaxError: invalid syntax

(注意:p 只是一个列表列表,我从中获取 x 和 y 值,我正在循环绘制)

【问题讨论】:

    标签: python matplotlib annotate


    【解决方案1】:

    您应该可以使用 arrowprops 来设置颜色、宽度和其他属性。

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.annotate('test', xy=(0.9, 0.9),
                 xycoords='data',
                 xytext=(0, 0),
                 textcoords='data',
                 arrowprops=dict(arrowstyle= '<|-|>',
                                 color='blue',
                                 lw=3.5,
                                 ls='--')
               )
    
    ax.set_xlim(-0.1,1)
    ax.set_ylim(-0.1,1)
    fig.show()
    

    给出这个图:

    这有帮助吗?

    【讨论】:

    • 您使用的是哪个版本的 matplotlib?正如我所说,如果我的arrowprops 中有arrowstyle,那么任何colorlw 和其他line2D 属性都会导致invalid syntax
    • 我使用的是 2.0.0 版。请注意,您发布的代码中存在语法错误 - 当您应该有“arrowstyle=...”时,您有“arrowstyle:...”
    • 问题出在我之前版本的代码中,它有arrowprops={'arrowstyle':'&lt;|-|&gt;'} - 所以事实证明这只是字典不同语法的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多