Matplotlib
![]() | |
開発元 | John D. Hunter, Michael Droettboom など |
---|---|
最新版 |
3.10.3[1] ![]() |
リポジトリ | |
使用エンジン |
|
プラットフォーム | クロスプラットフォーム |
種別 | グラフ作成 |
ライセンス | matplotlib licence |
公式サイト |
matplotlib |
概要
[編集]プログラミング言語Pythonおよびその科学計算用ライブラリNumPyのためのグラフ描画ライブラリである。オブジェクト指向のAPIを提供しており、様々な種類のグラフを描画する能力を持つ。描画できるのは主に2次元のプロットだが、3次元プロットの機能も追加されてきている。描画したグラフを各種形式の画像(各種ベクトル画像形式も含む)として保存することもできるし、wxPython、Qt、GTKといった一般的なGUIツールキット製のアプリケーションにグラフの描画機能を組みこむこともできる。MATLABの対話環境のようなものを提供するpylabというインタフェースも持っている。Matplotlibは、BSDスタイルのライセンスの下で配布されている。
matplotlibは、Pythonのバージョン2.6以降、およびPython 3をサポートしている[3]。 matplotlib 1.1.x以前は、Pythonのバージョン2.4から2.7までをサポートしていた。
オリジナルの開発者であるJohn Hunterは、癌治療による合併症のため、2012年8月28日に死去した[4]。しかし、matplotlibの開発にはその他多数の人間が貢献しており、2012年11月9日には、Python 3を初めてサポートするバージョン1.2.0がリリースされた。John HunterにはPythonソフトウェア財団より特別功労賞 (The 2012 Distinguished Service Award) が贈られた[5]。
機能
[編集]インタラクティブ
[編集]Matplotlib はインタラクティブなグラフをサポートする。
Matplotlib は様々な GUI 実装をサポートしているため(⇒ #バックエンド)、それらを抽象化して統一的に扱えるようにするイベントが提供されている[6]。プログラマーはイベントに対応するコールバック関数を作成し登録することでインタラクティブなグラフを作成できる[7]。具体的には計 14 種類のイベントが定義されており(ボタン及びキーの押下・開放、マウスの運動・スクロール・Figure及びAxesへの出入り、ウィンドウの描画・リサイズ・閉鎖、Artistの選択)[8]、これらは Matplotlib に特化した情報も属性として有している(例: イベントが発火した Axes 情報とデータ座標)[9]。
またズーム・リサイズなど一般的なインタラクティブ機能をデフォルトで提供している[10]。
バックエンド
[編集]実際の描画を担う要素は
名称 | タイプ | 出力先 | 出力形式 | |
---|---|---|---|---|
ラスター | ベクター | |||
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
、などがある。
MATLABとの比較
[編集]matplotlibのpylabインタフェースは、MATLABの利用経験があるユーザがmatplotlibを簡単に習得できるように設計されている。
Python + Numpy + matplotlib + SciPy + etc. の組み合わせがMATLABに勝る点の例としては、以下のようなものが挙げられる。
- MATLABのような特定用途向けの言語ではなく、大規模なソフトウェア開発も可能な現代的オブジェクト指向言語であるPythonをベースにしている。
- 素早くスクリプトを書くのに向いている。CGIスクリプトを作ることもできる。
- フリーかつオープンソースである。ライセンスサーバも必要ない。
- ネイティブなSVGのサポート。
プロット例
[編集]折れ線グラフ

>>> 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のソースコードに付属するものもあれば、別途にダウンロードする必要のあるものもある。
- Basemap: 様々な投影法・海岸線・政治的国境による地図の描画
- 現在は cartopy の使用が推奨されている
- Mplot3d: 3次元プロット
- Natgrid: natgridライブラリ用のインタフェース
- Excel tools: Microsoft Excelとのデータ交換を行なうユーティリティ
- GTK tools: GTKライブラリ用のインタフェース
脚注
[編集]- ^ "Release 3.10.3"; 閲覧日: 2025年5月27日; 出版日: 2025年5月9日.
- ^
Pythonでグラフ作成に用いる定番のライブラリが「Matplotlib」です。
立山, 秀利 (2024-11-25). “データを可視化する「Matplotlib」、Pythonで棒グラフも円グラフも自由自在”. 日経クロステック . - ^ “What's new in matplotlib”. 2012年11月13日閲覧。
- ^ “John Hunter Memorial Fund”. 2012年11月13日閲覧。
- ^ “PSF Distinguished Service Awards”. 2013年2月16日閲覧。
- ^
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) - ^ (Matplotlib 2025a)
参考文献
[編集]- “Backends”. Matplotlib 3.10.3 documentation. 2025年7月12日閲覧。
- “Event handling and picking”. Matplotlib 3.10.3 documentation. 2025年7月14日閲覧。
外部リンク
[編集]- 公式ウェブサイト
- matplotlib Thumbnail gallery - 多数の応用的なプロット例を見ることができる。
- Matplotlib Cookbook