Programming

파이썬 데이타 표현 그래프 matplotlib

신농해태 2022. 3. 29. 09:37
반응형

import matplotlib.pyplot as plt

x = [1,2,3]
y = [2,4,8]
plt.plot(x,y)

 

plt.plot(x,y)
plt.show()

 

print(plt.plot(x,y))

 

plt.plot(x,y)
plt.title('line')

 

import matplotlib.font_manager as fm
fm.fontManager.ttflist
[f.name for f in fm.fontManager.ttflist]

 

import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False
plt.plot(x,y)
plt.title('그래프')

 

plt.plot([-1,0,1],[-5,-1,2])

 

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

 

x = [1,2,3]
y = [2,4,8]

 

plt.plot(x,y)
plt.title('꺽은선 그래프', fontdict={'family':'HYGungSo-Bold','size':20})

 

plt.plot(x,y)
plt.xlabel('x축', color='red', loc='right') # left, center,right
plt.ylabel('y축', color='#00aa00', loc='top') #top, center,bottom

 

plt.plot(x,y)
plt.xticks([1,2,3])
plt.yticks([3,6,9,12])
plt.show()

 

범례

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

 

x = [1,2,3]
y = [2,4,8]

 

plt.plot(x,y,label='데이타')
plt.legend(loc='upper right') #lower

 

plt.plot(x,y,label='데이타')
plt.legend(loc=(0.1,0.7)) #lower

 

스타일

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

x = [1,2,3]
y = [2,4,8]

plt.plot(x,y)

 

plt.plot(x,y,linewidth=5)

 

plt.plot(x,y,marker='o', linestyle='none')

 

plt.plot(x,y,marker='v', markersize=10, linestyle='none')

 

plt.plot(x,y, marker='X', markeredgecolor='red', markerfacecolor='yellow')

 

plt.plot(x,y,linestyle='-.') # -- :

 

 plt.plot(x,y,color='pink') # #ff0000  g  b  r

 

plt.plot(x,y, 'ro--')  # color, marker, linestyle

 

plt.plot(x,y,marker='o', mfc='red', ms=10, mec='blue', ls=':')

 

plt.plot(x,y,alpha=0.1)

 

plt.figure(figsize=(10, 5))
plt.plot(x,y)

 

plt.figure(figsize=(10, 5), dpi=100)
plt.plot(x,y)

 

plt.figure(facecolor='yellow')
plt.plot(x,y)

 

파일

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

x = [1,2,3]
y = [2,4,8]

plt.figure(dpi=200)
plt.plot(x,y)
plt.savefig('graph.png',dpi=100)

 

텍스트

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

x = [1,2,3]
y = [2,4,8]

plt.plot(x,y)

 

plt.plot(x,y,marker='o')
for idx, txt in enumerate(y):
    plt.text(x[idx],y[idx]+0.3,txt,ha='center', color='red')

 

다수의 데이타 표현

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

x = [1,2,3]
y = [2,4,8]

plt.plot(x,y)

 

days = [1,2,3]
az = [2,4,8]
pfizer=[5,1,3]
moderna=[1,2,5]

plt.plot(days, az)
plt.plot(days, pfizer)
plt.plot(days, moderna)

 

plt.plot(days, az, label = 'az',)
plt.plot(days, pfizer, label='pfzer', marker='o', linestyle='--')
plt.plot(days, moderna, label='moderna', marker='s', ls='-.')
plt.legend(ncol=3)

 

막대그래프

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

labels=['강백호', '서태웅', '정대만']
values = [190,187,184]
colors = ['r', 'g', 'b']
plt.bar(labels, values, color=colors, alpha=0.5)

 

labels=['강백호', '서태웅', '정대만']
values = [190,187,184]
colors = ['r', 'g', 'b']
plt.bar(labels, values, color=colors, width=0.5)
plt.ylim(175,195)
plt.xticks(rotation=45)
plt.yticks(rotation=45)

 

labels=['강백호', '서태웅', '정대만']
values = [190,187,184]
colors = ['r', 'g', 'b']
ticks = ['1번학생','2번학생','3번학생']
plt.bar(labels, values, color=colors, width=0.5)
plt.ylim(175,195)
plt.xticks(labels,ticks, rotation=20)

 

막대그래프

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

 

labels=['강백호', '서태웅', '정대만']
values = [190,187,184]
colors = ['r', 'g', 'b']
plt.barh(labels, values, color=colors, alpha=0.5)
plt.xlim(175,195)

 

bar = plt.bar(labels, values)
bar[0].set_hatch('/')
bar[1].set_hatch('x')
bar[2].set_hatch('..')

 

bar = plt.bar(labels, values)
plt.ylim(175, 195)
for idx, rect in enumerate(bar):
    plt.text(idx, rect.get_height()+0.5, values[idx], ha = 'center', color='red')

 

데이타프레임 활용

import pandas as pd

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family']='Malgun Gothic'
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['axes.unicode_minus'] = False

df = pd.read_excel('../Pandas/score.xlsx')
df

 

plt.plot(df['지원번호'],df['키'])

 

plt.plot(df['지원번호'], df['영어'])
plt.plot(df['지원번호'], df['수학'])

 

plt.plot(df['지원번호'], df['영어'])
plt.plot(df['지원번호'], df['수학'])
plt.grid(axis='y', color='purple', alpha=0.3, linestyle='--', linewidth=2)

728x90
LIST