科学の箱

科学・IT・登山の話題

Python

pyplotでグラフを表示

投稿日:

matplotlib.pyplotを利用すると、配列からお手軽にグラフを作成できる。

内容

  • 最もシンプルなグラフ
  • タイトル
  • ラベル、レジェンドを追加
  • 線の種類を変える

最もシンプルなグラフ

まずはpyplotでグラフを描き、そこにいろいろと足していく。

import matplotlib.pyplot as plt
%matplotlib inline
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.show()

タイトル

グラフの表示を整える―タイトル

ここでは以下の項目について取り扱う

  • タイトルを設定する
  • ラベルを設定する
  • 凡例を設定する

タイトルを設定する

グラフのタイトルはpyplot.titleで設定する。

  • label: タイトルを表示
  • loc : タイトルの場所を変える
  • fontdic : フォント形式を指定

label: タイトルを表示

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.title("Simple Plot Default")
plt.show()

タイトルの場所を変える

次にタイトルの場所を変えてみる。タイトルの場所は引数locに”center”, “right”, “left”を指定する。デフォルトは”center”である。

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.title("Simple Plot Default", loc="left")
plt.show()

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.title("Simple Plot Default", loc="left")
plt.show()

fontdic : フォント形式を指定

タイトルのフォントを変えるためにはfontdictにフォントを設定するための辞書を指定する。辞書の項目はfamily, color, weight, sizeなどがある。すべての項目については以下を参照

Text properties and layout 

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'bold',
        'size': 24,
        }
plt.title("Simple Plot Default", fontdict=font)
plt.show()

ラベル、レジェンドを追加

上記のグラフでは何も情報がないために、報告書などに使うのは不向きである。タイトル、ラベル、レジェンドを追加する。

タイトルはpyplot.title()を使う。引数にタイトル名を指定すればよい。

ラベルについては、x, y軸それぞれにpyplot.xlabel(), pyplot.ylabel()を指定する。

レジェンドはpyplot.legend()を引数なしに呼び出す。

import matplotlib.pyplot as plt
%matplotlib inline
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()

線の種類を変える

plot([x], y, [fmt], *, data=None, **kwargs)

plotでは[fmt]に対して値を指定することで線の種類、色を変えることができる。

fmt = ‘[marker][line][color]’

fmtのデフォルト値は’b’であり、これは青色の直線になる。

下記の例では上から点線、スターマーク、ドットのピンクでグラフを表示している。

import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 2, 100)

plt.plot(x, x, ',--', label='linear')
plt.plot(x, x**2, '*', label='quadratic')
plt.plot(x, x**3, ':m',label='cubic')

plt.show()

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

Python + Slack Bot – 3

さてリアルタイムでとりあえずうまくいったので、もう少し違うサンプルコードを試してみる。 参考にしたのはこちら。 https://www.fullstackpython.com/blog/build-f …

no image

automated the boring – day6

https://automatetheboringstuff.com/chapter11/ さてこの辺からようやく面白くなってくる。webscrapingをするにあたり、まずはブラウザをpythonか …

no image

scikit-learnで適切なアルゴリズムを選択するためのチートシート

  http://scikit-learn.org/stable/tutorial/machine_learning_map/index.html Related posts:seaborn …

no image

pythonでpostgresqlを使うときに必要なモジュール

pip install psycopg2-binary Related posts:pythonで地理情報を取り扱うSIGNATE お弁当の需要予測-4Numpyまとめ

no image

numpyで配列を抜き出す方法

numpyで配列を抜き出す まず基本となるやり方 arr = np.arange(50).reshape(5,10) arr[1:1,] arr[1:2,]   np.arange(50)で …

2019年9月
« 8月   10月 »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

side bar top



アーカイブ

カテゴリー