2013-02-17 195 views
3

我試圖在徑向座標軸上創建一個具有對數刻度的極座標圖,但我不斷收到錯誤。下面是一些示例代碼和錯誤。它似乎在笛卡爾座標中工作正常,有誰知道發生了什麼?如何在matplotlib中使用極座標軸上的對數刻度

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.cm as cm 

bazbins = np.linspace(0, 2*np.pi, 360) 
fbins = np.logspace(np.log10(0.05), np.log10(0.5), 101) 
theta, r = np.meshgrid(bazbins, fbins) 

# Set up plot window 
fig, ax = plt.subplots(figsize=(12,9))#, subplot_kw=dict(projection='polar')) 

# polar 
ax.set_theta_zero_location('N') 
ax.set_theta_direction(-1) 
ax.set_rscale('log') 

# carthesia 
#ax.set_yscale('log') 

# Plot data 
#ax.pcolormesh(theta, r, r) 
plt.gca().invert_yaxis() 
ax.contourf(theta, r, r) 
ax.set_ylim((0.0, 0.5)) 
plt.show() 

異常在Tkinter的回調:

Traceback (most recent call last): 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__ 
    return self.func(*args) 
    File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 236, in resize 
    self.show() 
    File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 239, in draw 
    FigureCanvasAgg.draw(self) 
    File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 421, in draw 
    self.figure.draw(self.renderer) 
    File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 898, in draw 
    func(*args) 
    File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 1997, in draw 
    a.draw(renderer) 
    File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1045, in draw 
    tick.draw(renderer) 
    File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 239, in draw 
    self.label1.draw(renderer) 
    File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 591, in draw 
    ismath=ismath) 
    File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 156, in draw_text 
    return self.draw_mathtext(gc, x, y, s, prop, angle) 
    File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 145, in draw_mathtext 
    x = int(x) + ox 
ValueError: cannot convert float NaN to integer 
+0

嘗試set_ylim後'設置rscale(這基本上是yscale)()' – theta 2013-02-17 10:07:52

+0

奏效,但爲什麼它需要去後? – Dave 2013-02-17 19:26:13

回答

2

這似乎是matplotlib中的一個錯誤。您無需在set_rscale(或set_yscale)之前撥打set_rlim(或set_ylim)方法。另外,您必須撥打set_rlimset_ylim,以0或0.0作爲下限。其他值的下限也會導致崩潰。其他後端也會出現問題(我已經確認了gtkagg和pdf後端的問題)。

我爲這個問題提交了一個錯誤報告,可以找到這個錯誤報告here。如果這個問題影響到你,請到bug報告頁面並留下評論,讓matplotlib開發人員知道這個問題對用戶很重要。

1

當我拿出在建立運行了我的繪圖窗口就行了評論。

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.cm as cm 

bazbins = np.linspace(0, 2*np.pi, 360) 
fbins = np.logspace(np.log10(0.05), np.log10(0.5), 101) 
theta, r = np.meshgrid(bazbins, fbins) 

# Set up plot window 
fig, ax = plt.subplots(figsize=(12,9), subplot_kw=dict(projection='polar')) 

# polar 
ax.set_theta_zero_location('N') 
ax.set_theta_direction(-1) 
ax.set_rscale('log') 

plt.gca().invert_yaxis() 
ax.contourf(theta, r, r) 
ax.set_ylim((0.0, 0.5)) 
plt.show() 
+0

我試過'set_rscale'和'set_ylim',沒有網格線出現。還有一些非常小的座標(r〜0.02)未被繪製。不知道在某個時候是否引入了錯誤? – 2016-10-31 21:06:06

0

rscaleset_ylim()後,像THETA在上述評論的回答。

3

當前的matplotlib和對數極座標圖有更多的問題。

例如,嘗試在極座標圖的matplotlib example的半徑上添加一個較小的值,然後使用set_rlim(0)set_rscale('log')對其進行繪圖(如此處註釋中所述)。所有低於0.1的值得到一些特殊的處理。這會影響對r軸線蜱(注意完全錯位的10E-2和10E-3),以及所繪製的數據:

Examples of polar and log-polar plots

的行爲似乎是無證。我最終手動進行了對數轉換(上面的系列中的第三個繪圖)。對於其他人碰到這個線程來了,這裏是我的代碼:

import numpy as np 
import matplotlib.pyplot as plt 

def scatter_polar_mpl(ax, theta, r): 
    ax.scatter(theta, r) 
    ax.set_rlim(0) 
    ax.set_title('polar matplotlib') 

def scatter_logpolar_mpl(ax, theta, r): 
    ax.scatter(theta, r) 
    ax.set_rlim(0) 
    ax.set_rscale('log') 
    ax.set_title('log-polar matplotlib') 

def scatter_logpolar(ax, theta, r_, bullseye=0.3, **kwargs): 
    min10 = np.log10(np.min(r_)) 
    max10 = np.log10(np.max(r_)) 
    r = np.log10(r_) - min10 + bullseye 
    ax.scatter(theta, r, **kwargs) 
    l = np.arange(np.floor(min10), max10) 
    ax.set_rticks(l - min10 + bullseye) 
    ax.set_yticklabels(["1e%d" % x for x in l]) 
    ax.set_rlim(0, max10 - min10 + bullseye) 
    ax.set_title('log-polar manual') 
    return ax 

r = np.arange(0, 3.0, 0.01) + 0.001 

theta = 2 * np.pi * r 

ax = plt.subplots(1, 3, subplot_kw=dict(polar=True))[1].flatten() 
scatter_polar_mpl(ax[0], theta, r) 
scatter_logpolar_mpl(ax[1], theta, r) 
scatter_logpolar(ax[2], theta, r) 

plt.show() 
+2

嘗試運行此代碼和中間陰謀'scatter_logpolar_mpl'不再打印那麼好的螺旋。它只是把所有的點放在網格的中心。 – 2016-10-31 21:08:10

相關問題