import matplotlib.pyplot as plt
import pandas as pd
from pandas_datareader import data
# 선그래프
plt.plot([1,2,3,4,5], [11,12,13,14,15]) # [x값], [y값]
plt.show()
# 데이터프레임으로 그래프 그리기
plt.plot(df.index, df['Close'])
plt.show()
# 그래프 옵션 추가
plt.figure(figsize=(10,10)) # 그래프 사이즈
plt.plot(df.index, df['Close'], color="crimson")
plt.xlabel('time') # x축 이름
plt.ylabel('price') # y축 이름
plt.legend(['apple']) # 범례
plt.show()
# 여러개 선 동시에 그리기 (plt.plot 여러번 사용)
plt.plot([2018,2019,2020,2021], [50000,60000,75000,70000])
plt.plot([2018,2019,2020,2021], [30000,40000,50000,35000])
plt.xlabel('time')
plt.ylabel('sales')
plt.legend(['Samsung', 'LG'])
plt.show()
# bar 그래프
plt.bar(['A','B','C'], [10,20,30])
plt.show()
# 파이 그래프
plt.pie([5,10,15])
plt.show()
# 히스토그램
plt.hist([160,161,168,165,169,170,171,172,180])
plt.show()
# 분포도 (scatter)
math = [5,8,23,5,67,34,34,23]
eng = [23,6,3,1,5,45,54,34]
plt.scatter(math, eng)
plt.show()
# y값 누적 그래프
plt.stackplot(['A','B','C'], [10,20,30], [30,20,50], [10,20,30])
plt.show()
# Volume변수 2020년의 평균Volume보다 높은 날의 가격만 그래프로 그리기
df = data.DataReader('BTC-USD','yahoo', start="2020-01-01", end="2020-12-31")
avg = df['Volume'].mean()
print(avg)
df['tf'] = df['Volume'] > avg # 평균보다 높으면 True 아니면 False를 저장하는 tf변수 생성
print(df)
a = df[df['tf']==True] # df['tf'] 안에 들어있는 값이 True 인 행만 출력하는 새로운 a라는 데이터프레임 생성
print(a)
plt.bar(a.index, a['Close'])
plt.show()
'Python' 카테고리의 다른 글
| pykrx_상장종목 일별종가 (0) | 2024.02.13 |
|---|---|
| 네이버 파파고 API 활용 (0) | 2024.02.12 |
| 주식 일별종가_(t+1, t+2, ... 형식) (3) | 2024.02.07 |
| yfinance 주가 데이터 가져오기 (2) | 2024.02.04 |
| pandas_2, 정규식 (3) | 2024.02.04 |