大佬看到这里就可以走了, 小白可以继续看了,本篇文章只是【plotly】入门级
注意:本篇文章使用的是离线版本的plotly,没有用线上版的,直接在pycharm上运行的。
plotly
pip install plotly
plotly
模块import plotly.graph_objects as go
import plotly.offline as of # 这个为离线模式的导入方法
import pandas as pd #使用pandas处理csv数据
Scatter
通过Scatter
方法画折线图:
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\nz_weather.csv')
print(data.head()) #显示5条数据
# Scatter
line1 = go.Scatter(y=data['Auckland'], x=data['DATE'], name='Auckland') # name定义每条线的名称
line2 = go.Scatter(y=data['Wellington'], x=data['DATE'], name='Wellington')
fig = go.Figure([line1, line2])
fig.update_layout(
title = 'New Zealand Weather', #定义生成的plot 的标题
xaxis_title = 'DATE', #定义x坐标名称
yaxis_title = 'Weather' #定义y坐标名称
)
of.plot(fig)
使用数据:
DATE Auckland Christchurch Dunedin Hamilton Wellington
0 2000-01 115.4 47.2 174.8 96.2 91.8
1 2000-02 8.4 25.2 41 8.2 35.2
2 2000-03 57.2 60.8 74.2 33.8 53.4
3 2000-04 106.8 58.2 50 129.6 109.8
4 2000-05 128.2 62.0 '- 98.2 78.2
Bar
通过Bar
画条形图:
# Bar
# 由于数据有很多,我就挑选一年的数据做为示例
data_2010 = data[(data['DATE'] >= '2010-01') & (data['DATE'] < '2011-01')]
bar1 = go.Bar(y=data_2010['Auckland'], x=data_2010['DATE'],
text = data_2010['Auckland'], textposition = 'outside', name='Auckland') #
bar2 = go.Bar(y=data_2010['Wellington'], x=data_2010['DATE'],
text = data_2010['Wellington'], textposition = 'outside', name='Wellington') # textpositon 是将text数据显示在条形柱上, outside是外面, inside是里面 ;name 和上述Scatter一样
fig = go.Figure([bar1,bar2])
fig.update_layout(
title = 'New Zealand Weather',
xaxis_title = 'DATE',
yaxis_title = 'Weather',
)
of.plot(fig)
Histogram
Histogram
统计某列数据的次数:
# Histogram
hist1 = go.Histogram(x=data['Auckland'], xbins={'size': 10}, name='Auckland') # xbins={'size': 10} 是以10为范围,统计数据次数
hist2 = go.Histogram(x=data['Wellington'], xbins={'size': 10}, name='Wellington')
fig = go.Figure([hist1, hist2])
fig.update_layout(
title = 'New Zealand Weather',
xaxis_title = 'Weather',
yaxis_title = 'count',
bargap = 0.1, # 是柱与柱之间存在空隙
)
of.plot(fig)
Express
Express
是一种更方便的制作图表的方法:
import plotly.express as px
Express
来完成example1的方法:# plotly.express
import plotly.express as px
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\iris.csv')
fig = px.scatter(data, x='SepalLength', y='SepalWidth',
color='Name',
)
of.plot(fig)
使用的数据:鸢尾花数据集
SepalLength SepalWidth PetalLength PetalWidth Name
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
pie
通过Express方法生成饼图:
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\pie-charts-with-excel.csv')
print(data.head())
fig = px.pie(data, names=data['Label'], values=data['Values'], title='pie-charts-with-excel',)
of.plot(fig)
使用数据:
Unnamed: 0 Label Values
0 0 ZDS.FY 70
1 1 RPF.JV 20
2 2 ACC.UM 17
3 3 NCY.LC 76
4 4 EIK.JE 99
3D
# 3D
data1 = pd.read_csv(r'C:\Users\Administrator\Desktop\data\3d-line1.csv')
#print(data1.head())
line1 = go.Scatter3d(x=data1['x'], y=data1['y'], z=data1['z'],
mode='markers', marker={'size': 3, 'color': data1['color']})
fig4 = go.Figure(line1)
of.plot(fig4)
fig5 = px.scatter_3d(data1, x='x', y='y', z='z', color='color')
of.plot(fig5)
使用数据:
x y z color
0 100.000000 0.613222 0.734706 0
1 99.238875 0.589852 0.781320 0
2 99.559608 0.599743 0.762566 0
3 97.931425 0.549296 0.859966 0
4 96.837832 0.515613 0.927150 0
Densitymapbox
生成地图数据:
data = pd.read_csv(r'C:\Users\Administrator\Desktop\data\earthquakes.csv')
# print(data.head())
my_map = go.Densitymapbox(lat=data['Latitude'], lon=data['Longitude'], z=data['Magnitude'], radius=4)
fig = go.Figure(my_map)
fig.update_layout(mapbox_style='open-street-map') #必须有这个地图参数
#可以使用的免费地图:"open-street-map", "carto-positron", "carto-darkmatter", "stamen-terrain", "stamen-toner" or "stamen-watercolor"
of.plot(fig)
使用数据:
Date Latitude Longitude Magnitude
0 01/02/1965 19.246 145.616 6.0
1 01/04/1965 1.863 127.352 5.8
2 01/05/1965 -20.579 -173.972 6.2
3 01/08/1965 -59.076 -23.557 5.8
4 01/09/1965 11.938 126.427 5.8
plotly
官方文档:https://plotly.com/python/
结束语:
1.所有的数据都可以在plotly官方的github上找到。
2.如果我有什么讲得不好的地方欢迎大佬指点评论
3.原文链接https://blog.csdn.net/weixin_43347550/article/details/106163458