본문 바로가기

수업정리/Data Visualization

Chap1. (Week2) matplotlib, numpy

https://colab.research.google.com/drive/1EWQ_g5sTDbKG_iDN35gTtuL22c3edHPb?usp=sharing

plt.plot

 
import matplotlib.pyplot as plt

# 우리나라의 연간 1인단 국민소득을 각각 years, gdp에 저장
years=[1950,1960,1970,1980,1990,2000,2010]
gdp= [67.0, 80.0, 257.0, 1686.0, 6505, 11865.3, 22105.3]

# plt.plot(x, y, color, market, linestyle)
plt.plot(years,gdp, color='green', marker='o', linestyle='solid', )

# title it / 1인당 국민소득
plt.title("GDP per capita")

# label at x, y
plt.xlabel("year")
plt.ylabel("dollars")

# x, y 범위 지정
plt.axis([1950,2020,0,23000])

plt.legend() #디폴트 위치에 범례(각 차트를 설명)를 표
 

plt.savefig("gdp_per_capita.png", dpi=300) #can save as png
plt.show()


plt.bar

import numpy as np
import matplotlib.pyplot as plt

years= [1965,1975, 1985, 1995, 2005, 2015]
ko= [130,650,2450,11600,17790,27250]
jp= [890, 5120,11500,42130,40560,38780]
ch= [100,200,290,540,1760,7940]


# 기준이 되는 점 만들고 / 소수 -+ 가능하게 하기
x_range= np.arange(len(years))    # years의 길이만큼 소수점 단위도 가능한 범위 생성. check ppt's 6th page
plt.xticks(range(len(years)),years) # x 축을 원소들로 입력해줘야함

plt.bar(x_range+0.0,ko,width=0.25)
plt.bar(x_range+0.3,jp,width=0.25)
plt.bar(x_range+0.6,ch,width=0.25)

plt.show()

 


plt.scatter

import matplotlib.pyplot as plt
import numpy as np

xData=np.arange(20,50)
yData= xData + 2 * np.random.randn(30) #add noise to xData using randn()

plt.scatter(xData, yData)
plt.title('Real Age vs Physical Age')
plt.xlabel('Real Age')
plt.ylabel('Physical Age')

plt.savefig("age.png", dpi=600)
plt.show()


plt.scatter2 + 파일 저장

import matplotlib.pyplot as plt
import numpy as np

# Generate random numbers for x and y values
xValues = np.random.randn(1000)# 1000 random values with a standard normal distribution
yValues = np.random.randn(1000)  # Another 1000 random values for the y axis

# Create a scatter plot of these values
plt.scatter(xValues, yValues, alpha=0.5)
plt.title('2D Scatter Plot with Standard Normal Distribution')
plt.xlabel('X Values')
plt.ylabel('Y Values')

plt.savefig("scatter_plot.png", dpi=600)  # Save the plot with high resolution
plt.show()

 

ppt Box Chart 부터 시작

 

'수업정리 > Data Visualization' 카테고리의 다른 글

[중간대비] (Week2 - Week3) 이론 정리  (0) 2024.04.12
Chap2. matplotlib, numpy  (3) 2024.03.15