データ分析と可視化

20731 ワード

11.累積棒グラフ
import pandas as pd

import matplotlib.pyplot as plt

import matplotlib  
matplotlib.rcParams['font.family'] = 'Malgun Gothic' #windows
matplotlib.rcParams['font.size'] = 15
matplotlib.rcParams['axes.unicode_minus'] = False

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

df
plt.bar(df['이름'], df['국어'], label='국어')
plt.bar(df['이름'], df['영어'], bottom=df['국어'], label='영어')

plt.ylim(0, 250)
plt.legend()
#plt.bar(df['이름'], df['국어'], label='국어')
plt.bar(df['이름'], df['영어'], bottom=df['국어'], label='영어')

plt.ylim(0, 250)
plt.legend()
plt.bar(df['이름'], df['국어'], label='국어')
plt.bar(df['이름'], df['영어'], bottom = df['국어'], label='영어')
plt.bar(df['이름'], df['수학'], bottom = df['국어'] + df['영어'], label='수학')

#plt.ylim(0, 250)
plt.xticks(rotation=60)
plt.legend()

12.複数の棒グラフ
import pandas as pd

import matplotlib.pyplot as plt

import matplotlib  
matplotlib.rcParams['font.family'] = 'Malgun Gothic' #windows
matplotlib.rcParams['font.size'] = 15
matplotlib.rcParams['axes.unicode_minus'] = False

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

df
import numpy as np

np.arange(5)

>>> array([0, 1, 2, 3, 4])

np.arange(3,6)

>>> array([3, 4, 5])

arr = np.arange(5)
arr

>>> array([0, 1, 2, 3, 4])

arr+100

>>> array([100, 101, 102, 103, 104])

arr*3

>>> array([ 0,  3,  6,  9, 12])

df.shape[0]

>>> 8

N = df.shape[0]
N

>>> 8

index = np.arange(N)
index

>>> array([0, 1, 2, 3, 4, 5, 6, 7])

w =0.25
plt.bar(index - w, df['국어'])
#x의 위치가 약간 옆으로 가있음
plt.bar(index, df['영어'])
plt.bar(index + w, df['수학'])
w =0.25
plt.bar(index - w, df['국어'], width=w, label= '국어')
plt.bar(index, df['영어'], width=w, label= '영어')
plt.bar(index + w, df['수학'], width=w, label= '수학' )

plt.legend(ncol=3)
plt.figure(figsize=(10, 5))
plt.title('학생별 성적')

w =0.25
plt.bar(index - w, df['국어'], width=w, label= '국어')
plt.bar(index, df['영어'], width=w, label= '영어')
plt.bar(index + w, df['수학'], width=w, label= '수학' )

plt.legend(ncol=3)
plt.xticks(index, df['이름'], rotation=60)
plt.show()