2017-04-21 55 views
2

當我做出pyplot次要情節,然後將光標的位置不正確顯示,因爲你可以在下面的圖片中看到(光標在右上角副區某處(相同的行爲經歷了所有其他的副區),但是當我提出的屏幕截圖,所述光標是不存在(I使用Win10)): enter image description here []之間的值是正確的顏色值,但xy值未示出。這隻有當我使用subplots時纔會發生。
下面是產生了圖片代碼:Pyplot:光標的位置未顯示

def plot_subplots(lam, t): 
    # lam: list of 4 lambda values, t fixed 
    f, ax = plt.subplots(2, 2, sharex='col', sharey='row') 
    ((ax1, ax2), (ax3, ax4)) = ax 
    ax = [ax1, ax2, ax3, ax4] 
    # get X and K (both equidistant arrays) 

    for k, l in enumerate(lam): 
     # get the color array husimi 
     p = ax[k].pcolorfast(X, K, husimi, cmap='jet') 
     ax[k].set_title(r'$\lambda='+str(l)+'$') 

    ax1.set_ylabel(r'$k$') 
    ax3.set_ylabel(r'$k$') 
    ax1.set_yticks([min(K), 0, max(K)]) 
    ax1.set_yticklabels([r'$-\pi$', r'$0$', r'$\pi$']) 
    ax3.set_yticks([min(K), 0, max(K)]) 
    ax3.set_yticklabels([r'$-\pi$', r'$0$', r'$\pi$']) 
    ax3.set_xlabel(r'$x$') 
    ax4.set_xlabel(r'$x$') 
    ax3.set_xticks([min(X), 0, max(X)]) 
    ax4.set_xticks([min(X), 0, max(X)]) 
    ax3.set_xticklabels([r'$'+str(min(X))+'$', r'$0$', r'$'+str(max(X))+'$']) 
    ax4.set_xticklabels([r'$'+str(min(X))+'$', r'$0$', r'$'+str(max(X))+'$']) 
    f.suptitle(r'$t_2='+str(t)+'$') 

我使用Python 3.4.3 64位和Matplotlib 1.5.2,如果這個問題。是否有人發現在產生這種行爲或者這只是plt.pcolorfast一些bug代碼中的錯誤?

回答

3

此無關大選的。這也不是pcolorfast中的錯誤或錯誤。

的原因,沒有號碼顯示的是你手動設置xticklabels。使用ax.set_xticklabels覆蓋軸的格式化程序並創建一個固定的格式化程序。如果您設置了ax.set_xticklabels(["apple", "banana", "cherry"]),問題可能會變得很明顯;蘋果和香蕉之間有哪些價值?

所以這個想法當然不會使用set_xticklabels,因此不使用固定的格式化程序。相反,人們可以使用FuncFormatter與每一個可能的輸入返回一個值的函數,只有確保,即如np.pi被格式化爲π

import matplotlib.pyplot as plt 
import matplotlib.ticker 
import numpy as np; np.random.seed(1) 

x = np.linspace(-np.pi,np.pi) 
X,Y = np.meshgrid(x,x) 
Z = np.random.normal(size=np.array(X.shape)-1) 


fig, ax = plt.subplots() 

pc = ax.pcolorfast(X,Y,Z) 
ax.set_yticks([-np.pi, 0, np.pi]) 

def fmt(x,pos): 
    if np.isclose([np.abs(x)],[np.pi]): 
     if x>0: return r'$\pi$' 
     else: return r'$-\pi$' 
    else: 
     return "%g" % x 
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(fmt)) 

fig.colorbar(pc, ax=fig.axes) 
plt.show() 

enter image description here

+0

謝謝!很高興知道,當我以簡單的方式設置標籤時,我有點摧毀了某些東西。 – Michael