๋ชฉ์ฐจ
In [1]: import pandas as pd
In [2]: import matplotlib.pyplot as plt
In [3]: air_quality = pd.read_csv("data/air_quality_no2.csv", index_col=0, parse_dates=True)
In [4]: air_quality.head()
Out[4]:
station_antwerp station_paris station_london
datetime
2019-05-07 02:00:00 NaN NaN 23.0
2019-05-07 03:00:00 50.5 25.0 19.0
2019-05-07 04:00:00 45.0 27.7 19.0
2019-05-07 05:00:00 NaN 50.4 16.0
2019-05-07 06:00:00 NaN 61.9 NaN
index_col ๋ฐ parse_dates ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ read_csv ํจ์์์ ๊ฒฐ๊ณผ์ ์ผ๋ก ์์ฑ๋๋ DataFrame์ ์ธ๋ฑ์ค๋ก ์ฒซ ๋ฒ์งธ(0๋ฒ์งธ) ์ด์ ์ ์ํ๊ณ , ์ด์ ์๋ ๋ ์ง๋ฅผ ๊ฐ๊ฐ Timestamp ๊ฐ์ฒด๋ก ๋ณํํ๋ค
1) ๋ฐ์ดํฐ๋ฅผ ๋น ๋ฅด๊ฒ ์๊ฐ์ ์ผ๋ก ํ์ธํ๊ธฐ
In [5]: air_quality.plot()
Out[5]: <AxesSubplot: xlabel='datetime'>
In [6]: plt.show()
DataFrame์์ ํ๋ค์ค๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ซ์ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฐ ์ด์ ๋ํด ํ๋์ ์ ๊ทธ๋ํ(line plot)๋ฅผ ์์ฑํ๋ค.
2) 'Paris'์์ ๋์จ ๋ฐ์ดํฐ๋ง ํฌํจํ๋ ๋ฐ์ดํฐ ํ ์ด๋ธ์ ์ด๋ง ๊ทธ๋ฆฌ๊ธฐ
In [7]: air_quality["station_paris"].plot()
Out[7]: <AxesSubplot: xlabel='datetime'>
In [8]: plt.show()
ํน์ ์ด์ ๊ทธ๋ฆฌ๋ ค๋ฉด, ๋ถ๋ถ ๋ฐ์ดํฐ ์ ํ ๋ฐฉ๋ฒ๊ณผ plot() ๋ฉ์๋๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ค.
๋ฐ๋ผ์ plot() ๋ฉ์๋๋ Series์ DataFrame ๋ชจ๋์์ ์๋ํ๋ค.
3) ๋ฐ๋๊ณผ ํ๋ฆฌ์์ ์ธก์ ๋ ๊ฐ์ ์๊ฐ์ ์ผ๋ก ๋น๊ตํ๊ธฐ
In [9]: air_quality.plot.scatter(x="station_london", y="station_paris", alpha=0.5)
Out[9]: <AxesSubplot: xlabel='station_london', ylabel='station_paris'>
In [10]: plt.show()
plot ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ธฐ๋ณธ ์ ๊ทธ๋ํ ์ธ์๋ ๋ฐ์ดํฐ๋ฅผ ํ๋กฏํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ ์ฌ๋ฌ ๋ฐฉ๋ฒ์ด ์๋ค.
In [11]: [
....: method_name
....: for method_name in dir(air_quality.plot)
....: if not method_name.startswith("_")
....: ]
....:
Out[11]:
['area',
'bar',
'barh',
'box',
'density',
'hexbin',
'hist',
'kde',
'line',
'pie',
'scatter']
์ด ์ฝ๋๋ air_quality.plot ๊ฐ์ฒด์์ ์์ํ๋ "_"๋ก ์์ํ์ง ์๋ ๋ชจ๋ ๋ฉ์๋ ์ด๋ฆ์ ์ฐพ์ ๋ฆฌ์คํธ๋ก ๋ฐํํ๋ค.
'area': ๋ฉด์ ๊ทธ๋ํ
'bar': ๋ง๋ ๊ทธ๋ํ(์์ง ๋ฐฉํฅ)
'barh': ์ํ ๋ง๋ ๊ทธ๋ํ(์ํ ๋ฐฉํฅ)
'box': ๋ฐ์ค ํ๋กฏ
'density': ๋ฐ๋ ๊ทธ๋ํ
'hexbin': ์ก๊ฐํ ๊ตฌ๊ฐ ๊ทธ๋ํ
'hist': ํ์คํ ๊ทธ๋จ
'kde': ์ปค๋ ๋ฐ๋ ์ถ์ ๊ทธ๋ํ
'line': ์ ๊ทธ๋ํ(๊ธฐ๋ณธ)
'pie': ์(ํ์ด) ๊ทธ๋ํ
'scatter': ์ฐ์ ๋ ๊ทธ๋ํ
๋ง์ ๊ฐ๋ฐ ํ๊ฒฝ์์ IPython๊ณผ Jupyter Notebook๊ณผ ๊ฐ์ด TAB ๋ฒํผ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฉ์๋์ ๋ํ ๊ฐ์๋ฅผ ์ป์ ์ ์๋ค. ์๋ฅผ ๋ค์ด, air_quality.plot. + TAB์ ๋๋ค.
์ต์ ์ค ํ๋๋ DataFrame.plot.box()์ธ๋ฐ, ์ด๋ ๋ฐ์ค ํ๋กฏ์ ์๋ฏธํฉ๋๋ค. ๋ฐ์ค ๋ฉ์๋๋ ๊ณต๊ธฐ ์ง ์์ ๋ฐ์ดํฐ์ ์ ์ฉ ๊ฐ๋ฅํ๋ค.
In [12]: air_quality.plot.box()
Out[12]: <AxesSubplot: >
In [13]: plt.show()
4) ๊ฐ ์ด์ ๋ณ๋์ ์๋ธํ๋กฏ์ ๊ทธ๋ฆฌ๊ธฐ
In [14]: axs = air_quality.plot.area(figsize=(12, 4), subplots=True)
In [15]: plt.show()
ํ๋กฏ ํจ์์์ ์ ๊ณต๋๋ subplots ์ธ์๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ ๋ฐ์ดํฐ ์ด์ ๋ํ ๋ณ๋์ ์๋ธํ๋กฏ์ ์ง์ํ ์ ์๋ค. ํ๋ค์ค ๋ด์ฅ ํ๋กฏ ํจ์์์ ๊ฐ๋ฅํ ์ต์ ๋ค์ ๋ชจ๋ ๊ฒํ ํ๋ ๊ฒ์ด ์ข๋ค.
5) ๊ทธ๋ํ๋ฅผ ์ปค์คํฐ๋ง์ด์ฆ, ํ์ฅํ์ฌ ์ ์ฅํ๊ธฐ
In [16]: fig, axs = plt.subplots(figsize=(12, 4))
In [17]: air_quality.plot.area(ax=axs)
Out[17]: <AxesSubplot: xlabel='datetime'>
In [18]: axs.set_ylabel("NO$_2$ concentration")
Out[18]: Text(0, 0.5, 'NO$_2$ concentration')
In [19]: fig.savefig("no2_concentrations.png")
In [20]: plt.show()
ํ๋ค์ค์์ ์์ฑํ ๊ฐ ํ๋กฏ ๊ฐ์ฒด๋ Matplotlib ๊ฐ์ฒด๋ค. Matplotlib๋ ๋ค์ํ ์ต์ ์ ์ ๊ณตํ์ฌ ๊ทธ๋ํ๋ฅผ ์ปค์คํฐ๋ง์ด์ง ํ ์ ์๊ธฐ ๋๋ฌธ์, ํ๋ค์ค์ Matplotlib ๊ฐ์ ๊ด๊ณ๋ฅผ ๋ช ํํ ํจ์ผ๋ก์จ Matplotlib์ ๋ชจ๋ ๊ธฐ๋ฅ์ ๊ทธ๋ํ์ ์ ์ฉํ ์ ์๋ค. ์ด ์ ๋ต์ ์ด์ ์์์์ ์ฌ์ฉ๋์๋ค.
fig, axs = plt.subplots(figsize=(12, 4)) # Create an empty Matplotlib Figure and Axes
air_quality.plot.area(ax=axs) # Use pandas to put the area plot on the prepared Figure/Axes
axs.set_ylabel("NO$_2$ concentration") # Do any Matplotlib customization you like
fig.savefig("no2_concentrations.png") # Save the Figure/Axes using the existing Matplotlib method.
plt.show() # Display the plot
์ ๋ฆฌ
.plot.* ๋ฉ์๋๋ Series์ DataFrames ๋ชจ๋์ ์ ์ฉ ๊ฐ๋ฅ
๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ ์ด์ ๋ค๋ฅธ ์์(์ , ๋ฐ์คํ๋กฏ, ...)๋ก ํ์
ํ๋ค์ค์์ ์์ฑ๋ ๋ชจ๋ ๊ทธ๋ํ๋ Matplotlib ๊ฐ์ฒด
๋ณธ ๋ด์ฉ์ ๊ณต๋ถ ๊ธฐ๋ก์ฉ์ผ๋ก ์ถ์ฒ๋ ํ๋ค์ค์ ๊ณต์๋ฌธ์(How do I create plots in pandas?) ์ ๋๋ค