簡體   English   中英

誤差線,但不是直線,作為python matplotlib圖例中的標記符號

[英]errorbar, but not line, as marker symbol in python matplotlib legend

我有一個誤差線圖,每個數據集只有一個數據點(即一個誤差線)。 因此,我也想在圖例中使用單個errorbar符號。 可以通過legend(numpoints=1)來實現。 在以下代碼中使用它:

    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()

    ax.errorbar(x=[0.3], y=[0.7], xerr=[0.2], marker='+', markersize=10, label='horizontal marker line')
    ax.errorbar(x=[0.7], y=[0.3], yerr=[0.2], marker='+', markersize=10, label='is too long')

    ax.set_xlim([0,1])
    ax.set_ylim([0,1])
    ax.legend(numpoints=1) # I want only one symbol

    plt.show()

在圖例中產生以下符號: 圖例中的錯誤欄與線條混合在一起

如您所見,誤差線與水平線混合在一起,當要連接的誤差線不止一個時(使用legend(numpoints=2)或更高),這是有意義的,但在我的情況下看起來很難看。

如何擺脫圖例標記中的線條而又不丟失錯誤欄?

這是由於matplotlib中的默認設置。 在代碼的開頭,您可以通過使用rcParams更改設置來更改它們:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['legend.handlelength'] = 0
mpl.rcParams['legend.markerscale'] = 0

fig, ax = plt.subplots()

ax.errorbar(x=[0.3], y=[0.7], xerr=[0.2], marker='+', markersize=10, label='horizontal marker')
ax.errorbar(x=[0.7], y=[0.3], yerr=[0.2], marker='+', markersize=10, label='is gone')

ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.legend(numpoints=1) 
plt.show()

注意:這將更改將在代碼中繪制的所有圖形的設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM