とらりもんHOME  Index  Search  Changes  Login

とらりもん - python_matplotlib Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

matplotliob.pyplotの使い方メモ

* 複数の図(subplot)の全体についてタイトルをつける:
plt.suptitle
* 複数の図(subplot)どうしの間隔を調整:
plt.subplots_adjust(wspace=0.4, hspace=0.6)
* 新規ウィンドウ(複数の図を別々のウィンドウで出す時) / Display two images in different windows
plt.imshow(im1)
plt.figure()    # <---- This is the key!!!
plt.imshow(im2)
plt.show()

* plt.show()せずにウィンドウをキャッシュから消す:
plt.close()
* RGB画像を出したいときは, データをnp.arrayでastype(uint8)にすること。uint16やfloatでは色が変になる。たとえvmin, vmaxを指定しても。

!drawing a map (plt.imshow)
* change aspect (narrower, for example):
plt.imshow(img, aspect=2) # ... vertical length is 2 times of horizontal.

!drawing a RGB map
plt.imshow(x[:,:,[4,2,1]]/np.max(x[:,:,[4,2,1]]))
* Here, x is np.ndarray with a shape of (lines, pixels, bands).
* The data must be represented by real numbers ranging from 0 to 1. No "int" or "uint", please!
* By setting the third index as [4, 2, 1], the band 4, band 2, and band 1 are assigned to R, G, B, respectively.

!Label
* give title of the picture:
plt.title("title")
* x-axis label:
plt.xlabel("PRVI (dB)")
* y-axis label:
plt.ylabel("NDVI")
* erase labels:
plt.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False)

!Color
* How to describe colors: [[https://pythondatascience.plavox.info/matplotlib/%E8%89%B2%E3%81%AE%E5%90%8D%E5%89%8D]]
* names of colors: [[https://pythondatascience.plavox.info/wp-content/uploads/2016/06/colorpalette.png]]

!Colormap
* [[https://matplotlib.org/tutorials/colors/colormaps.html?highlight=colormap]]

* show the list of color maps:
help(matplotlib.cm)
* Change color table of a map:
plt.imshow(x, cmap=matplotlib.cm.gray) # このgrayのところをカラーテーブル名にする。
* Reverse the color map: Put "_r" at the end of the color map name.
plt.imshow(x, cmap=matplotlib.cm.rainbow_r)

!output
* create a png file:
plt.savefig(title+'_LC.png', bbox_inches="tight")
# ("bbox_inches="tight" is good for removing big empty space surrounding the picture)