Matplotlib
数据集
站点编号 | 日期 | 时刻 | 进站人数 | 出站人数 |
155 | 2015-10-01 | 7 | 294 | 1215 |
155 | 2015-10-01 | 8 | 1128 | 4067 |
155 | 2015-10-01 | 9 | 1441 | 3713 |
155 | 2015-10-01 | 10 | 2043 | 2976 |
155 | 2015-10-01 | 11 | 2678 | 3198 |
155 | 2015-10-01 | 12 | 2515 | 2804 |
155 | 2015-10-01 | 13 | 2313 | 2396 |
155 | 2015-10-01 | 14 | 1767 | 2680 |
155 | 2015-10-01 | 15 | 1873 | 2202 |
155 | 2015-10-01 | 16 | 1860 | 2350 |
155 | 2015-10-01 | 17 | 2348 | 1987 |
155 | 2015-10-01 | 18 | 1136 | 1982 |
155 | 2015-10-01 | 19 | 2125 | 1442 |
155 | 2015-10-01 | 20 | 1066 | 930 |
155 | 2015-10-01 | 21 | 1273 | 441 |
155 | 2015-10-01 | 22 | 999 | 323 |
155 | 2015-10-01 | 24 | 49 | 194 |
155 | 2015-10-02 | 7 | 130 | 648 |
155 | 2015-10-02 | 8 | 496 | 2611 |
(1)散点图绘制
读取“各站点各时刻进出站客流数据.xlsx”,绘制站点155各时刻进站客流散点图。
代码:
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
data = pd.read_excel('各站点各时刻进出站客流数据.xlsx')
# 提取站点155的进站客流数据
station_155_data = data[data['站点编号'] == 155]
# 绘制散点图
plt.figure(figsize=(12, 6))
plt.scatter(station_155_data['时刻'], station_155_data['进站人数'],color='red', marker='o')
plt.title('Station 155 Inbound Passenger Flow')
plt.xlabel('Time')
plt.ylabel('Inbound Passenger Flow')
plt.grid(True)
plt.xticks(rotation=45)
plt.show()
(2)线性图绘制
读取“各站点各时刻进出站客流数据.xlsx”,绘制站点157各时刻进站客流线形图。
代码:
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
data = pd.read_excel('各站点各时刻进出站客流数据.xlsx')
# 筛选出站点157的数据
station_157_data = data[data['站点编号'] == 157]
# 绘制线性图
plt.figure(figsize=(12, 6))
plt.plot(station_157_data['时刻'], station_157_data['进站人数'], marker='o', color='b', linestyle='-')
plt.title('Station 157 Inbound Passenger Flow at Different Times')
plt.xlabel('Time')
plt.ylabel('Inbound Passenger Count')
plt.grid(True)
plt.show()
(3)柱状图绘制
读取“各站点各时刻进出站客流数据.xlsx”,绘制站点157各时刻进站客流柱状图。
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
data = pd.read_excel('各站点各时刻进出站客流数据.xlsx')
# 筛选出站点157的数据
station_157_data = data[data['站点编号'] == 157]
# 绘制柱状图并增加数据标注
plt.figure(figsize=(12, 6))
bars = plt.bar(station_157_data['时刻'], station_157_data['进站人数'], color='b', alpha=0.7)
plt.title('Station 157 Inbound Passenger Flow at Different Times')
plt.xlabel('Time')
plt.ylabel('Inbound Passenger Count')
plt.grid(axis='y')
# 添加数据标注
for bar in bars:
yval = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2, yval, round(yval, 1), ha='center', va='bottom')
(4)直方图绘制
读取“各站点各时刻进出站客流数据.xlsx”,绘制站点157各时刻进站客流直方图。
代码:
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
data = pd.read_excel('各站点各时刻进出站客流数据.xlsx')
# 筛选出站点157的数据
station_157_data = data[data['站点编号'] == 157]
# 绘制柱状图并增加数据标注
plt.figure(figsize=(12, 6))
bars = plt.bar(station_157_data['时刻'], station_157_data['进站人数'], color='b', alpha=0.7)
plt.title('Station 157 Inbound Passenger Flow at Different Times')
plt.xlabel('Time')
plt.ylabel('Inbound Passenger Count')
plt.grid(axis='y')
# 添加数据标注
for bar in bars:
yval = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2, yval, round(yval, 1), ha='center', va='bottom')
(5)饼图绘制
读取 “各站点各时刻进出站客流数据.xlsx”,绘制站点157各时刻进站客流饼图。
代码:
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
data = pd.read_excel('各站点各时刻进出站客流数据.xlsx')
# 筛选出站点157的数据并选择前10行
station_157_data = data[data['站点编号'] == 157].head(10)
# 绘制饼图
plt.figure(figsize=(8, 8))
plt.pie(station_157_data['进站人数'], labels=station_157_data['时刻'], autopct='%.1f%%', startangle=90)
plt.title('Inbound Passenger Flow at Station 157 (Top 10 Entries)')
plt.axis('equal')
plt.show()
(6)箱线图绘制
读取“各站点各时刻进出站客流数据.xlsx”,绘制各站点在9时刻进站客流的箱线图。
代码:
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
data = pd.read_excel('各站点各时刻进出站客流数据.xlsx')
# 筛选出所有站点在9时刻的进站客流数据
data_9am = data[data['时刻'] == 9]
# 提取进站客流数据列
inbound_data_9am = data_9am['进站人数']
# 绘制箱线图
plt.figure(figsize=(12, 6))
plt.boxplot(inbound_data_9am, labels=['9am Inbound Passenger Flow'], patch_artist=True, showmeans=True)
plt.title('Box Plot of Inbound Passenger Flow at 9am for All Stations')
plt.ylabel('Inbound Passenger Count')
plt.grid(axis='y')
plt.show()
(7)子图绘制
读取“各站点各时刻进出站客流数据.xlsx”,将155、157、151、123四个站点在各时刻的进站客流,用一个2*2的子图,绘制其线性图。
代码:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 读取 Excel 文件
data = pd.read_excel('各站点各时刻进出站客流数据.xlsx')
# 筛选出指定四个站点的数据
selected_stations = [155, 157, 151, 123]
station_data = data[data['站点编号'].isin(selected_stations)]
# 创建一个2*2的子图
fig, axs = plt.subplots(2, 2, figsize=(12, 8))
for idx, station in enumerate(selected_stations):
row = idx // 2
col = idx % 2
station_data_selected = station_data[station_data['站点编号'] == station].head(10)
axs[row, col].plot(station_data_selected['时刻'], station_data_selected['进站人数'], marker='o')
axs[row, col].set_title(f'车站 {station} - 入站客流量')
axs[row, col].set_xlabel('时间')
axs[row, col].set_ylabel('入站客流量')
# 添加数据标签
for i, j in zip(station_data_selected['时刻'], station_data_selected['进站人数']):
axs[row, col].annotate(str(j), xy=(i, j), xytext=(5, 5), textcoords='offset points')
plt.rcParams['font.sans-serif']=['SimHei']
plt.tight_layout()
plt.show()