我是Python新手(之前是IDL用戶),所以我希望我以一種可以理解的方式提出這個問題。我一直在試圖創建一個帶有x個bin的極座標圖,其中bin中的數據被平均並賦予與該值相關的顏色。這似乎工作正常,同時使用plt.fill命令,我可以定義bin和填充顏色。問題出現在我嘗試製作一個彩條時去解決。我不斷收到狀態爲AttributeError的錯誤:'圖'對象沒有屬性'autoscale_None'爲plt.fill製作的陰影創建顏色條
任何建議將有幫助謝謝。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.pyplot import figure, show, rc, grid
import pylab
r = np.arange(50)/5.
rstep = r[1] - r[0]
theta = np.arange(50)/50.*2.*np.pi
tstep = theta[1] - theta[0]
colorv = np.arange(50)/50.
# force square figure and square axes looks better for polar, IMO
width, height = mpl.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True)
my_cmap = cm.jet
for j in range(len(r)):
rbox = np.array([r[j], r[j], r[j]+ rstep, r[j] + rstep])
for i in range(len(theta)):
thetabox = np.array([theta[i], theta[i] + tstep, theta[i] + tstep, theta[i]])
x = rbox*np.cos(thetabox)
y = rbox*np.sin(thetabox)
plt.fill(x,y, facecolor = my_cmap(colorv[j]))
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)])
cb = plt.colorbar()
plt.show()
*這裏是我的真實數據稍微好一點的例子,有洞失蹤無處不在,所以在這個例子中,我只是做了一個很大的圓的四分之一。當我嘗試齧合時,代碼似乎嘗試插入這些區域。
r = np.arange(50)/50.*7. + 3.
rstep = r[1] - r[0]
theta = np.arange(50)/50.*1.5*np.pi - np.pi
tstep = theta[1] - theta[0]
colorv = np.sin(r/10.*np.pi)
# force square figure and square axes looks better for polar, IMO
width, height = mpl.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True)
my_cmap = cm.jet
for j in range(len(r)):
rbox = np.array([r[j], r[j], r[j]+ rstep, r[j] + rstep])
for i in range(len(theta)):
thetabox = np.array([theta[i], theta[i] + tstep, theta[i] + tstep, theta[i]])
x = rbox*np.cos(thetabox)
y = rbox*np.sin(thetabox)
plt.fill(x,y, facecolor = my_cmap(colorv[j]))
# Add colorbar, make sure to specify tick locations to match desired ticklabels
#cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)])
#cb = plt.colorbar()
plt.show()
然後與參與齧合...
從matplotlib.mlab進口的GridData
r = np.arange(50)/5.
rstep = r[1] - r[0]
theta = np.arange(50)/50.*1.5*np.pi - np.pi
tstep = theta[1] - theta[0]
colorv = np.sin(r/10.*np.pi)
# force square figure and square axes looks better for polar, IMO
width, height = mpl.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True)
my_cmap = cm.jet
x = r*np.cos(theta)
y = r*np.sin(theta)
X,Y = np.meshgrid(x,y)
data = griddata(x,y,colorv,X,Y)
cax = plt.contourf(X,Y, data)
plt.colorbar()
# Add colorbar, make sure to specify tick locations to match desired ticklabels
#cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)])
#cb = plt.colorbar()
plt.show()
在一個側面說明,在'抗鋸齒= TRUE' kwarg一般不會是必要的matplotlib命令(對大部分事情默認值)。由於性能原因,「pcolormesh」默認爲無抗鋸齒,因爲網格「單元」通常是垂直的,並且看起來沒有抗鋸齒。在這種情況下,單元格不是垂直的,並且性能影響不會太差,因此打開抗鋸齒功能打開網格是個好主意。 – 2012-01-18 20:15:10
+1,btw:「有很多方法可以從你的色彩地圖中僞造它」請問,你能給出一些提示/示例嗎?我一直試圖從OP代碼中取出色條,但沒有成功...... – joaquin 2012-01-18 20:30:54
我正在考慮使用代理藝術家,但我認爲可能有更清晰的方法。我會添加一個例子。 – 2012-01-18 20:35:24