Matplotlib 隨手記
為了避免每次都要重新查,迅速紀錄一下
Published in
May 20, 2020
後來做研究的時候被迫學會更多的 matplotlib 技巧… 紀錄在個人部落格中!
以下保留原始的紀錄!
取消座標軸刻度 / 坐標軸
import numpy as np
import matplotlib.pyplot as pltimg = np.random.rand(169)
im = plt.imshow(img.reshape(13,13),cmap='Greys_r', interpolation='none', vmin=0, vmax=1, aspect='equal')plt.title("Without ticks ")
ax = plt.gca()# 沒有座標軸的線
plt.axis('off')# 沒有刻度
plt.xticks([])
plt.yticks([])plt.show()
減少儲存時圖片的留白(padding)
要小心padding 太小,有些東西會被切掉
plt.savefig("img.png".format(m),bbox_inches='tight',pad_inches=0.1)
(右) 指定刻度間距,間隔為 1
(右)設定次要座標軸刻度 並指定次要坐標軸線寬度
# Major ticksax.set_xticks(np.arange(0, 13, 1));
ax.set_yticks(np.arange(0, 13, 1));# Labels for major ticksax.set_xticklabels(np.arange(1, 14, 1));
ax.set_yticklabels(np.arange(1, 14, 1));# Minor ticks 次要坐標軸的設定ax.set_xticks(np.arange(-.5, 13, 1), minor=True);
ax.set_yticks(np.arange(-.5, 13, 1), minor=True);# Gridlines based on minor ticksax.grid(which='minor', color='w', linestyle='-', linewidth=2)name = 'set minor ticks'
plt.title(name)
fig.savefig(name+".png" ,bbox_inches='tight',pad_inches=0.1)
# plt.show()
Reference:
值得參考的資料: