기본 콘텐츠로 건너뛰기

[matplotlib] 등고선(Contour)

최근 글

벡터와 행렬에 관련된 그림들

다음의 코드들과 그림들은 전자책 파이썬과 함께 하는 선형대수 에 수록된 것입니다. import numpy as np import matplotlib.pyplot as plt import seaborn sns.set_style("darkgrid") #fig 1.1.1 fig, ax=plt.subplots(figsize=(3,2)) cord=[(0,0), (3,1),(2,3)] nme=["O","A","B"] col=['g','b','r'] for i, j in enumerate(cord): ax.scatter(j[0], j[1], color="white", edgecolors=col[i]) ax.text(j[0], j[1]+0.15, nme[i], color=col[i], fontweight="bold") ax.arrow(0,0, 3, 1, color="b", head_width=0.1) ax.arrow(0,0, 2, 3, color="r", head_width=0.1) ax.text(1, 0.5, r"a", color="b") ax.text(1, 1.8, r"b",color="r") ax.spines['left'].set_position(("data", 0)) ax.spines['bottom'].set_position(("data", 0)) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.grid(True) plt.show() #fig112 a=np.array([10,15])...

Statistical analysis results graph code

The following graphs are the codes for the figures included in Chapters 6 and 7 of the e-book Statistics with Python . import numpy as np import pandas as pd from scipy import stats from sklearn.preprocessing import StandardScaler import FinanceDataReader as fdr import yfinance as yf import statsmodels.api as sm from statsmodels.formula.api import ols from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt import seaborn as sns sns.set_style("darkgrid") #fig 611 st=pd.Timestamp(2024,1, 1) et=pd.Timestamp(2024, 5, 30) code=["^KS11", "^KQ11", "^DJI", "KRW=X"] nme=['kos','kq','dj','WonDol'] da=pd.DataFrame() for i in code: x=yf.download(i,st, et)['Close'] x1=x.pct_change() da=pd.concat([da, x1], axis=1) da.columns=nme da1=da.dropna() da1.index=range(len(da1)) da2=pd.melt(da1, value_vars=['kos', 'kq', 'dj', 'WonDol'], var_name=...

Graph code related to statistical tests

The following graphs are the codes for the figures included in Chapter 5 of the e-book Statistics with Python . import numpy as np import pandas as pd from scipy import stats from sklearn.preprocessing import StandardScaler import yfinance as yf import matplotlib.pyplot as plt import seaborn as sns sns.set_style("darkgrid") #fig511 st=pd.Timestamp(2024, 4,20) et=pd.Timestamp(2024, 5, 30) da1=yf.download('GOOGL', st, et)["Close"] da2=yf.download('MSFT', st, et)["Close"] da1=da1.iloc[:,0].pct_change()[1:]*100 da2=da2.iloc[:,0].pct_change()[1:]*100 da=pd.DataFrame([da1, da2], index=['data1', 'data2']).T da.index=range(len(da1)) mu1, sd1, n1=np.mean(da1), np.std(da1, ddof=1), len(da1) mu2, sd2, n2=np.mean(da2), np.std(da2, ddof=1), len(da2) s_p=np.sqrt(((n1-1)*sd1**2+(n2-1)*sd2**2)/(n1+n2-2)) se=s_p*np.sqrt((1/n1+1/n2)) df=n1+n2-2 mu=mu1-mu2 testStatic=((mu1-mu2)-0)/se x=np.linspace(-3, 3, 500) p=stats.t.pdf(x, df) l=stats.t...

Statistics related graph code

The following graphs are the codes for the figures included in Chapter 4 of the e-book Statistics with Python . import numpy as np import pandas as pd from scipy import stats from sklearn.preprocessing import StandardScaler import yfinance as yf import matplotlib.pyplot as plt import seaborn as sns sns.set_style("darkgrid") # fig 421 st=pd.Timestamp(2022,12, 1) et=pd.Timestamp(2023, 4, 1) da=yf.download("^DJI", st, et) pop=(da['Close']-da['Open'])/da['Open']*100 pop.columns=['change'] pop.index=pd.DatetimeIndex(pop.index.date) xBar=np.array([]) for i in range(20): x=pop.sample(5, replace=False, random_state=i) xBar=np.append(xBar, x.mean()) xBar2=np.array([]) for i in range(100): x=pop.sample(5, replace=False, random_state=i) xBar2=np.append(xBar2, x.mean()) plt.figure(figsize=(7, 3)) plt.subplot(1,2,1) plt.hist(xBar, 10, rwidth=0.8, color="blue", label="n=20") plt.axvline(-0.18, 0, 0.9, color=...