Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2024/09/03 20:25:59.812 (台灣標準時)

Matplotlib の pyplot interface と object-oriented interface

Matplotlib を使うと、高品質な図 を作ることができるでござる。プログラムを書いて作図をするので、 PGPLOT を思い 出させるでござる。 Matplotlib はとてもよいのでござるが、使い方に、関数 を使う procedural な pyplot interface と、メソッドを使う object-oriented interface の二つがあり、解説によって、 pyplot interface を使っていたり、 object-oriented interface を使っていたりす るので、最初のうちは混乱する場合があるでござる。 Python のモジュールに よっては、 pyplot interface でないと使えないようになっていたりするもの もあるでござる。

Matplotlib のウェブページには、以下のように記述されているでござる。

Matplotlib's documentation and examples use both the OO and the pyplot
styles. In general, we suggest using the OO style, particularly for
complicated plots, and functions and scripts that are intended to be
reused as part of a larger project. However, the pyplot style can be
very convenient for quick interactive work.
複雑な図を作ったり、或いは、コードを再利用しようと思っている場合には object-oriented style がよく、対話的にお試しで作図してみようというよう な場合には pyplot style がよいとのことでござる。

pyplot interface による作図

pyplot interface を使った作図の例は以下の通りでござる。


#!/usr/pkg/bin/python3.9

#
# Time-stamp: <2022/08/16 16:37:18 (CST) daisuke>
#

# importing numpy module
import numpy

# importing matplotlib module
import matplotlib.pyplot

# making numpy arrays
data_x = numpy.linspace (0.0, 30.0, 3001)
data_y = numpy.sin (data_x)

# making a plot using procedural pyplot interface
matplotlib.pyplot.figure ()
matplotlib.pyplot.plot (data_x, data_y, label="sine curve")
matplotlib.pyplot.xlim (0.0, 30.0)
matplotlib.pyplot.ylim (-1.3, +1.3)
matplotlib.pyplot.xlabel ("X-axis [arbitrary unit]")
matplotlib.pyplot.ylabel ("Y-axis [arbitrary unit]")
matplotlib.pyplot.legend ()
matplotlib.pyplot.grid ()

# producing a plot as an output file
matplotlib.pyplot.savefig ("test_pyplot_00.png")

fig_202208/test_pyplot_00.png

単純な図の場合なら、

matplotlib.pyplot.figure ()
がなくても 作図できるようでござる。

#!/usr/pkg/bin/python3.9

#
# Time-stamp: <2022/08/16 16:37:27 (CST) daisuke>
#

# importing numpy module
import numpy

# importing matplotlib module
import matplotlib.pyplot

# making numpy arrays
data_x = numpy.linspace (0.0, 30.0, 3001)
data_y = numpy.sin (data_x)

# making a plot using procedural pyplot interface
#matplotlib.pyplot.figure ()
matplotlib.pyplot.plot (data_x, data_y, label="sine curve")
matplotlib.pyplot.xlim (0.0, 30.0)
matplotlib.pyplot.ylim (-1.3, +1.3)
matplotlib.pyplot.xlabel ("X-axis [arbitrary unit]")
matplotlib.pyplot.ylabel ("Y-axis [arbitrary unit]")
matplotlib.pyplot.legend ()
matplotlib.pyplot.grid ()

# producing a plot as an output file
matplotlib.pyplot.savefig ("test_pyplot_01.png")

fig_202208/test_pyplot_01.png

object-oriented interface による作図

次に、 object-oriented interface を使った作図の例でござる。 fig object を作るところは、 figure () 関数で行い、その後の部分をメソッドを使って 行っているでござる。


#!/usr/pkg/bin/python3.9

#
# Time-stamp: <2022/08/16 16:37:36 (CST) daisuke>
#

# importing numpy module
import numpy

# importing matplotlib module
import matplotlib.pyplot

# making numpy arrays
data_x = numpy.linspace (0.0, 30.0, 3001)
data_y = numpy.sin (data_x)

# making a fig object using matplot.pyplot.figure function
fig = matplotlib.pyplot.figure ()

# making an axes object using object-oriented interface
ax = fig.add_subplot (111)

