官网例子
plotly官网
https://github.com/tpof314/plotly_examples
安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple plotly
import plotly
plotly.__version__
import pandas as pd
data = pd.read_csv('C:\\Users\\Administrator\\Desktop\\plotly_examples-master\\data\\nz_weather.csv')
data.head()
import plotly.graph_objects as go
line1 = go.Scatter(x=data['DATE'], y=data['Auckland'], name='Auckland')
fig = go.Figure(line1)
fig.show()
line1 = go.Scatter(x=data['DATE'], y=data['Auckland'], name='Auckland')
line2 = go.Scatter(x=data['DATE'], y=data['Wellington'], name='Wellington')
fig = go.Figure([line1, line2])
fig.update_layout(
title = "New Zealand Weather",
xaxis_title = "Date",
yaxis_title = "Weather"
)
fig.show()
data_2010 = data[(data['DATE'] >= '2010-01') & (data['DATE'] < '2011-01')]
data_2010
bar1 = go.Bar(x = data_2010['DATE'], y = data_2010['Auckland'],
text = data_2010['Auckland'], textposition='outside')
fig = go.Figure(bar1)
fig.show()
bar1 = go.Bar(x = data_2010['DATE'], y = data_2010['Auckland'],
text = data_2010['Auckland'], textposition='outside', name='Auckland')
bar2 = go.Bar(x = data_2010['DATE'], y = data_2010['Wellington'],
text = data_2010['Wellington'], textposition='outside', name='Wellington')
fig = go.Figure([bar1, bar2])
fig.show()
hist = go.Histogram(x = data['Auckland'])
fig = go.Figure(hist)
fig.update_layout()
fig.show()
hist = go.Histogram(x = data['Auckland'])
fig = go.Figure(hist)
fig.update_layout(bargap=0.1)
fig.show()
hist = go.Histogram(x = data['Auckland'], xbins={'size': 10})
fig = go.Figure(hist)
fig.update_layout(bargap=0.1)
fig.show()
import plotly.graph_objects as go
import pandas as pd
data = pd.read_csv('./data/iris.csv')
data.head()
points = go.Scatter(x = data['SepalLength'], y = data['SepalWidth'], mode='markers')
fig = go.Figure(points)
fig.show()
import plotly.express as px
fig = px.scatter(data, x = 'SepalLength', y = 'SepalWidth',
color='Name')
fig.show()
fig = px.scatter_matrix(data, dimensions=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'],
color='Name')
fig.show()
import plotly.graph_objects as go
import pandas as pd
data = pd.read_csv('C:\\Users\\Administrator\\Desktop\\plotly_examples-master\\data\\iris.csv')
data.head()
line = go.Scatter(x = data['x'], y = data['y'])
fig = go.Figure(line)
fig.show()
line = go.Scatter3d(x = data['x'], y = data['y'], z = data['z'])
fig = go.Figure(line)
fig.show()
line = go.Scatter3d(x = data['x'], y = data['y'], z = data['z'], mode='markers')
fig = go.Figure(line)
fig.show()
line = go.Scatter3d(x = data['x'], y = data['y'], z = data['z'],
mode='markers', marker={'size': 3, 'color': 'red'})
fig = go.Figure(line)
fig.show()
import plotly.express as px
fig = px.scatter_3d(data, x='x', y='y', z='z', color='color')
fig.show()
import plotly.graph_objects as go
import pandas as pd
data = pd.read_csv('C:\\Users\\Administrator\\Desktop\\plotly_examples-master\\data\\mt_bruno_elevation.csv')
del data['index']
height = data.values
surface = go.Surface(z = height)
fig = go.Figure(surface)
fig.show()
import numpy as np
x = np.arange(-5, 6)
y = np.arange(-5, 6)
xv, yv = np.meshgrid(x, y)
xv
'''
array([[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5],
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]])
'''
yv
'''
array([[-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5],
[-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4],
[-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3],
[-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]])
'''
z = xv**2 + yv**2
z
'''
array([[50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50],
[41, 32, 25, 20, 17, 16, 17, 20, 25, 32, 41],
[34, 25, 18, 13, 10, 9, 10, 13, 18, 25, 34],
[29, 20, 13, 8, 5, 4, 5, 8, 13, 20, 29],
[26, 17, 10, 5, 2, 1, 2, 5, 10, 17, 26],
[25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25],
[26, 17, 10, 5, 2, 1, 2, 5, 10, 17, 26],
[29, 20, 13, 8, 5, 4, 5, 8, 13, 20, 29],
[34, 25, 18, 13, 10, 9, 10, 13, 18, 25, 34],
[41, 32, 25, 20, 17, 16, 17, 20, 25, 32, 41],
[50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50]])
'''
surface = go.Surface(x = xv, y = yv, z = z)
fig = go.Figure(surface)
fig.show()
import plotly.graph_objects as go
import pandas as pd
data = pd.read_csv('C:\\Users\\Administrator\\Desktop\\plotly_examples-master\\data\\earthquakes.csv')
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")
fig.show()
import plotly.express as px
# 准备数据
df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")
df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # Represent only large countries
df.head()
fig = px.pie(df, values='pop', names='country', title='Population of European continent')
fig.show()
import plotly.express as px
# This dataframe has 244 lines, but 4 distinct values for `day`
df = px.data.tips()
df
fig = px.pie(df, values='tip', names='day')
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day', color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()
import plotly.graph_objects as go
fig =go.Figure(go.Sunburst(
labels=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
))
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill')
fig.show()