matplotを利用する際、毎回どこかの例をコピペするので、ココにまとめておきます。
今後、追加していきましょう。
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.font_manager import FontProperties
fp=FontProperties(fname='c:/WINDOWS/Fonts/msgothic.ttc',size = 10)
#fp=FontProperties(fname='./font/TakaoGothic.ttf')
#import japanize_matplotlib
#import seaborn as sns
#sns.set(font="IPAexGothic")
#####################################################
#ヒストグラム
#CDF(2軸)
#凡例は外側に配置、透明度0
#####################################################
def plot_hist(col,SaveFName):
data=df[col]
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
# histogram
n, bins, patches = ax1.hist(data,
bins=200,
#density=True,
#cumulative=True,
range=[-100,100],
alpha=0.7,
label='頻度')
# another y-axis
y2 = np.add.accumulate(n) / n.sum()
x2 = np.convolve(bins, np.ones(2) / 2, mode="same")[1:]
lines = ax2.plot(x2, y2, ls='-',
color='r',
#marker='o',
label='累積分布')
# set axis
ax1.set_xlim(-100, 100)
ax1.set_ylim(0, ylim)
ax2.set_ylim(0, 1.05)
#ax2.yaxis.set_major_locator(ticker.MultipleLocator(0.2))
ax1.grid(visible=True)
ax1.set_axisbelow(True)
# legend
handler1, label1 = ax1.get_legend_handles_labels()
handler2, label2 = ax2.get_legend_handles_labels()
ax1.legend(handler1 + handler2,
label1 + label2,
loc=2,
shadow=True,
#borderaxespad=0.
prop=fp
)
ax1.set_xlabel(col, fontproperties=fp)
ax1.set_ylabel('頻度', fontproperties=fp)
ax2.set_ylabel('累積確率', fontproperties=fp)
plt.savefig('./output/'+SaveFName+'.png',
format='png',
dpi=300)
plt.show()
#####################################################
#棒グラフ
#CDF(2軸)
#凡例は外側に配置、透明度0
#####################################################
fig, ax1 = plt.subplots(figsize=(6,3))
ax2 = ax1.twinx()
i=0
for col, data in df.iteritems():
data_sum=data.sum()
data1=data.div(data_sum)
data_cum = data1.cumsum()
bar= ax1.bar(df.index+0.4*i,
data,
width=0.4,
align='edge',
label=str(col))
line = ax2.plot(df.index+0.5,
data_cum,
ls='--',
#marker='o',
#color='r'
label='累積'+str(col))
i=i+1
# set axis
ax1.set_xlim(10, 100)
ax1.set_ylim(0, 105)
ax2.set_ylim(0, 1.05)
ax1.grid(visible=True)
ax1.set_axisbelow(True)
# legend
handler1, label1 = ax1.get_legend_handles_labels()
handler2, label2 = ax2.get_legend_handles_labels()
ax1.legend(handler1 + handler2,
label1 + label2,
bbox_to_anchor=(1.15, 1.02),
loc='upper left',
shadow=True,
#borderaxespad=0.
prop=fp
).get_frame().set_alpha(1.0)
ax1.set_xlabel('aaa', fontproperties=fp)
ax1.set_ylabel('bbb', fontproperties=fp)
ax2.set_ylabel('ccc', fontproperties=fp)
plt.savefig('./output/ddd.png',
format='png',
bbox_inches='tight',
dpi=300)
#####################################################
#折れ線(マーカーのみの散布図)
#移動平均
#dfのカラムを順にプロット
#散布図と移動平均の色を合わせる
#####################################################
def plot_rolling(df, SaveFName):
fig, ax1 = plt.subplots()
for i in range(df.shape[1]):
lines = ax1.plot(df.index,df.iloc[:,i],
ls=' ',
color='C'+str(i),
marker='o',
label=df.columns[i])
for i in range(df_rate.shape[1]):
s=df.iloc[:,i].rolling(window=10).mean()
lines = ax1.plot(df.index-4.5,s,
ls='-',
color='C'+str(i),
#marker='o',
label='移動平均'+str(df.columns[i]))
# set axis
ax1.set_xlim(0, 50)
ax1.set_ylim(0, 0.05)
ax1.grid(visible=True)
ax1.set_axisbelow(True)
# legend
ax1.legend(bbox_to_anchor=(1.05, 1), loc='upper left',prop=fp)
ax1.set_xlabel('aaa', fontproperties=fp)
ax1.set_ylabel('bbb', fontproperties=fp)
plt.savefig('./output/'+SaveFName+'.png',
format='png',
bbox_inches='tight',
dpi=300)