# making a plot using object-oriented interface
ax.plot (data_x, data_y, label="sine curve")
ax.set_xlim (0.0, 30.0)
ax.set_ylim (-1.3, +1.3)
ax.set_xlabel ("X-axis [arbitrary unit]")
ax.set_ylabel ("Y-axis [arbitrary unit]")
ax.legend ()
ax.grid ()

# producing a plot as an output file
fig.savefig ("test_oo_00.png")

fig_202208/test_oo_00.png

fig オブジェクトと ax オブジェクトを subplots () 関数を使って作り、そ の後、メソッドを使って作図することもできるようでござる。


#!/usr/pkg/bin/python3.9

#
# Time-stamp: <2022/08/16 16:37:45 (CST) daisuke>
#

# importing numpy module
import numpy

# importing matplotlib module
import matplotlib.pyplot

# making numpy arrays
data_x = numpy.linspace (0.0, 30.0, 3001)
data_y = numpy.sin (data_x)

# making a fig and an axes objects using matplot.pyplot.subplots function
fig, ax = matplotlib.pyplot.subplots ()

# making a plot using object-oriented interface
ax.plot (data_x, data_y, label="sine curve")
ax.set_xlim (0.0, 30.0)
ax.set_ylim (-1.3, +1.3)
ax.set_xlabel ("X-axis [arbitrary unit]")
ax.set_ylabel ("Y-axis [arbitrary unit]")
ax.legend ()
ax.grid ()

# producing a plot as an output file
fig.savefig ("test_oo_01.png")

fig_202208/test_oo_01.png

pyplot の関数を一切使わずに、作図することもできるでござる。


#!/usr/pkg/bin/python3.9

#
# Time-stamp: <2022/08/16 16:37:53 (CST) daisuke>
#

# importing numpy module
import numpy

# importing matplotlib module
import matplotlib.backends.backend_agg
import matplotlib.figure

# making numpy arrays
data_x = numpy.linspace (0.0, 30.0, 3001)
data_y = numpy.sin (data_x)

# making a fig object using object-oriented interface
fig = matplotlib.figure.Figure ()

# making a canvas object
canvas = matplotlib.backends.backend_agg.FigureCanvasAgg (fig)

# making an axes object
ax = fig.add_subplot (111)

# making a plot using object-oriented interface
ax.plot (data_x, data_y, label="sine curve")
ax.set_xlim (0.0, 30.0)
ax.set_ylim (-1.3, +1.3)
ax.set_xlabel ("X-axis [arbitrary unit]")
ax.set_ylabel ("Y-axis [arbitrary unit]")
ax.legend ()
ax.grid ()

# producing a plot as an output file
fig.savefig ("test_pureoo_00.png")

fig_202208/test_pureoo_00.png

参考文献



Frequently accessed files

  1. Computer___Python/20220518_0.html
  2. Computer___Network/20230726_00.html
  3. Computer___Network/20230516_00.html
  4. Computer___FreeBSD/20220621_0.html
  5. Misc___Taiwan/20240207_00.html
  6. Computer___Python/20220715_0.html
  7. Food___Taiwan/20220429_0.html
  8. Computer___Network/20230508_00.html
  9. Computer___NetBSD/20220817_3.html
  10. Computer___Python/20220410_0.html
  11. Computer___Debian/20210223_1.html
  12. Computer___Python/20210124_0.html
  13. Computer___NetBSD/20220818_1.html
  14. Computer___NetBSD/20220428_0.html
  15. Computer___Python/20221013_0.html
  16. Science___Math/20220420_0.html
  17. Computer___NetBSD/20230119_00.html
  18. Science___Astronomy/20220503_0.html
  19. Computer___TeX/20230503_00.html
  20. Computer___NetBSD/20220808_0.html
  21. Computer___Network/20240130_00.html
  22. Computer___NetBSD/20230515_00.html
  23. Computer___Network/20240416_00.html
  24. Computer___NetBSD/20240101_02.html
  25. Travel___Taiwan/20220809_2.html
  26. Computer___NetBSD/20210204_0.html
  27. Computer___Python/20220816_1.html
  28. Science___Astronomy/20220420_1.html
  29. Food___Taiwan/20230526_00.html
  30. Computer___Python/20240101_00.html


HTML file generated by Kinoshita Daisuke.