本文介紹了Matplotlib 在刻度標簽(字符串)中顯示美元符號的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我不知道如何在不是數字而是字符串的刻度標簽中顯示美元符號.
這是我的意思的一個例子:
import matplotlib.pyplot as plt將 numpy 導入為 np類別 = ['$0-$10','$10-$20','$20-$30']y_pos = np.arange(len(categories))數據 = 3 + 10 * np.random.rand(len(categories))plt.barh(y_pos, 數據, align='center', alpha=0.4)plt.yticks(y_pos, 類別)plt.show()
產量:
我試過這個,它可以用數千美元:
fmt = '${x:,.0f}'滴答聲 = mtick.StrMethodFormatter(fmt)plt.yaxis.set_major_formatter(勾選)
...但是像我這里的字符串沒有運氣.
解決方案
用反斜杠轉義美元符號,以便 Matplotlib 不會將它們解釋為表示
I can't figure out how to display dollar signs in tick labels that are not numbers, but strings.
Here's an example of what I mean:
import matplotlib.pyplot as plt
import numpy as np
categories = ['$0-$10','$10-$20','$20-$30']
y_pos = np.arange(len(categories))
data = 3 + 10 * np.random.rand(len(categories))
plt.barh(y_pos, data, align='center', alpha=0.4)
plt.yticks(y_pos, categories)
plt.show()
Yields:
I tried this, which works with thousands of dollars:
fmt = '${x:,.0f}'
tick = mtick.StrMethodFormatter(fmt)
plt.yaxis.set_major_formatter(tick)
...but no luck with strings like I have here.
解決方案
Escape the dollar signs with a backslash so that Matplotlib does not interpret them as indicating the beginning (or ending) of LaTeX math mode:
import matplotlib.pyplot as plt
import numpy as np
categories = ['$0-$10','$10-$20','$20-$30']
y_pos = np.arange(len(categories))
data = 3 + 10 * np.random.rand(len(categories))
plt.barh(y_pos, data, align='center', alpha=0.4)
plt.yticks(y_pos, categories)
plt.show()
這篇關于Matplotlib 在刻度標簽(字符串)中顯示美元符號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!