<수업 내용>
scatter marker
import matplotlib.pyplot as plt
fig,ax=plt.subplots(figsize=(5,5))
ax.scatter(0,0,
s=10000,
facecolor='red',
edgecolor='b',
linewidth=5)
import numpy as np
n_data=100
x_data=np.random.normal(0,1,(n_data,))
y_data=np.random.normal(0,1,(n_data,))
fig,ax=plt.subplots(figsize=(7,7))
ax.scatter(x_data, y_data,
s=300,
facecolor='None',
edgecolor='tab:blue',
linewidth=5,
alpha=0.6)
plot그래프의 x_ticks 조절
np.random.seed(0)
n_data=100
s_idx=30
x_data=np.arange(s_idx,s_idx + n_data)
y_data=np.random.normal(0,1,n_data)
fig, ax=plt.subplots(figsize=(10,5))
ax.plot(x_data, y_data)
fig.tight_layout(pad=3)
x_ticks=np.arange(s_idx,s_idx +n_data +1, 20)
ax.set_xticks(x_ticks)
ax.tick_params(labelsize=25)
ax.grid()
plot 그래프의 y_lim 조절
np.random.seed(0)
x_data=np.array([10,25,31,40,55,80,100])
y_data=np.random.normal(0,1,7)
fig,ax=plt.subplots(figsize=(10,5))
ax.plot(x_data, y_data)
fig.subplots_adjust(left=0.2)
ax.tick_params(labelsize=10)
ax.set_xticks(x_data)
ylim=ax.get_ylim()
yticks=np.linspace(ylim[0],ylim[1],10)
ax.set_yticks(yticks)
ax.grid()
삼각함수 그리기
import numpy as np
import matplotlib.pyplot as plt
PI=np.pi
t=np.linspace(-4*PI, 4*PI, 100)
sin=np.sin(t)
linear=0.1*t
fig,ax=plt.subplots(figsize=(14,7))
ax.plot(t,sin)
ax.plot(t, linear)
ax.set_ylim([-1.5,1.5])
x_ticks=np.arange(-4*PI, 4*PI+0.1,PI)
x_ticklabels=[str(i)+ r'$\pi$' for i in range(-4,5)]
ax.set_xticks(x_ticks)
ax.set_xticklabels(x_ticklabels)
ax.tick_params(labelsize=30)
ax.grid()
PI=np.pi
t=np.linspace(-4*PI, 4*PI, 1000)
sin=np.sin(t)
cos=np.cos(t)
tan=np.tan(t)
tan[:-1][np.diff(tan)<0]=np.nan
fig,axes=plt.subplots(3,1,figsize=(7,10))
axes[0].plot(t,sin)
axes[1].plot(t,cos)
axes[2].plot(t,tan)
fig.tight_layout()
axes[2].set_ylim([-5,5])
PI=np.pi
t=np.linspace(-4*PI, 4*PI, 1000).reshape(1,-1)
print(t.shape)
sin=np.sin(t)
cos=np.cos(t)
tan=np.tan(t)
data=np.vstack((sin, cos, tan))
title_list=[r'$sin(t)$', r'$cos(t)$', r'$tan(t)$']
x_ticks=np.arange(-4*PI, 4*PI+0.1,PI)
x_ticklabels=[str(i)+ r'$\pi$' for i in range(-4,5)]
fig,axes=plt.subplots(3,1,figsize=(7,10), sharex=True)
for ax_idx, ax in enumerate(axes.flat):
ax.plot(t.flatten(), data[ax_idx]),
ax.set_title(title_list[ax_idx], fontsize=30),
ax.tick_params(labelsize=20)
ax.grid()
if ax_idx==2:
ax.set_ylim([-3,3])
fig.subplots_adjust(left=0.1, right=0.95, bottom=0.05, top=0.95)
axes[-1].set_xticks(x_ticks)
axes[-1].set_xticklabels(x_ticklabels)
점근선
fig,ax=plt.subplots(figsize=(7,7))
ax.set_xlim([-5,5])
ax.set_ylim([-5,5])
ax.axvline(x=1,
ymax=0.8,
ymin=0.2,
color='black',
linewidth=1)
fig,ax=plt.subplots(figsize=(7,7))
ax.set_xlim([-5,5])
ax.set_ylim([-5,5])
ax.axhline(y=1,
xmax=0.8,xmin=0.2,
color='black',
linewidth=1)
x=np.linspace(-4*np.pi, 4*np.pi, 200)
sin=np.sin(x)
fig, ax=plt.subplots(figsize=(10,5))
ax.plot(x,sin)
ax.axhline(y=1, ls=":", lw=1,color='gray')
ax.axhline(y=-1, ls=":", lw=1,color='gray')
plt.show()
legend 설정
np.random.seed(0)
n_data=100
random_noise1=np.random.normal(0,1,n_data)
random_noise2=np.random.normal(1,1,n_data)
random_noise3=np.random.normal(2,1,n_data)
fig, ax=plt.subplots(figsize=(10,7))
ax.tick_params(labelsize=20)
ax.plot(random_noise1, label='random noise1')
ax.plot(random_noise2, label='random noise2')
ax.plot(random_noise3, label='random noise3')
ax.legend(fontsize=10,
loc='upper right')
ax.legend(fontsize=10,
bbox_to_anchor=(1,0.5),
loc='center left')
Iris 데이터 violinpolt그리기
from sklearn.datasets import load_iris
import numpy as np
import pandas as pd
Iris = load_iris()
Iris_Data = pd.DataFrame(data=Iris['data'], columns= Iris['feature_names'])
Iris_target = pd.DataFrame(data=Iris['target'],columns = ['target'])
Iris_target.replace([0,1,2],Iris['target_names'],inplace=True)
total_data=pd.concat([Iris_Data, Iris_target], axis=1)
fig,ax=plt.subplots(figsize=(7,7))
df=total_data.groupby('target')['sepal width (cm)']
df1=df.get_group("setosa")
df2=df.get_group("versicolor")
df3=df.get_group("virginica")
xticks=np.arange(3)
violin=ax.violinplot([df1,df2,df3], showmeans=True, positions=xticks)
ax.set_xticks(xticks)
ax.set_xticklabels(["setosa","versicolor","virginica"])
ax.set_xlabel('Species', fontsize=15)
ax.set_ylabel('sepal length', fontsize=15)