๐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
Plotting time-series with different variables
[Step1] ์ผ๋จ ํฉ์ณ๋ณด๋ฉด, ๋ญ๊ฐ ๋ถ์กฑํ๋ค!
[Step2] .twinx() : ๋ subplot์ด x์ถ์ ๊ณต์ ํ๋ค
# Create a twin Axes that shares the x-axis
# x์ถ์ ๊ณต์ ํ๋ twin axes๋ฅผ ์์ฑํ๋ค
ax2 = ax.twinx()
[Step3] color keyword arguement : ๊ฐ ๋ณ์์ ์์ ์ง์
[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)
๐Annotating time-series data