-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_plot.py
More file actions
98 lines (74 loc) · 4.32 KB
/
Copy pathbasic_plot.py
File metadata and controls
98 lines (74 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
项目: Plotly基础可视化
数据路径:data/2016-weather-data-seattle.csv, data/iris-data.csv
实现: 折线图、柱状图、饼图、频数直方图、2D散点图
处理: Pandas数据清洗 + Plotly交互式绘图
数据源: 开源CSV(README标注来源)
"""
# 导入依赖:pandas处理数据,plotly.graph_objects绘制交互式图表
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px # 快速极简绘图
# ========== 1.折线图:气温随时间的趋势变化 ==========
# 读取数据
data = pd.read_csv('data/2016-weather-data-seattle.csv')
# 简单清洗
data = data.dropna() # 删除空值
data1 = data.head(1000) # 截取前1000行数据,减少绘图数据量
# 开始画图,构造三条折线,并标出图例:最高温、平均温、最低温
line1 = go.Scatter(x=data1['Date'], y=data1['Max_TemperatureC'], name='Max_TemperatureC')
line2 = go.Scatter(x=data1['Date'], y=data1['Mean_TemperatureC'], name='Mean_TemperatureC')
line3 = go.Scatter(x=data1['Date'], y=data1['Min_TemperatureC'], name='Min_TemperatureC')
# 组合折线放进画布
fig1 = go.Figure([line1, line2, line3])
# 设置图表标题、XY轴名称
fig1.update_layout(title = 'weather line graph', xaxis_title = 'Data', yaxis_title = 'weather temperature')
# 弹出展示交互式图表
fig1.show()
# ========== 2.柱状图:十年的气温 ==========
data2 = data[(data['Date'] >= '1/1/2006') & (data['Date'] <= '1/1/2016')] # 筛选2006.1.1~2016.1.1区间数据用于柱状图绘制
# 分别构造最高、平均、最低气温柱状序列,配置数值标签位置
bar1 = go.Bar(x=data2['Date'], y=data2['Max_TemperatureC'], text=data2['Max_TemperatureC'], textposition='outside', name='Max_TemperatureC')
bar2 = go.Bar(x=data2['Date'], y=data2['Mean_TemperatureC'], text=data2['Mean_TemperatureC'], textposition='outside', name='Mean_TemperatureC')
bar3 = go.Bar(x=data2['Date'], y=data2['Min_TemperatureC'], text=data2['Min_TemperatureC'], textposition='outside', name='Min_TemperatureC')
fig2 = go.Figure([bar1, bar2, bar3])
fig2.update_layout(title = 'weather bar chart', xaxis_title = 'Data', yaxis_title = 'weather temperature')
fig2.show()
# ========== 3.直方图:最高气温分布 ==========
# 绘制最高气温分布直方图,分组区间跨度5℃
hist = go.Histogram(x=data['Max_TemperatureC'], xbins={'size': 5}, texttemplate='%{y}', textposition='outside') # texttemplate='%{y}'显示柱子对应数值
fig3 = go.Figure(hist)
fig3.update_layout(title = 'weather Histogram', xaxis_title = 'Max_TemperatureC', yaxis_title = 'Number of Days', bargap=0.1) # bargap用来调整柱子间隙
fig3.show()
# ========== 4.饼图:最高气温分布 ==========
# 自定义分类:>20℃高温,0~20常温,<0低温
def temp_type(x):
if x > 20:
return 'High Temperature'
elif x < 0:
return 'Low Temperature'
else:
return 'Normal Temperature'
data['Temp_Category'] = data['Mean_TemperatureC'].apply(temp_type) # 新增分类列
data4 = data['Temp_Category'].value_counts() # 统计高温、常温、低温各自的总天数
# 绘制饼图
pie = go.Pie(labels=data4.index, values=data4.values, texttemplate="%{label}: %{percent:.1%}", textposition='outside')
fig4 = go.Figure(pie)
fig4.update_layout(title="Proportion of Days by Temperature Level")
fig4.show()
# ========== 5.2D散点图:鸢尾花二维散点图 ==========
# 读取数据
data5 = pd.read_csv('data/iris-data.csv')
# 处理数据
name_to_color = {'Iris-setosa': 0, 'Iris-setossa': 1, 'Iris-versicolor': 2, 'Iris-virginica': 3, 'versicolor': 4} # 建立品种与数字映射,用于散点颜色区分
data5['color'] = data5['class'].map(name_to_color) # 根据品种映射生成颜色编码列
# 绘制二维散点:x花萼长、y花萼宽,依靠color字段区分点颜色
points = go.Scatter(x = data5['sepal_length_cm'], y = data5['sepal_width_cm'], mode = 'markers', marker={'color': data5['color']})
fig5 = go.Figure(points)
fig5.show()
# px快速绘制2D散点,直接根据品种class自动配色
fig6 = px.scatter(data5, x = data5['sepal_length_cm'], y = data5['sepal_width_cm'], color='class')
fig6.show()
# 绘制特征散点矩阵,4项花特征两两对比,按品种区分颜色
fig7 = px.scatter_matrix(data5, dimensions=['sepal_length_cm', 'sepal_width_cm', 'petal_length_cm', 'petal_width_cm'], color='class')
fig7.show()