簡體   English   中英

Matplotlib Ticker

[英]Matplotlib Ticker

有人可以給我一個如何使用以下tickFormatters的例子。 這些文檔對我沒有任何意義。

ticker.StrMethodFormatter() ticker.IndexFormatter()

例如,我可能會這么認為

x = np.array([ 316566.962,  294789.545,  490032.382,  681004.044,  753757.024,
            385283.153,  651498.538,  937628.225,  199561.358,  601465.455])
y = np.array([ 208.075,  262.099,  550.066,  633.525,  612.804,  884.785,
            862.219,  349.805,  279.964,  500.612])
money_formatter = tkr.StrMethodFormatter('${:,}')

plt.scatter(x,y)
ax = plt.gca()
fmtr = ticker.StrMethodFormatter('${:,}')
ax.xaxis.set_major_formatter(fmtr)

將我的刻度標簽設置為美元簽名和逗號sep為數千個地方ala

['$300,000', '$400,000', '$500,000', '$600,000', '$700,000', '$800,000', '$900,000']

但我得到一個索引錯誤。

IndexError: tuple index out of range

對於IndexFormatter文檔說:

從標簽列表中設置字符串

我真的不知道這意味着什么,當我嘗試使用它時,我的抽動消失了。

StrMethodFormatter確實通過提供可以使用format方法格式化的字符串來工作。 所以使用'${:,}'是朝着正確的方向發展的。

但是從我們學到的文檔中

用於該值的字段必須標記為x,並且用於該位置的字段必須標記為pos。

這意味着您需要為該字段提供實際標簽x 此外,您可能希望將數字格式指定為g不具有小數點。

fmtr = matplotlib.ticker.StrMethodFormatter('${x:,g}')

在此輸入圖像描述

IndexFormatter在這里IndexFormatter不大。 如您所知,您需要提供標簽列表。 這些標簽用於索引,從0開始。因此,使用此格式化器需要使x軸從零開始並覆蓋整數。

例:

plt.scatter(range(len(y)),y)
fmtr = matplotlib.ticker.IndexFormatter(list("ABCDEFGHIJ"))
ax.xaxis.set_major_formatter(fmtr)

在此輸入圖像描述

這里,刻度線位於(0,2,4,6,....) ,列表中的相應字母(A, C, E, G, I, ...)用作標簽。

暫無
暫無

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

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