2015-07-10 81 views
0

徑向等高線圖,自定義的規模我有一個示例腳本生成matplotlib極等高線圖:在matplotlib

import os 
import math 
import numpy as np 
import matplotlib.pyplot as plt 
import mpl_toolkits.axisartist.floating_axes as floating_axes 
from matplotlib.projections import PolarAxes 
from mpl_toolkits.axisartist.grid_finder import FixedLocator, MaxNLocator, DictFormatter 
import random 

# ------------------------------------ # 

def setup_arc_radial_axes(fig, rect, angle_ticks, radius_ticks, min_rad, max_rad): 

    tr = PolarAxes.PolarTransform() 

    pi = np.pi 

    grid_locator1 = FixedLocator([v for v, s in angle_ticks]) 
    tick_formatter1 = DictFormatter(dict(angle_ticks)) 

    grid_locator2 = FixedLocator([a for a, b in radius_ticks]) 
    tick_formatter2 = DictFormatter(dict(radius_ticks)) 

    grid_helper = floating_axes.GridHelperCurveLinear(tr, 
           extremes=((370.0*(pi/180.0)), (170.0*(pi/180.0)), max_rad, min_rad), 
           grid_locator1=grid_locator1, 
           grid_locator2=grid_locator2, 
           tick_formatter1=tick_formatter1, 
           tick_formatter2=tick_formatter2, 
           ) 

    ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper) 
    fig.add_subplot(ax1) 

    ax1.grid(True) 

    # create a parasite axes whose transData in RA, cz 
    aux_ax = ax1.get_aux_axes(tr) 

    aux_ax.patch = ax1.patch 
    ax1.patch.zorder=0.9 

    #ax1.axis["left"].set_ticklabel_direction("+") 

    return ax1, aux_ax 

# ------------------------------------ # 
# write angle values to the plotting array 
angles = [] 
for mic_num in range(38): 
    angle = float(mic_num)*(180.0/36.0)*(math.pi/180.0)+math.pi 
    angles.append(angle) 

# ------------------------------------ # 
### these are merely the ticks that appear on the plot axis 
### these don't actually get plotted 

angle_ticks = range(0,190,10) 
angle_ticks_rads = [a*math.pi/180.0 for a in angle_ticks] 
angle_ticks_rads_plus_offset = [a+math.pi for a in angle_ticks_rads] 
angle_ticks_for_plot = [] 
for i in range(len(angle_ticks)): 
    angle_ticks_for_plot.append((angle_ticks_rads_plus_offset[i],r"$"+str(angle_ticks[i])+"$")) 

# ------------------------------------ # 

scale = 1.0 
aspect = 1.50 
height = 8.0 
fig = plt.figure(1, figsize=(height*aspect*scale, height*scale)) 
fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95, top=0.84) 
fig.subplots_adjust() 

plot_real_min = 30.0 
plot_real_max = 100.0 

plot_fake_min = 0.0 
plot_fake_max = 5000.0 

rad_tick_increment = 500.0 

radius_ticks = [] 
for i in range(int(plot_fake_min),int(plot_fake_max)+int(rad_tick_increment),int(rad_tick_increment)): 
    plot_fake_val = ((i-plot_fake_min)/(plot_fake_max-plot_fake_min))*(plot_real_max-plot_real_min)+plot_real_min 
    radius_ticks.append((plot_fake_val, r"$"+str(i)+"$")) 

ax2, aux_ax2 = setup_arc_radial_axes(fig, 111, angle_ticks_for_plot, radius_ticks, plot_real_min, plot_real_max) 

azimuths = np.radians(np.linspace(0, 180, 91)) 
azimuths_adjusted = [ (x + math.pi) for x in azimuths ] 
zeniths = np.arange(0, 5050, 50) 
zeniths_adjusted = [((x-plot_fake_min)/(plot_fake_max-plot_fake_min))*(plot_real_max-plot_real_min)+plot_real_min for x in zeniths] 

r, theta = np.meshgrid(zeniths_adjusted, azimuths_adjusted) 
values = 90.0+5.0*np.random.random((len(azimuths), len(zeniths))) 

aux_ax2.contourf(theta, r, values) 

cbar = plt.colorbar(aux_ax2.contourf(theta, r, values), orientation='vertical') 
cbar.ax.set_ylabel('Contour Value [Unit]', fontsize = 16) 

plt.suptitle('Plot Title ', fontsize = 24, weight="bold") 
plt.legend(loc=3,prop={'size':20}) 
plt.xlabel('Angle [deg]', fontsize=20, weight="bold") 
plt.ylabel('Frequency [Hz]', fontsize=20, weight="bold") 

# plt.show() 
plt.savefig('test.png', dpi=100) 
plt.close() 

這個腳本會產生一個情節,看起來像:

enter image description here

我的問題是我怎樣才能用一個替代的彩色條形圖進行繪圖?是否可以定義自定義比例?

就像一個藍白色,紅色等級,其中圍繞一箇中心值增量可以很容易地顯示將是最好的,是這樣的:

enter image description here

回答

1

您可以創建自定義比例,但matplotlib已經擁有你想要的。所有你需要做的就是添加一個參數contourf:

aux_ax2.contourf(theta, r, values, cmap = 'bwr')

如果你不喜歡BWR,coolwarm和地震也是藍色到紅色。如果您需要反轉比例,只需將_r添加到顏色映射名稱即可。你可以在這裏找到更多的色彩地圖:http://matplotlib.org/examples/color/colormaps_reference.htmlContour Plot

0

我不能運行你的代碼,但我認爲你能解決你的問題是這樣的:

from matplotlib import pyplot as plt 
import matplotlib as mpl 

f = plt.figure(figsize=(5,10)) 
ax = f.add_axes([0.01, 0.01, 0.4, 0.95]) 

#here we create custom colors 
cmap = mpl.colors.LinearSegmentedColormap.from_list(name='Some Data',colors=['b', 'w','w', 'r']) 

cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, orientation='vertical') 
cb.set_label('Some Data') 
plt.show() 

如果線性的方式是不是你正在尋找這裏是一些其他類型: http://matplotlib.org/api/colors_api.html#module-matplotlib.colors