2
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python matplotlibでグラフを作る(超初心者向け)

Last updated at Posted at 2018-03-27

文字に日本語を使用すると文字化けしたので、FONTの指定を追加。

Python matplotlib 説明図を書いてみる
https://qiita.com/damyarou/items/eafcf27aa1a7852d32e9
を見て、matplotlibって割とすごいことができるなと思い、どのような事をしているのか見ても、私レベルの知識では???でした。

他にもいろいろサンプルプログラムがあるのですが、これもちょっと難しいので私が理解出来る範囲でいろいろやっています。

一番短いプログラムのグラフ

ドキュメント
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html

Y = 15, 30, 3, 25
X = 0, 1, 2, 3 (x座標のリストを省略)

graph.py
import matplotlib.pyplot as plt
plt.plot([15,30,3,25])  #リストを直接記載、x座標のリストを省略
# plt.plot([0,1,2,3],[15,30,3,25])  #x座標のリストを省略しない場合
plt.show()

image.png

基本的なグラフ
Graph.py
# グラフ y=x^2
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 100, endpoint=True)
# x = np.arange(-5,5 + 0.1, 0.1)  #見た目は同じグラフが出来る
y = x ** 2

plt.plot(x, y)  #実線
# plt.plot(x, y, 'bo')  #ドット(青色の丸)
# plt.plot(x, y, 'g^')  #ドット(緑色の▲)

plt.show()

image.png
グラフ.png

円グラフ

ドキュメント
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html

Graph.py
# 円グラフ
import matplotlib.pyplot as plt

a = ([1,2,3,20,5,6,7,8,9,10])
a[3] = 4  #20を4に変更

plt.pie(a)  #円グラフ

plt.show()

# グラフn1の角度
# 360 / (a[0] + ・・・ + a[n]) * a[n1]

円グラフ.png

文字

ドキュメント
https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text

text.py
# Text
import matplotlib.pyplot as plt

font = {'family':'IPAGothic'} #日本語Fontを指定

plt.text(0.5, 0.5, '日本語', color="#FF0000", fontsize=20, alpha=0.5, rotation=-45, **font)   #alpha=透明度

plt.text(0.2, 0.2, 'ABC', color="#FF0000", fontsize=20, alpha=0.5, rotation=45,ha='center',va='center')   #ha=文字の基準位置(横方向)、va=文字の基準位置(縦方向)

plt.text(0.4, 0.1, 'Text',fontsize=12,fontweight='bold',ha='left',va='bottom',color='k')

plt.text(0.1, 0.8, 'matplotlib',fontsize=12,fontweight='bold',ha='left',va='center',color='k',bbox=dict(facecolor='b', alpha=0.2))

plt.show()

image.png

罫線

ドキュメント
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.grid.html

RuledLine.py
import matplotlib.pyplot as plt
import numpy as np

xmax = 4
ymax = 3
scalewidth = 0.2

font = {'family':'IPAGothic'} #日本語Fontを指定

plt.xlim([0,xmax])
plt.ylim([0,ymax])

plt.xlabel('X座標', **font)
plt.ylabel('Y座標', **font)

plt.xticks(np.arange(0,xmax+scalewidth,scalewidth))
plt.yticks(np.arange(0,ymax+scalewidth,scalewidth))
plt.grid(color='#FF0000',linestyle='solid')

plt.title('日本語', **font)

plt.show()

image.png

矢印プロット(ベクトル)

ドキュメント
https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.quiver.html

vector.py
import matplotlib.pyplot as plt

plt.quiver(0.5,0.5,0.5,0.5)  #(x,y,u,v) x,y-始点座標、u,v-ベクトルの向き
# plt.quiver(0.5,0.5,0.5,0.5,color='red')

plt.show()

ベクトル.png

寸法

上記の(Python matplotlib 説明図を書いてみる)@damyarouさんを参考にしました。

dim.py
import matplotlib.pyplot as plt
import matplotlib.ticker as tick

arst='<->,head_width=3,head_length=10'

x1=0.1; y1=0.5; x2=0.5; y2=y1

plt.annotate('',
    xy=(x1,y1), xycoords='data',
    xytext=(x2,y2), textcoords='data', fontsize=0,  #fontsize 矢印の大きさ
    arrowprops=dict(arrowstyle=arst,connectionstyle='arc3',facecolor='#333333',edgecolor='#333333',shrinkA=0,shrinkB=0))

plt.text(0.3,0.55,'0.4',color="#FF0000",ha='center',va='center',fontsize=20,rotation=0)

plt.show()

寸法.png

Python matplotlibでグラフを作る(超初心者向け)-2
https://qiita.com/ty21ky/items/2b1403317e769cccabac

参考
サンプルプログラムが沢山あります。
https://matplotlib.org/xkcd/examples/index.html
https://matplotlib.org/gallery/index.html

MATPLOTLIB
https://matplotlib.org/xkcd/api/pyplot_api.html?highlight=plot#matplotlib.pyplot.plot

1.科学技術計算のために Python を始めよう。
1.4.Matplotlib: 作図
http://www.turbare.net/transl/scipy-lecture-notes/intro/matplotlib/matplotlib.html

2
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?