2010-04-01 93 views
3

基本上,當使用matplotlib生成圖時,y軸上的比例將變爲數百萬。如何開啓數字分組(即1000000顯示爲1,000,000)還是打開小數點分隔符?Matplotlib數字分組(小數分隔符)

+0

您可以這樣做:http://tiku.io/questions/1009459 /如何對格式軸數格式到數千上帶有一個逗號式-matplotlib – 2015-02-13 05:41:49

回答

3

我不認爲有一個內置的功能來做到這一點。 (這是我讀完Q後的想法;我只是查了一下,在文檔中找不到)。

無論如何,它很容易推出自己的。 (下面是一個完整的例子 - 也就是說,它將生成一個帶有一個帶有通知的刻度標籤的軸的mpl圖 - 雖然需要創建自定義刻度標籤,但需要五行代碼 - 三個(包括導入語句)用於創建自定義標籤的功能,以及兩行來創建新標籤並將它們放置在指定軸上。)

# first code a function to generate the axis labels you want 
# ie, turn numbers greater than 1000 into commified strings (12549 => 12,549) 

import locale 
locale.setlocale(locale.LC_ALL, 'en_US') 
fnx = lambda x : locale.format("%d", x, grouping=True) 

from matplotlib import pyplot as PLT 
import numpy as NP 

data = NP.random.randint(15000, 85000, 50).reshape(25, 2) 
x, y = data[:,0], data[:,1] 

fig = PLT.figure() 
ax1 = fig.add_subplot(111) 
ax1.plot(x, y, "ro") 
default_xtick = range(20000, 100000, 10000) 

# these two lines are the crux: 
# create the custom tick labels 
new_xtick = map(fnx, default_xtick) 
# set those labels on the axis 
ax1.set_xticklabels(new_xtick) 

PLT.show() 
相關問題