Gallery Basic1
2020-11-26 本文已影响0人
数科每日
Point Scatter
image.pngimport plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
X, Y Scatter Plot
image.png# x and y given as DataFrame columns
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()
Bubble Chart
image.pngimport plotly.express as px
df = px.data.iris()
df.head()
fig = px.scatter(df, x="sepal_width",
y="sepal_length",
color="species",
size='petal_length',
hover_data=['petal_width'])
fig.show()
Function Draw
image.pngimport plotly.express as px
import numpy as np
t = np.linspace(0, 2*np.pi, 100)
fig = px.line(x=t, y=np.cos(t), labels={'x':'t', 'y':'cos(t)'})
fig.show()
Multline Plot
image.pngimport plotly.express as px
df = px.data.gapminder().query("continent == 'Oceania'")
df.head()
fig = px.line(df, x='year', y='lifeExp', color='country')
fig.show()
Dot line Plot
image.pngimport plotly.graph_objects as go
import numpy as np
N = 1000
t = np.linspace(0, 10, 100)
y = np.sin(t)
fig = go.Figure(data=go.Scatter(x=t, y=y, mode='markers'))
fig.show()
Multiple Line Style
image.pngimport plotly.graph_objects as go
# Create random data with numpy
import numpy as np
np.random.seed(1)
N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5
fig = go.Figure()
# Add traces
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
mode='markers',
name='markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
mode='lines+markers',
name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
mode='lines',
name='lines'))
fig.show()
Use size and color
image.pngimport plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(
x=[1, 2, 3, 4],
y=[10, 11, 12, 13],
mode='markers',
marker=dict(size=[40, 60, 80, 100],
color=[0, 1, 2, 3])
))
fig.show()
Update Figure
image.pngimport plotly.graph_objects as go
import numpy as np
t = np.linspace(0, 10, 100)
fig = go.Figure()
fig.add_trace(go.Scatter(
x=t, y=np.sin(t),
name='sin',
mode='markers',
marker_color='rgba(152, 0, 0, .8)'
))
fig.add_trace(go.Scatter(
x=t, y=np.cos(t),
name='cos',
marker_color='rgba(255, 182, 193, .9)'
))
# Set options common to all traces with fig.update_traces
fig.update_traces(mode='markers', marker_line_width=2, marker_size=10)
fig.update_layout(title='Styled Scatter',
yaxis_zeroline=False, xaxis_zeroline=False)
fig.show()
Gradually changed color with y-value
image.pngimport plotly.graph_objects as go
import pandas as pd
# 1. Point color is accordence with y-value
data= pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv")
fig = go.Figure(data=go.Scatter(x=data['Postal'],
y=data['Population'],
mode='markers',
marker_color=data['Population'],
text=data['State'])) # hover text goes here
fig.update_layout(title='Population of USA States')
fig.show()
Plotly colorscale
image.pngimport plotly.graph_objects as go
import numpy as np
fig = go.Figure(data=go.Scatter(
y = np.random.randn(500),
mode='markers',
marker=dict(
size=16,
color=np.random.randn(500), #set color equal to a variable
colorscale='Viridis', # one of plotly colorscales
showscale=True
)
))
fig.show()
Plot Large Dataset
image.pngimport plotly.graph_objects as go
import numpy as np
N = 100000
fig = go.Figure(data=go.Scattergl(
x = np.random.randn(N),
y = np.random.randn(N),
mode='markers',
marker=dict(
color=np.random.randn(N),
colorscale='Viridis',
line_width=1
)
))
fig.show()
image.png
import plotly.graph_objects as go
import numpy as np
N = 100000
r = np.random.uniform(0, 1, N)
theta = np.random.uniform(0, 2*np.pi, N)
fig = go.Figure(data=go.Scattergl(
x = r * np.cos(theta), # non-uniform distribution
y = r * np.sin(theta), # zoom to see more points at the center
mode='markers',
marker=dict(
color=np.random.randn(N),
colorscale='Viridis',
line_width=1
)
))
fig.show()