๐Ÿ“ŠMatplotlib ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” ๊ธฐ์ดˆ Plotting time-series data

2020. 9. 12. 10:27ใ†Learning archive/Data Science

Image credits : datacamp.com

 

CONTENTS 

1. ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” (Plotting time-series data)
2. ์‹œ๊ณ„์—ด ์ƒ ์„œ๋กœ ๋‹ค๋ฅธ ๋ณ€์ˆ˜ ์‹œ๊ฐํ™” (Plotting time-series data with different variables)
3. ๊ทธ๋ž˜ํ”„์˜ ํŠน์ • ๋ถ€๋ถ„ ๊ฐ•์กฐํ•˜๊ธฐ : ํ…์ŠคํŠธ, ํ™”์‚ดํ‘œ Annotating time-series data 

 

๐Ÿ“Œ ์˜ค๋Š˜์˜ ๋ชฉํ‘œ ๊ทธ๋ž˜ํ”„! 

 

 

๐Ÿ“Œ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ Time series data

 

 

 

๐Ÿ“Œ์˜ˆ์ œ์—ฐ์Šต : Read data with a time index  

 

To designate the index as a DateTimeIndex, you will use the parse_dates and index_col key-word arguments both to parse this column as a variable that contains dates and also to designate it as the index for this DataFrame.

# Import pandas
import pandas as pd

# Read the data from file using read_csv
climate_change = pd.read_csv('climate_change.csv', parse_dates=["date"], index_col="date")

 

 

 

๐Ÿ“ŒZoom in 

Image credit datacamp.com

 

 

 

 

Plotting time-series with different variables 

[Step1] ์ผ๋‹จ ํ•ฉ์ณ๋ณด๋ฉด, ๋ญ”๊ฐ€ ๋ถ€์กฑํ•˜๋‹ค! 

scale, color๋“ฑ์ด ์„ค์ •๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ. 

 

[Step2] .twinx() : ๋‘ subplot์ด x์ถ•์„ ๊ณต์œ ํ•œ๋‹ค 

# Create a twin Axes that shares the x-axis
# x์ถ•์„ ๊ณต์œ ํ•˜๋Š” twin axes๋ฅผ ์ƒ์„ฑํ•œ๋‹ค 
ax2 = ax.twinx() 

 

Image credit datacamp.com

 

 

[Step3] color keyword arguement : ๊ฐ ๋ณ€์ˆ˜์˜ ์ƒ‰์„ ์ง€์ •

Image credit datacamp.com

[Step4] .tick_params() ๋ฉ”์„œ๋“œ๋กœ Tick์— ์ƒ‰์„ ์„ค์ •

๐Ÿ“Œ๋ณ€์ˆ˜ 2๊ฐœ ์‹œ๊ฐํ™” ์˜ˆ์ œ 

 

(์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋ฌผ!) 

import matplotlib.pyplot as plt

# Initalize a Figure and Axes
fig, ax = plt.subplots()

# Plot the CO2 variable in blue 
ax.plot(climate_change.index, climate_change["co2"], color='blue')

# Create a twin Axes that shares the x-axis
ax2 = ax.twinx()

# Plot the relative temperature in red
ax2.plot(climate_change.index, climate_change["relative_temp"], color='red')

plt.show()

 

 

 

 

๐Ÿ“Œ ํ•จ์ˆ˜ : ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๊ฐํ™”ํ•˜๋Š” ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ 

# Define a function called plot_timeseries
def plot_timeseries(axes, x, y, color, xlabel, ylabel):

  # Plot the inputs x,y in the provided color
  axes.plot(x,y, color=color)

  # Set the x-axis label
  axes.set_xlabel(xlabel)

  # Set the y-axis label
  axes.set_ylabel(ylabel, color=color)

  # Set the colors tick params for y-axis
  axes.tick_params('y', colors=color)

Image credit datacamp.com

 

 

 

๐Ÿ“ŒAnnotating time-series data