Python - Datacamp - Introduction to Matplotlib

Python - Datacamp - Introduction to Matplotlib

Datacamp: https://www.datacamp.com/

# 1.py 基本matplotlib.pyplot子模块入门

# Import the matplotlib.pyplot submodule and name it plt
import matplotlib.pyplot as plt

# Create a Figure and an Axes with plt.subplots
fig, ax = plt.subplots()  

# Call the show function 
plt.show()

 

# 2.py 添加数据

import matplotlib.pyplot as plt

# 月份
MONTH = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# 降雨量
rainfall = [44.10, 71.08, 93.36, 184.63, 286.79, 318.60, 238.22, 233.75, 194.35, 68.65, 38.40, 29.32] 

# 数据
data = {}
data['MONTH'] = MONTH
data['rainfall'] = rainfall

# 创建数据以及轴 
fig, ax = plt.subplots()

# ax: (x, y)
ax.plot(data['MONTH'], data['rainfall'])

# 显示
plt.show()

 

# 3.py 自定义显示

import matplotlib.pyplot as plt

# 月份
MONTH = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# 降雨量
rainfall = [44.10, 71.08, 93.36, 184.63, 286.79, 318.60, 238.22, 233.75, 194.35, 68.65, 38.40, 29.32] 

# 数据
data = {}
data['MONTH'] = MONTH
data['rainfall'] = rainfall

# 创建数据以及轴 
fig, ax = plt.subplots()

# ax: (x, y)
ax.plot(data['MONTH'], data['rainfall'], color='b', marker='*', linestyle='--')

# 显示
plt.show()

 

# 4.py 自定义标签以及抬头

import matplotlib.pyplot as plt

# 月份
MONTH = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# 降雨量
rainfall = [44.10, 71.08, 93.36, 184.63, 286.79, 318.60, 238.22, 233.75, 194.35, 68.65, 38.40, 29.32] 

# 数据
data = {}
data['MONTH'] = MONTH
data['rainfall'] = rainfall

# 创建数据以及轴 
fig, ax = plt.subplots()

# ax: (x, y)
ax.plot(data['MONTH'], data['rainfall'], color='b', marker='*', linestyle='--')

# 标签
ax.set_xlabel('Months')
ax.set_ylabel('Rainfall in CAN')

# 标题
ax.set_title('Weather in CAN')

# 显示
plt.show()

 

# 5.py 多重图表

import matplotlib.pyplot as plt

# 月份
MONTH_F = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
MONTH_L = ['Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# 降雨量
rainfall_canton_F = [44.10, 71.08, 93.36, 184.63, 286.79, 318.60] 
rainfall_shanghai_F = [61.79, 59.40, 94.99, 83.19, 92.78, 173.57]
rainfall_canton_L = [238.22, 233.75, 194.35, 68.65, 38.40, 29.32]
rainfall_shanghai_L = [148.84, 193.88, 109.60, 62.44, 54.14, 37.71]

# 创建数据以及轴 
fig, ax = plt.subplots(2, 2)

# ax: (x, y)
ax[0,0].plot(MONTH_F, rainfall_canton_F, color='b', marker='*', linestyle='--')
ax[0,1].plot(MONTH_F, rainfall_shanghai_F, color='r', marker='o', linestyle='--')
ax[1,0].plot(MONTH_L, rainfall_canton_L, color='b', marker='*', linestyle='--')
ax[1,1].plot(MONTH_L, rainfall_shanghai_L, color='r', marker='o', linestyle='--')

# 标签
ax[0,0].set_xlabel('Months')
ax[0,0].set_ylabel('Rainfall in Canton')
ax[0,1].set_xlabel('Months')
ax[0,1].set_ylabel('Rainfall in Shanghai')

# 标题
ax[0,0].set_title('Weather in Canton')
ax[0,1].set_title('Weather in Shanghai')
# 显示
plt.show()

 

# 6.py small multiples with shared y axis

import matplotlib.pyplot as plt

# 月份
MONTH = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# 降雨量
rainfall = [44.10, 71.08, 93.36, 184.63, 286.79, 318.60, 238.22, 233.75, 194.35, 68.65, 38.40, 29.32] 
# 温度
temperature = [10, 14, 20, 22, 19, 25, 30, 31, 32, 29, 15, 16]

# 数据
data = {}
data['MONTH'] = MONTH
data['rainfall'] = rainfall
data['temperature'] = temperature

# Create a figure and an array of axes: 2 rows, 1 column with shared y axis
fig, axes = plt.subplots(2, 1, sharey=True)

axes[0].plot(data['MONTH'], data['rainfall'], color='b')
axes[0].plot(data['MONTH'], data['temperature'], color='b', linestyle='--')

axes[1].plot(data['MONTH'], data['rainfall'], color='b')
axes[1].plot(data['MONTH'], data['temperature'], color='b', linestyle='--')

plt.show()

 

上一篇:4 - 规约性 - Reducibility


下一篇:数据清洗--1. Introduction of Data Wrangling