参照
レベル
最上級:matplotlibの「ステートマシン環境」。オブジェクトのメソッドとプロパティを使用していない、物事を行うには、モジュール全体のPLT機能の使用、次の例を参照してください。
import matplotlib.pyplot as plt
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212) # the second subplot in the first figure
plt.plot([4, 5, 6])
plt.figure(2) # a second figure
plt.plot([4, 5, 6]) # creates a subplot(111) by default
plt.figure(1) # figure 1 current; subplot(212) still current
plt.subplot(211) # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title
plt.show()
二次:オブジェクト指向インタフェースの第1層。この時点で、PLTの機能モジュールは、図、軸や他のオブジェクトを作成し、インタフェースオブジェクトを直接物事を行うために使用されます。
x = np.arange(0, 10, 0.2)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y) # 使用ax的方法做事情
plt.show()
オブジェクト
フィギュア
図は、複数の軸を備え、それはないかもしれません
- 複数の数字、複数の軸
import matplotlib.pyplot as plt
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212) # the second subplot in the first figure
plt.plot([4, 5, 6])
plt.figure(2) # a second figure
plt.plot([4, 5, 6]) # creates a subplot(111) by default
plt.figure(1) # figure 1 current; subplot(212) still current
plt.subplot(211) # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title
plt.show()
- フィギュア、無軸
fig = plt.figure() # an empty figure with no axes
fig.suptitle('No axes on this figure') # Add a title so we know which it is
# plt.plot([1,2,3]) # draw a line (now have an axes)
# plt.show() # show the plot
軸
ターゲット部材は、複数の軸が(そこ二つが、であり、xおよびy軸である2次元プロット、3Dプロットは、3つ有する)軸オブジェクトました
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.2)
y = np.sin(x)
fig, ax = plt.subplots()
fig.suptitle("basic math")
ax.plot(x, y, label='sin') # 使用ax的方法做事情
ax.set_title('sin function')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_xlim(-1, 11)
ax.legend()
plt.show()
データ
好ましくnp.arrayデータ、PD、およびnp.matrix次の変換であってもよいです
a = pandas.DataFrame(np.random.rand(4,5), columns = list('abcde'))
a_asarray = a.values
b = np.matrix([[1,2],[3,4]])
b_asarray = np.asarray(b)
MPL、PLTとpylab
matplotlibのはpyplotはmatplotlibの、pylab収集pyplot下のモジュールであり、統一された名前空間(推奨されません)を形成するnumpyの、パッケージです。
PLTのために、その機能は、現在の図と現在の軸(自動的に作成)が常にあります
x = np.linspace(0, 2, 100)
plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()
コードスタイル
- いないすべてのオブジェクトのNpを明示的にインポートPLTと再利用は、汚染の名前空間を避けるために、導入しました
- 使用PLTは、軸とFigureオブジェクトを作成し、制御方法は、さらにオブジェクトを使用します
- データのNPメイク使用
- PLT表示画像を使用します
# 导入方式
import matplotlib.pyplot as plt
import numpy as np
def my_plotter(ax, data1, data2, param_dict):
"""
A helper function to make a graph
Parameters
----------
ax : Axes
The axes to draw to
data1 : array
The x data
data2 : array
The y data
param_dict : dict
Dictionary of kwargs to pass to ax.plot
Returns
-------
out : list
list of artists added
"""
out = ax.plot(data1, data2, **param_dict) # 使用对象操作
return out
# which you would then use as:
data1, data2, data3, data4 = np.random.randn(4, 100) # 数据
fig, (ax1, ax2) = plt.subplots(1, 2) # 获取figure和axes对象
my_plotter(ax1, data1, data2, {'marker': 'x'})
my_plotter(ax2, data3, data4, {'marker': 'o'})
plt.show()
バックエンド
インタラクティブおよび非対話型バックエンドバックエンド
バックエンドを指定します
- バックエンドのパラメータmatplotlibrcファイルが(私のコンピュータは、このファイルはに位置している
C:\ Users \ユーザーXXXX \ PycharmProjects \無題\ venv \ Libの\のsite-packages \ matplotlibの\ の下MPL-データ)
- 脚本中指定
import matplotlib
matplotlib.use('PS') # generate postscript output by default
この時点で、plt.showスクリプトがある場合は()の画像を表示しない、とUserWarning報告されます。
- 環境変数(NO特定の例こと)
パフォーマンスの考慮事項
主な単純化は、レンダリング、線、印、等を含む、単純な高速モードの設定として使用することができるされています
import matplotlib.style as mplstyle
mplstyle.use('fast')