我正在嘗試使用 matplotlib(3.5 版本)在 Python 中可視化具有 (10, 10) 方形的隨機陣列。我還包括 xaxis 和 yaxis 刻度,但 10 的刻度顯示空資料。有誰知道如何整理?
這是我的代碼:
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.pylab as pylab
params = {'legend.fontsize': 'medium',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)
arr = np.random.rand(10, 10)
plt.imshow(arr)
plt.ylim(0, 10)
plt.xlim(0, 10)
plt.xticks(np.arange(0.0, 11.0, 1.0))
plt.yticks(np.arange(0.0, 11.0, 1.0))
plt.show()
這是生成的影像:

uj5u.com熱心網友回復:
正如其他用戶指出的那樣,Python 陣列從“0”開始索引。您可以欺騙刻度以顯示您想要的值:
創建要繪制的資料
import numpy as np from matplotlib import pyplot as plt import matplotlib.pylab as pylab params = {'legend.fontsize': 'medium', 'figure.figsize': (15, 5), 'axes.labelsize': 'x-large', 'axes.titlesize':'x-large', 'xtick.labelsize':'x-large', 'ytick.labelsize':'x-large'} pylab.rcParams.update(params) arr = np.random.rand(10, 10)然后你可以使用
plt.xticks(tickPosition,tickValues)和相同的 yticks。請注意,每個數字都指向影像中每個值的中心,因此您也必須更改 lim 位置:
plt.figure()
plt.imshow(arr)
plt.ylim(-0.5, 9.5) #to show no blank spaces
plt.xlim(-0.5, 9.5) #to show no blank spaces
plt.xticks(np.arange(0, 10),np.arange(1, 11)) #trick the ticks
plt.yticks(np.arange(0, 10),np.arange(1, 11))
這會給你下一個數字

- 您也可以在開始時(左下角)設定值,只需多花點功夫:
plt.xticks(np.arange(-0.5, 9.5),np.arange(1, 11))
plt.yticks(np.arange(-0.5, 9.5),np.arange(1, 11))
這會給你這個結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367150.html
標籤:Python 麻木的 matplotlib 可视化
上一篇:4D混沌系統Lyapunov指數
下一篇:繪制散點圖時如何控制影像大小?
