折れ線グラフと棒グラフの混在は可能か?できます。
ソースです。
#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.style as mplstyle
import matplotlib
import pandas as pd
matplotlib.rc ('font', family='Noto Sans CJK JP')
data = pd.read_csv('weather.csv')
df = data.iloc[0:10]
print(df['Temparature'])
hour = df['Hour']
min = min(hour)
max = max(hour)
color = 'red'
fig = plt.figure(figsize=(4.0, 3.0), tight_layout = True)
ax1 = fig.add_subplot(111, xlabel='時', ylabel='気温',xticks=df.Hour)
ax1.plot(df['Hour'], df['Temparature'], 'o-', color= color)
ax1.tick_params(axis='y', labelcolor=color)
color = 'blue'
w= 0.3
ax2 = ax1.twinx() # X軸を共有するax2を作成
ax2.set_ylabel('湿度', color=color)
ax2.bar(df['Hour'], df['Humidity'], width=w, align='center', color = color, alpha = 0.6)
ax2.tick_params(axis='y', labelcolor=color)
plt.savefig('line_bar.png')
plt.show()
何もしないと折れ線グラフが棒グラフで上書きされるところがあります。 alpha = 0.6
で棒グラフの透明度を上げて下の折れ線グラフを見えるようにします。
|