matplotlibの入門

このパートでは、我々は以下となります。

  • 単一または複数行のための基本的なmatplotlibのploting能力を探検。
  • このよう伝説、軸ラベルやタイトルとしてプロットするに情報を追加します。
  • ファイルへのプロットを保存します。
  • Ipythonとの相互作用をDescrible。
  • カスタマイズ[自定义] matplotlibの、両方の設定ファイルとPythonのコードを。

グラフを見ていきましょう。

matplotlibのとの最初のプロット

ここに私たちの最初の例があります。

PLT#1インポートとしてインポートmatplotlib.pyplot、ploting pyplotのための主matplotlibのサブモジュール
([1、4、2、6、8、5])#このコード行で実際の描画コマンドplt.plot 
plt.show()#このこのコマンドは、実際にプロット画像を含むウィンドウが開きます。

このコードは、次のスクリーンショットに示すような出力が得られます。

  

ことを強くようにインポートしないように奨励しています:

<モジュール>輸入から*

もちろん、我々はまた、明示的に両方のリストを指定することができます。

PLTとしてインポートmatplotlib.pyplot 
X =範囲(6)
plt.plot(X、[XIにおけるxのXI ** 2])
plt.show()

次のスクリーンショットでその結果:

だから我々はより細かい範囲を生成する(手配使用することができます。

PLT用としてインポートmatplotlib.pyplot 
NPとしてインポートnumpyの
X = np.arange(0.0、6.0、0.01)
plt.plot(X、[XにおけるxのX ** 2])
plt.show()

マルチラインプロット

それはラインをプロットするのも楽しいですが、私たちは同じ図に複数のラインをプロットすることができたときには、もっと楽しいです。我々は、単にショーを呼び出す前に、私たちが望むすべてのラインをプロットすることができ、これは)(matplotlibのを本当に簡単です。次のコードとスクリーンショットを見て:

import matplotlib.pyplot as plt
x = range(1, 5)
plt.plot(x, [xi*1.5 for xi in x])
plt.plot(x, [x**3.0 for x in x])
plt.plot(x, [xi/3.0 for xi in x])
plt.show()

Note how Matplotlib automatically chooses different colors for each line—green for the first line, blue for the second line, and red for the third one (from top to bottom).

Can you tell why a float value was used in line [5]? Try it yourself with an integer one and you'll see. The answer is that if divided by 3 (that is, by using an integer coefficient), the result will also be an integer. So you'll see a line plot like "stairs".

plot() supports another syntax useful in this situation. We can plot multiline figures by passing the X and Y values list in a single plot() call:

import matplotlib.pyplot as plt
x = range(1, 5)
plt.plot(x, [xi*1.5 for xi in x], x, [x**3.0 for x in x], x, [xi/3.0 for xi in x])
plt.show()

The preceding code simply rewrites the previous example with a different syntax.

While list comprehensions are a very powerful tool, they can generate a little bit of confusion in these simple examples. So we'd like to show another feature of the arange() function of NumPy:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 5)
plt.plot(x, x*1.5, x**3.0, x/3.0)
plt.show()

Here, we take advantage of the NumPy array objects returned by arange().

The multiline plot is possible because, by default, the hold property is enabled (consider it as a declaration to preserve all the plotted lines on the current figure instead of replacing them at every plot() call). Try this simple example and see what happens:

おすすめ

転載: www.cnblogs.com/Junlong/p/12122272.html