簡體   English   中英

Jupyter Notebook 中的 Python Matplotlib get_xticklabels - 為什么它在與繪圖代碼相同的單元格中返回一個空白列表?

[英]Python Matplotlib get_xticklabels in Jupyter Notebook - why does it return a blank list in same cell as plot code?

問題摘要:我正在開發一個 Jupyter 筆記本作為使用新包的模板。 對於數據可視化,我嘗試使用日期字符串和實驗名稱作為 x 軸刻度標簽進行繪圖。 有時,這些圖會導致 x 軸擁擠。 我正在嘗試編寫一個函數,允許用戶根據輸入步長大小每隔一個標簽(和刻度線)、第三個標簽等。 簡單的函數如下所示:

def run_number_labels(axes, label_list, stepsize):
    '''
    This function changes a plot to plot on run numbers. The stepsize determines 
    how many labels to display, use larger numbers for more concentrated data sets
    (1 is every 1, 2 is every 2, and 5 is every 5 labels, for instance). Care should
    be taken to ensure that the length of the label_list is the same as the amount
    of x-axis ticks. This can be verified by running print(axes.get_xticks()) in 
    your notebook. 
    '''
    axes.set_xticks(axes.get_xticks()[::stepsize])
    b = axes.set_xticklabels(labels=label_list[::stepsize], rotation=55, ha='right')

這在大多數情況下都有效,但是我注意到 Jupyter 筆記本中的一個錯誤 (?) 阻礙了我的測試。

到目前為止,我不能簡單地獲取 x 軸刻度標簽並將它們打印在單元格中!

我試過的:我提供下面的測試代碼:

x = np.linspace(0,10*math.pi,50)
print(x.shape)
y1 = []
y2 = []
for ind, val in enumerate(x):
    y1.append(math.sin(val))
    y2.append(math.cos(val+math.pi))

print(np.array(y1).shape)
fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True)
ax[0].plot(x, np.array(y1), linestyle='-', color='lightgreen')
ax[1].plot(x, np.array(y2), linestyle='-', color='rosybrown')

#Now get the list of xlables
label_list = ax[1].get_xticklabels()
print(label_list)

這會在具有共享 x 軸的 matplotlib 子圖數組中生成一個簡單的圖。 它嘗試從第二個或底部 x 軸命名標簽字符串列表label_list 情節有效,但未生成列表: 具有兩行一列的子圖,共享 x 軸范圍為 0-30,步長為 5。

返回的x軸標簽列表如下: [Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')]

換句話說,它是空白的。

如果我用同一行代碼在下一個單元格中生成列表,我會得到: [Text(-5.0, 0, '−5'), Text(0.0, 0, '5'), Text(5.0, 0, '15'), Text(10.0, 0, '25'), Text(15.0, 0, '35'), Text(20.0, 0, ''), Text(25.0, 0, ''), Text(30.0, 0, ''), Text(35.0, 0, '')]

但是,在下一個單元格中,我似乎無法使用相同的軸將圖形可視化。 我已經讀過 plt.draw() 函數應該在 Jupyter 筆記本中啟用此容量,但是由於未指定的渲染器,此命令返回錯誤。

是否有任何變通方法可以讓我在 Jupyter 筆記本的同一單元格中查看 x 軸標簽列表?

編輯Coup 的答案,建議在請求軸標簽列表之前使用plt.show()來獲取列表。 但是,我無法操作軸(例如,使用上面的 run_number_labels 函數並將標簽列表傳遞給它)。 有沒有辦法在 plt.show() 之后重新操作軸? 如果沒有,我將需要與 Coup 的答案不同的方法來解決這個問題。

謝謝你。

在定義 label_list 之前添加 plt.show() 有效。

在此處輸入圖片說明

x = np.linspace(0,10*math.pi,50)

y1 = []
y2 = []
for ind, val in enumerate(x):
    y1.append(math.sin(val))
    y2.append(math.cos(val+math.pi))

fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True)
ax[0].plot(x, np.array(y1), linestyle='-', color='lightgreen')
ax[1].plot(x, np.array(y2), linestyle='-', color='rosybrown')

plt.show()

#Now get the list of xlables
label_list = ax[1].xaxis.get_majorticklabels()
print(label_list)

如果您有最新的 matplotlib,您可以執行 fig.draw_no_output(),它是 fig.canvas.draw() 的包裝器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM