python
matplotlib 시각화 정리 [여러 그림 그리기, quiver plot]
cj92
2020. 11. 5. 00:25
In [1]:
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
In [2]:
import os,re
files=[i for i in os.listdir(os.getcwd()) if re.compile('csv').findall(i)]
print(files)
In [3]:
df=pd.read_csv(os.getcwd()+'/'+files[0],encoding='cp949')
u=np.sin(np.deg2rad(270-df['풍향(deg)']))*df['풍속(m/s)']
v=np.cos(np.deg2rad(270-df['풍향(deg)']))*df['풍속(m/s)']
In [4]:
df['일시']=pd.to_datetime(df['일시'])
df=df.set_index(['일시'])
In [5]:
df.head(1)
Out[5]:
In [6]:
df.columns
Out[6]:
폰트 설정¶
In [7]:
import matplotlib.font_manager as fm
font=fm.get_fontconfig_fonts()[11]
font
Out[7]:
In [8]:
font_name = fm.FontProperties(fname=font).get_name()
#matplotlib.rc('font', family=font_name)
matplotlib.rc('font',family='NanumBarunGothic.ttf')
#'C:/Windows/Fonts/malgun.TTF'
마이너스 부호¶
In [9]:
matplotlib.rcParams['axes.unicode_minus']=False
figure 기본옵션¶
In [10]:
plt.figure(num=None, figsize=[6.4,4.8], dpi=100, facecolor='white', edgecolor='w', frameon=True)
plt.plot([1,2,3])
Out[10]:
여러개로 된 그래프 한번에 그릴 때 옵션 조절하기¶
In [11]:
matplotlib.gridspec.GridSpec(nrows=3, ncols=3,left=0.1, bottom=0.15, right=0.94, top=0.94, wspace=0.3, hspace=0.3\
,width_ratios=None,height_ratios =None)
Out[11]:
In [12]:
# equivalent but more general
ax1=plt.subplot(2, 2, 1)
# add a subplot with no frame
ax2=plt.subplot(222, frameon=False)
# add a polar subplot
plt.subplot(223, projection='polar')
# add a red subplot that shares the x-axis with ax1
plt.subplot(224, sharex=ax1, facecolor='red')
# delete ax2 from the figure
plt.delaxes(ax2)
# add ax2 to the figure again
plt.subplot(ax2)
Out[12]:
In [13]:
fig, ax = plt.subplots(2,3)
plt.show()
In [14]:
fig = plt.figure()
#fig를 shape으로 나눴을 때 rowspan, colspan만큼 차지하며 위치는 loc로 찾음
ax1 = plt.subplot2grid(shape=(3,3),loc=(0,0),rowspan=1,colspan=1)
ax2 = plt.subplot2grid(shape=(2,2),loc=(1,1))
plt.show()
In [15]:
df.columns
Out[15]:
In [16]:
defaultColor=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
In [17]:
plt.scatter(df.index,df['기온(°C)'],s=20,c=defaultColor[0],marker='o',label='기온(°C)',edgecolors='black')
plt.grid()
plt.tight_layout()
plt.ylim((0,20))
plt.yticks(range(0, 20, 5),fontsize=15)
plt.gca().yaxis.set_major_formatter(plt.matplotlib.ticker.StrMethodFormatter('{x:,.0f} °C'))
plt.ylabel('시정(m)',fontsize=20,rotation=30)
plt.gca().axhline(12, linestyle='--', color='gray',alpha=.3)
plt.axvspan(pd.to_datetime('2020-10-20 01'), pd.to_datetime('2020-10-22 15'), color='red', alpha=0.2,label='dd')
plt.legend(loc='lower right', prop={'size': 15},scatterpoints=300,bbox_to_anchor=(1, 1))
import matplotlib.dates as md
xfmt = md.DateFormatter('%m/%d %H')
plt.gca().xaxis.set_major_formatter(xfmt)
plt.legend(loc=4,bbox_to_anchor=(0.45,-0.28),framealpha=0.0,fontsize=15,ncol=2)
Out[17]:
In [18]:
fig=plt.figure()
host= plt.subplot()
ax1 = host.twinx()
host.yaxis.set_label_coords(0.06,1.03)
host.set_ylabel(host.get_ylabel(), rotation=0, labelpad=20,fontsize=20)
ax1.spines['right'].set_position(('axes',1.11))
plt.tight_layout()
p0,=plt.plot([1,2,3])
ax1.plot([4,7,8])
host.tick_params(axis='y', colors=p0.get_color(), direction='in')
ax1.tick_params(axis='y', direction='in')
host.spines["left"].set_visible(False)
ax1.spines["left"].set_edgecolor(p0.get_color())
In [19]:
x=df.index
k=1
plt.quiver(x[::k],np.zeros(len(x))[::k],u.values.tolist()[::k],v.values.tolist()[::k],scale_units='y',angles='uv',scale=1,width=.0008)
plt.ylim(-10,10)
plt.axis('off')
Out[19]: