2016-03-01 171 views
4

我想知道我怎麼可以明確地設定一個彩條對象的格式matplotlibmatplotlib彩條刻度標籤格式

下面是一個例子繪製腳本:

from matplotlib import pyplot 
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 
from matplotlib.colors import BoundaryNorm 
from matplotlib.ticker import MaxNLocator 
from pylab import * 
import numpy as np 
import random 

# ---------- 

plot_aspect = 1.2 
plot_height = 10.0 
plot_width = int(plot_height*plot_aspect) 

# ---------- 

pyplot.figure(figsize=(plot_width, plot_height), dpi=100) 
pyplot.subplots_adjust(left=0.10, right=1.00, top=0.90, bottom=0.06, hspace=0.30) 
subplot1 = pyplot.subplot(111) 

# ---------- 

cbar_max = 40.0 
cbar_min = 20.0 
cbar_step = 1.0 
cbar_num_colors = 200 
cbar_num_format = "%d" 

# ---------- 
# make random dataset 

dx, dy = 5.0, 5.0 
y, x = np.mgrid[slice(-100.0, 100.0 + dy, dy),slice(-100.0, 100.0 + dx, dx)] 

z = [] 
for i in x: 
    z.append([]) 
    for j in y: 
     z[-1].append(random.uniform(cbar_min,cbar_max)) 

# ---------- 
# make random dataset 

levels = MaxNLocator(nbins=cbar_num_colors).tick_values(cbar_min, cbar_max) 
cmap = pyplot.get_cmap('gist_ncar') 
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) 
pp = pyplot.contourf(x,y,z,levels=levels,cmap=cmap) 
cbar = pyplot.colorbar(pp, orientation='vertical', ticks=np.arange(cbar_min, cbar_max+cbar_step, cbar_step), format=cbar_num_format) 
cbar.ax.set_ylabel('Color Scale [unit]', fontsize = 16, weight="bold") 

CS = pyplot.contour(x,y,z, alpha=0.5) 

majorLocator1 = MultipleLocator(10) 
majorFormatter1 = FormatStrFormatter('%d') 
minorLocator1 = MultipleLocator(5) 

subplot1.xaxis.set_major_locator(majorLocator1) 
subplot1.xaxis.set_major_formatter(majorFormatter1) 
subplot1.xaxis.set_minor_locator(minorLocator1) 

pyplot.xticks(fontsize = 16) 
pyplot.xlim(-100.0,100.0) 

majorLocator2 = MultipleLocator(10) 
majorFormatter2 = FormatStrFormatter('%d') 
minorLocator2 = MultipleLocator(5) 

subplot1.yaxis.set_major_locator(majorLocator2) 
subplot1.yaxis.set_major_formatter(majorFormatter2) 
subplot1.yaxis.set_minor_locator(minorLocator2) 

pyplot.yticks(fontsize = 16) 
pyplot.ylim(-100.0,100.0) 

subplot1.xaxis.grid() 
subplot1.yaxis.grid() 
subplot1.axes.set_aspect('equal') 

pyplot.suptitle('Main Title', fontsize = 24, weight="bold") 

pyplot.xlabel('X [unit]', fontsize=16, weight="bold") 
pyplot.ylabel('Y [unit]', fontsize=16, weight="bold") 

pyplot.show() 
pyplot.close() 

這使我的輸出是這樣的:

enter image description here

目前,彩條刻度標記的格式將使用以前提供的格式字符串:cbar_num_format = "%d",但是我想也設置使用字體大小和重量:

cbar.ax.set_yticklabels(np.arange(cbar_min, cbar_max+cbar_step, cbar_step), fontsize=16, weight='bold') 

...但我這樣做的時候,以前應用的格式化字符串似乎消失和數字回到了"%0.1f"格式而不是我在前面所施加的"%d"

enter image description here

我怎樣才能防止這種情況發生或者以更好的方式控制彩條刻度標記?

回答

5

一種選擇是僅手動格式化標記。可能有更好的方法,但這通常適用於我。

cbar.ax.set_yticklabels(['{:.0f}'.format(x) for x in np.arange(cbar_min, cbar_max+cbar_step, cbar_step)], fontsize=16, weight='bold') 

編輯:

如果你不想自己找出蜱你可以使用:

for l in cbar.ax.yaxis.get_ticklabels(): 
    l.set_weight("bold") 
    l.set_fontsize(16) 

您可能需要調用draw(),如果他們不正確更新。這可以減少到一個,內襯:

setp(cbar.ax.yaxis.get_ticklabels(), weight='bold', fontsize=16) 
+0

[ '{:.0f}'。格式(X)爲X的......似乎沒有工作,但[ (「%d」%x)for x in ... does!也許它是一個版本差異? – HotDogCannon

+0

它可能是一個版本差異你使用的是什麼Python版本? – GWW

+0

我使用的版本2.6.6 – HotDogCannon

1

只是將其更改爲

cbar.ax.set_yticklabels(np.arange(int(cbar_min), int(cbar_max+cbar_step), int(cbar_step)), fontsize=16, weight='bold') 

的伎倆!

即只是給int()np.arange()值內cbar.ax.set_yticklabels

enter image description here

+0

確實有效,但我希望它是靈活的,並且格式是輸入。謝謝,雖然! – HotDogCannon