|
matplotlib の公式ロゴマーク | |
| 開発元 | John D. Hunter, Michael Droettboom など |
|---|---|
| 最新版 |
3.11.2 |
| リポジトリ | |
| 使用エンジン |
|
| プラットフォーム | クロスプラットフォーム |
| 種別 | グラフ作成、開発支援、ライブラリ、科学技術分野向け |
| ライセンス | Matplotlib license |
| 公式サイト |
matplotlib |
プログラミング言語Pythonおよびその科学計算用ライブラリNumPyのためのグラフ描画ライブラリである。オブジェクト指向のAPIを提供しており、様々な種類のグラフを描画する機能を持つ。描画できるのは主に2次元のプロットだが、3次元プロットの機能も追加されてきている。描画したグラフを各種形式の画像(各種ベクトル画像形式も含む)として保存することが可能で、wxPython、Qt、GTKといった一般的なGUIツールキット製のアプリケーションにグラフの描画機能を組みこむこともできる。MATLABの対話環境のようなものを提供するpylabというインタフェースも持っている。Matplotlibは、BSDスタイルのライセンスの下で配布されている。
matplotlibは、Pythonのバージョン2.6以降、およびPython 3をサポートしている。 matplotlib 1.1.x以前は、Pythonのバージョン2.4から2.7までをサポートしていた。
オリジナルの開発者であるJohn Hunterは、癌治療による合併症のため、2012年8月28日に死去した。しかし、matplotlibの開発にはその他多数の人間が貢献しており、2012年11月9日には、Python 3を初めてサポートするバージョン1.2.0がリリースされた。John HunterにはPythonソフトウェア財団より特別功労賞 (The 2012 Distinguished Service Award) が贈られた。
Matplotlib はインタラクティブなグラフをサポートする。
Matplotlib は様々な GUI 実装をサポートしているため(⇒ #バックエンド)、それらを抽象化して統一的に扱えるようにするイベントが提供されている。プログラマーはイベントに対応するコールバック関数を作成し登録することでインタラクティブなグラフを作成できる。具体的には計 14 種類のイベントが定義されており(ボタン及びキーの押下・開放、マウスの運動・スクロール・Figure及びAxesへの出入り、ウィンドウの描画・リサイズ・閉鎖、Artistの選択)、これらは Matplotlib に特化した情報も属性として有している(例: イベントが発火した Axes 情報とデータ座標)。
またズーム・リサイズなど一般的なインタラクティブ機能をデフォルトで提供している。
実際の描画を担う要素は
| 名称 | タイプ | 出力先 | 出力形式 | |
|---|---|---|---|---|
| ラスター | ベクター | |||
| AGG | 非インタラクティブ | .png | ✔ | - |
| - | ✔ | |||
| PS | .ps、.eps | - | ✔ | |
| SVG | .svg | - | ✔ | |
| PGF | .pgf、.pdf | - | ✔ | |
| Cairo | .png、.ps、.pdf、.svg | ✔ | ✔ | |
| QtAgg | インタラクティブ | Qt | ✔ | |
| ipympl | Jupyter Notebook / ipympl | ✔ | ||
| GTK4Agg | GTK | ✔ | ||
| macosx | macOS / Cocoa | ✔ | ||
| TkAgg | Tk | ✔ | ||
| WebAgg | Webブラウザ / Tornado | |||
| GTK4Cairo | GTK | |||
| wxAgg | WxWidgets | ✔ | ||
古い環境向けのバックエンドとしては GTK3Agg、nbAgg、GTK3Cairo、などがある。
matplotlibのpylabインタフェースは、MATLABの利用経験があるユーザがmatplotlibを簡単に習得できるように設計されている。
Python + Numpy + matplotlib + SciPy + etc. の組み合わせがMATLABに勝る点の例としては、以下のようなものが挙げられる。
折れ線グラフ

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> a = np.linspace(0,10,100)
>>> b = np.exp(-a)
>>> plt.plot(a,b)
>>> plt.show()

>>> import matplotlib.pyplot as plt
>>> from numpy.random import normal,rand
>>> x = normal(size=200)
>>> plt.hist(x,bins=30)
>>> plt.show()

>>> import matplotlib.pyplot as plt
>>> from numpy.random import rand
>>> a = rand(100)
>>> b = rand(100)
>>> plt.scatter(a,b)
>>> plt.show()
3Dグラフ

>>> from matplotlib import cm
>>> from mpl_toolkits.mplot3d import Axes3D
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> fig = plt.figure()
>>> ax = fig.gca(projection='3d')
>>> X = np.arange(-5, 5, 0.25)
>>> Y = np.arange(-5, 5, 0.25)
>>> X, Y = np.meshgrid(X, Y)
>>> R = np.sqrt(X**2 + Y**2)
>>> Z = np.sin(R)
>>> surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm)
>>> plt.show()
Matplotlibの機能を拡張するためのいくつかのツールキットが存在する。Matplotlibのソースコードに付属するものもあれば、別途にダウンロードする必要のあるものもある。
Pythonでグラフ作成に用いる定番のライブラリが「Matplotlib」です。立山, 秀利 (2024-11-25). “データを可視化する「Matplotlib」、Pythonで棒グラフも円グラフも自由自在”. 日経クロステック.
Matplotlib works with a number of user interface toolkits ... it is helpful to the developers to have an API for interacting with the figure ... that is "GUI neutral" so we don't have to repeat a lot of code across the different user interfaces.(Matplotlib 2025b)
To receive events, you need to write a callback function and then connect your function to the event manager(Matplotlib 2025b)
Here are the events that you can connect to, the class instances that are sent back to you when the event occurs, and the event descriptions:(Matplotlib 2025b)
The events that are triggered are also a bit richer vis-a-vis Matplotlib than standard GUI events, including information like which Axes the event occurred in. The events also understand the Matplotlib coordinate system(Matplotlib 2025b)
in order to support features like interactive panning and zooming of figures, ... have an API for interacting with the figure(Matplotlib 2025b)
Backends are used for displaying Matplotlib figures ... Matplotlib can target different outputs, and each of these capabilities is called a backend(Matplotlib 2025a)