安装 plotly
pip install plotly
导入plotly
模块
import plotly.graph_objects as go
import plotly.offline as of # 这个为离线模式的导入方法
import pandas as pd #使用pandas处理csv数据
example1 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