Introduction To Data Visualization With Matplotlib Chapter2
Introduction To Data Visualization With Matplotlib Chapter2
data
I N T R O D U C T I O N TO D ATA V I S U A L I Z AT I O N W I T H M AT P LOT L I B
Ariel Rokem
Data Scientist
Time-series data
0 0.10 0 315.71
1 0.01 1 317.45
2 0.08 2 317.50
3 -0.05 3 NaN
4 0.06 4 315.86
5 -0.06 5 314.93
6 -0.03 6 313.20
7 0.04 7 NaN
... ...
701 0.98 701 402.27
702 0.87 702 401.05
703 0.89 703 401.59
704 0.93 704 403.55
705 0.81 705 404.45
Name:co2, Length: 706, dtype: float64 Name:co2, Length: 706, dtype: float64
ax.plot(climate_change.index, climate_change['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
fig, ax = plt.subplots()
ax.plot(sixties.index, sixties['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
fig, ax = plt.subplots()
ax.plot(sixty_nine.index, sixty_nine['co2'])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm)')
plt.show()
Ariel Rokem
Data Scientist
Plotting two time-series together
import pandas as pd
climate_change = pd.read_csv('climate_change.csv',
parse_dates=["date"],
index_col="date")
climate_change
co2 relative_temp
date
1958-03-06 315.71 0.10
1958-04-06 317.45 0.01
1958-07-06 315.86 0.06
... ... ...
2016-11-06 403.55 0.93
2016-12-06 404.45 0.81
ax.plot(climate_change.index, climate_change["relative_temp"])
ax.set_xlabel('Time')
ax.set_ylabel('CO2 (ppm) / Relative temperature')
plt.show()
ax2 = ax.twinx()
ax2.plot(climate_change.index, climate_change["relative_temp"])
ax2.set_ylabel('Relative temperature (Celsius)')
plt.show()
ax2 = ax.twinx()
ax2.plot(climate_change.index, climate_change["relative_temp"],
color='red')
ax2.set_ylabel('Relative temperature (Celsius)', color='red')
plt.show()
ax.tick_params('y', colors='blue')
ax2 = ax.twinx()
ax2.plot(climate_change.index,
climate_change["relative_temp"],
color='red')
ax2.set_ylabel('Relative temperature (Celsius)',
color='red')
ax2.tick_params('y', colors='red')
plt.show()
ax2 = ax.twinx()
plot_timeseries(ax, climate_change.index,
climate_change['relative_temp'],
'red', 'Time', 'Relative temperature (Celsius)')
plt.show()
Ariel Rokem
Data Scientist
Time-series data
plt.show()