Notebook
これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。
HTML ファイル生成日時: 2024/05/08 09:31:52.302 (台灣標準時)
Matplotlib で作る図の縦横比
Matplotlib で図を作る際、図の縦横比を自分で指定したい場合があるでござる。
まずは、特に何も指定せずに以下のように作図してみるでござる。
#!/usr/pkg/bin/python3.9
#
# Time-stamp: <2022/05/18 09:05:44 (CST) daisuke>
#
# importing numpy module
import numpy
# importing matplotlib module
import matplotlib.figure
import matplotlib.backends.backend_agg
# output file
file_output = 'test_00.png'
# data
data_x = numpy.linspace (0.0, 20.0, 1000)
data_y = 0.5 * data_x
# making objects "fig" and "ax"
fig = matplotlib.figure.Figure ()
matplotlib.backends.backend_agg.FigureCanvasAgg (fig)
ax = fig.add_subplot (111)
# axes
ax.set_xlabel ("X")
ax.set_ylabel ("Y")
ax.set_xlim (0.0, 20.0)
ax.set_ylim (0.0, 10.0)
# plotting data
ax.plot (data_x, data_y, '-', label=r'$y=0.5x$')
# saving file
fig.savefig (file_output, dpi=225)
上のスクリプトを実行すると、以下のような図ができるでござる。 4:3 の縦
横比の横長の長方形の領域全体を使って図が作られるでござる。
次に、 set_aspect を使ってみるでござる。以下のようなスクリプトを用意し
てみるでござる。
#!/usr/pkg/bin/python3.9
#
# Time-stamp: <2022/05/18 09:06:51 (CST) daisuke>
#
# importing numpy module
import numpy
# importing matplotlib module
import matplotlib.figure
import matplotlib.backends.backend_agg
# output file
file_output = 'test_01.png'
# data
data_x = numpy.linspace (0.0, 20.0, 1000)
data_y = 0.5 * data_x
# making objects "fig" and "ax"
fig = matplotlib.figure.Figure ()
matplotlib.backends.backend_agg.FigureCanvasAgg (fig)
ax = fig.add_subplot (111)
# axes
ax.set_xlabel ("X")
ax.set_ylabel ("Y")
ax.set_xlim (0.0, 20.0)
ax.set_ylim (0.0, 10.0)
ax.set_aspect ('equal')
# plotting data
ax.plot (data_x, data_y, '-', label=r'$y=0.5x$')
# saving file
fig.savefig (file_output, dpi=225)
上のスクリプトを実行してみると、以下のような図ができるでござる。
set_aspect で equal を指定すると、 X 軸と Y 軸のスケールが同じになるで
ござる。つまり、 X 軸の 0 から 1 と Y 軸の 0 から 1 が同じ間隔で描かれ
るでござる。
スケールを同じにしたいのではなく、正方形の図を作りたいという場合もある
でござる。それには、以下のようなスクリプトにすればよいようでござる。
#!/usr/pkg/bin/python3.9
#
# Time-stamp: <2022/05/18 09:07:40 (CST) daisuke>
#
# importing numpy module
import numpy
# importing matplotlib module
import matplotlib.figure
import matplotlib.backends.backend_agg
# output file
file_output = 'test_02.png'
# data
data_x = numpy.linspace (0.0, 20.0, 1000)
data_y = 0.5 * data_x
# making objects "fig" and "ax"
fig = matplotlib.figure.Figure ()
matplotlib.backends.backend_agg.FigureCanvasAgg (fig)
ax = fig.add_subplot (111)
# axes
ax.set_xlabel ("X")
ax.set_ylabel ("Y")
ax.set_xlim (0.0, 20.0)
ax.set_ylim (0.0, 10.0)
ax.set_box_aspect (1)
# plotting data
ax.plot (data_x, data_y, '-', label=r'$y=0.5x$')
# saving file
fig.savefig (file_output, dpi=225)
上のスクリプトを実行すると、以下のような図ができるでござる。 X 軸で表
されている範囲と Y 軸で表されている範囲は異なるのでござるが、図自体が
正方形になっているでござる。
- About this article:
- author: daisuke
- file: 20220518_0.html
- category: Computer___Python
- title: Matplotlib で作る図の縦横比
- mode: public
- last modified: 2022/05/18 22:01:47 (Taiwan Standard Time)
- html generated: 2024/05/08 09:31:52.302 (Taiwan Standard Time)
Frequently accessed files
- Computer___Python/20220518_0.html
- 8327 page views
- title: Matplotlib で作る図の縦横比
- Computer___FreeBSD/20220621_0.html
- 1626 page views
- title: FreeBSD での X.org の設定の仕方
- Food___Taiwan/20220429_0.html
- 1567 page views
- title: 「楊滇風」の滇味辣炒豬
- Computer___Network/20230516_00.html
- 1510 page views
- title: OpenVPN 2.6 を使い VPN Gate に接続するときの注意点
- Computer___Python/20220715_0.html
- 1040 page views
- title: SciPy による最小二乗法
- Computer___NetBSD/20220817_3.html
- 959 page views
- title: JupyterLab のインストール直後に行うべきこと
- Computer___Python/20220410_0.html
- 861 page views
- title: Pint モジュールを使った単位を含む数値の取り扱い
- Computer___Debian/20210223_1.html
- 794 page views
- title: Debian で autofs を使い自動で NFS マウントする方法
- Food___Taiwan/20230526_00.html
- 788 page views
- title: 台南の「娟姐牛肉湯」の牛肉湯
- Food___Taiwan/20210205_5.html
- 768 page views
- title: 「莊記恩典豆漿」での夜食
- Computer___Python/20210124_0.html
- 668 page views
- title: Python での argparse を使ったコマンドライン引数の取り扱い方法
- Computer___Network/20230508_00.html
- 649 page views
- title: git push するときにパスワードの入力を省略する方法
- Computer___NetBSD/20220818_1.html
- 644 page views
- title: Emacs の markdown-mode について
- Food___Taiwan/20210205_1.html
- 623 page views
- title: 「澳門新濠記燒臘店」の蝦仁炒麵
- Computer___NetBSD/20220428_0.html
- 619 page views
- title: Beamer で verbatim 環境を使う方法
- Science___Astronomy/20220503_0.html
- 588 page views
- title: Lane-Emden 方程式を数値的に解く
- Computer___TeX/20230503_00.html
- 539 page views
- title: LaTeX CJK で日本語や中国語を取り扱うための準備について
- Travel___Taiwan/20220809_2.html
- 533 page views
- title: 老鷹溪生態親子步道
- Computer___NetBSD/20210204_0.html
- 522 page views
- title: Raspberry Pi 4 への NetBSD のインストール
- Computer___Network/20230726_00.html
- 522 page views
- title: git の SSL certificate problem の解決方法
- Science___Math/20220420_0.html
- 517 page views
- title: ラプラシアンの三次元極座標表示
- Computer___NetBSD/20230515_00.html
- 483 page views
- title: pkgsrc の fetch phase で問題
- Science___Astronomy/20220420_1.html
- 478 page views
- title: CFITSIO の使い方についての記録
- Computer___NetBSD/20220808_0.html
- 477 page views
- title: 端末エミュレーターで使うフォントを指定する方法
- Computer___NetBSD/20230119_00.html
- 465 page views
- title: NetBSD でバイナリーパッケージを利用する方法
- Food___Taiwan/20230618_00.html
- 464 page views
- title: 「曼尼食坊」の鮭魚丼飯 (2022 年 12 月下旬)
- Computer___Python/20220816_1.html
- 441 page views
- title: Binder を使って Python スクリプトを実行する
- Food___Taiwan/20220424_8.html
- 428 page views
- title: 「壹等品覇王豬腳」の中段飯
- Food___Taiwan/20220424_1.html
- 417 page views
- title: 「奇聞川菜館」の水煮魚片
- Computer___NetBSD/20210204_2.html
- 410 page views
- title: NetBSD でのカーネルの再構築
HTML file generated by Kinoshita Daisuke.