Quire
matplotlib
matplotlib の公式ロゴマーク
 
matplotlib の公式ロゴマーク Image: source and license
開発元John D. Hunter, Michael Droettboom など
最新版3.11.2[1]  - 2026年9月11日 [±]
リポジトリgithub.com/matplotlib/matplotlib
使用エンジンcairo
Anti-Grain Geometry
プラットフォームクロスプラットフォーム
種別グラフ作成、開発支援、ライブラリ、科学技術分野向け
ライセンスMatplotlib license
公式サイトmatplotlib.org 

Matplotlib(マットプロットリブ)はPython向けグラフ描画ライブラリの一種である[2]。

概要#edit

プログラミング言語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]。

機能#edit

インタラクティブ#edit

Matplotlib はインタラクティブなグラフをサポートする。

Matplotlib は様々な GUI 実装をサポートしているため(⇒ #バックエンド)、それらを抽象化して統一的に扱えるようにするイベントが提供されている[6]。プログラマーはイベントに対応するコールバック関数を作成し登録することでインタラクティブなグラフを作成できる[7]。具体的には計 14 種類のイベントが定義されており(ボタン及びキーの押下・開放、マウスの運動・スクロール・Figure及びAxesへの出入り、ウィンドウの描画・リサイズ・閉鎖、Artistの選択)[8]、これらは Matplotlib に特化した情報も属性として有している(例: イベントが発火した Axes 情報とデータ座標)[9]。

またズーム・リサイズなど一般的なインタラクティブ機能をデフォルトで提供している[10]。

バックエンド#edit

実際の描画を担う要素は backend(バックエンド)と呼ばれる[11]。Matplotlib は様々なバックエンドをサポートする。以下はその一例である:

表. Matplotlib バックエンドの一覧[12]
名称タイプ出力先出力形式
ラスターベクター
AGG非インタラクティブ.png✔-
PDF.pdf-✔
PS.ps、.eps-✔
SVG.svg-✔
PGF.pgf、.pdf-✔
Cairo.png、.ps、.pdf、.svg✔✔
QtAggインタラクティブQt✔
ipymplJupyter Notebook / ipympl✔
GTK4AggGTK✔
macosxmacOS / Cocoa✔
TkAggTk✔
WebAggWebブラウザ / Tornado
GTK4CairoGTK
wxAggWxWidgets✔

古い環境向けのバックエンドとしては GTK3Agg、nbAgg、GTK3Cairo、などがある。

MATLABとの比較#edit

matplotlibのpylabインタフェースは、MATLABの利用経験があるユーザがmatplotlibを簡単に習得できるように設計されている。

Python + Numpy + matplotlib + SciPy + etc. の組み合わせがMATLABに勝る点の例としては、以下のようなものが挙げられる。

  • MATLABのような特定用途向けの言語ではなく、大規模なソフトウェア開発も可能な現代的オブジェクト指向言語であるPythonをベースにしている。
  • 素早くスクリプトを書くのに向いている。CGIスクリプトを作ることもできる。
  • フリーかつオープンソースである。ライセンスサーバも必要ない。
  • ネイティブなSVGのサポート。

プロット例#edit

折れ線グラフ

Image: source and license
>>> 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()

ヒストグラム

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

散布図

Image: source and license
>>> import matplotlib.pyplot as plt
>>> from numpy.random import rand
>>> a = rand(100)
>>> b = rand(100)
>>> plt.scatter(a,b)
>>> plt.show()

3Dグラフ

Image: source and license
>>> 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()

ツールキット#edit

Matplotlibの機能を拡張するためのいくつかのツールキットが存在する。Matplotlibのソースコードに付属するものもあれば、別途にダウンロードする必要のあるものもある。

  • Basemap: 様々な投影法・海岸線・政治的国境による地図の描画
    • 現在は cartopy の使用が推奨されている
  • Mplot3d: 3次元プロット
  • Natgrid: natgridライブラリ用のインタフェース
  • Excel tools: Microsoft Excelとのデータ交換を行なうユーティリティ
  • GTK tools: GTKライブラリ用のインタフェース

脚注#edit

  1. ^ “Release 3.11.2” (2026年9月11日). 2026年9月11日閲覧。
  2. ^ Pythonでグラフ作成に用いる定番のライブラリが「Matplotlib」です。立山, 秀利 (2024-11-25). “データを可視化する「Matplotlib」、Pythonで棒グラフも円グラフも自由自在”. 日経クロステック.
  3. ^ “What's new in matplotlib”. 2012年11月13日閲覧。
  4. ^ “John Hunter Memorial Fund”. 2012年11月13日閲覧。
  5. ^ “PSF Distinguished Service Awards”. 2013年2月16日閲覧。
  6. ^ 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)
  7. ^ To receive events, you need to write a callback function and then connect your function to the event manager(Matplotlib 2025b)
  8. ^ 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)
  9. ^ 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)
  10. ^ in order to support features like interactive panning and zooming of figures, ... have an API for interacting with the figure(Matplotlib 2025b)
  11. ^ Backends are used for displaying Matplotlib figures ... Matplotlib can target different outputs, and each of these capabilities is called a backend(Matplotlib 2025a)
  12. ^ (Matplotlib 2025a)

参考文献#edit

外部リンク#edit

This article is mirrored from “Matplotlib” on 日本語 Wikipedia, revision 111220825, written by Wikipedia's contributors and licensed CC BY-SA 4.0. Editing it here creates a Quire copy with its own history.

Synced with Wikipedia revision 111220825 · just now