Plotly Express 是一个新的高级 Python 可视化库:它是 Plotly.py 的高级封装,它为复杂的图表提供了一个简单的语法。
用 pip install plotly_express 命令可以安装 Plotly Express。
import plotly_express as px
px.scatter(gapminder2007, x='gdpPercap', y='lifeExp',color='continent',
size='pop',size_max=60,hover_name='country')
px.scatter(gapminder2007, x='gdpPercap', y='lifeExp',color='continent',size='pop',size_max=60,
hover_name='country',facet_col ='continent',log_x=True)
px.scatter(gapminder, x='gdpPercap', y='lifeExp',
color='continent', size='pop',
size_max=60, hover_name='country',
animation_frame='year', animation_group='country',
log_x=True, range_x=[10,100000],
range_y=[25,90],
labels=dict(pop='Population', gdpPercap='GDP per Capita', lifeExp='Life Expectancy'))
px.choropleth(gapminder, locations='iso_alpha', color='lifeExp',
hover_name='country', animation_frame='year',
color_continuous_scale=px.colors.sequential.Plasma,
projection='natural earth')
iris = px.data.iris()
px.scatter(iris, x='sepal_width', y='sepal_length',
color='species', marginal_y='histogram',
marginal_x='box', trendline='ols')
tips = px.data.tips()
px.scatter(tips, x='total_bill', y='tip',facet_row='time',
facet_col='day',color='smoker',trendline='ols',
category_orders={'day':['Thur', 'Fri', 'Sat', 'Sun'], 'time':['Lunch', 'Dinner']})
px.parallel_coordinates(iris, color='species_id',
color_continuous_scale=px.colors.diverging.Tealrose,
color_continuous_midpoint=2)
px.parallel_categories(tips, color="size", color_continuous_scale=px.colors.sequential.Inferno)
px.density_contour(iris, x="sepal_width", y="sepal_length")
px.density_contour(iris, x="sepal_width", y="sepal_length", color="species")
px.histogram(tips, x="total_bill", color="smoker", facet_row="day", facet_col="time")
px.box(tips, y="tip", x="sex", color="smoker", facet_col="day", facet_row="time",
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
px.violin(tips, y="tip", x="sex", color="smoker", facet_col="day", facet_row="time",
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
election = px.data.election()
px.scatter_ternary(election, a="Joly", b="Coderre",
c="Bergeron", color="winner", size="total", hover_name="district",
symbol="result",
size_max=15, color_discrete_map =
{"Joly": "blue", "Bergeron": "green", "Coderre":"red"}
)
px.scatter_3d(election, x="Joly", y="Coderre", z="Bergeron",
color="winner", size="total", hover_name="district",
symbol="result", color_discrete_map = {"Joly": "blue",
"Bergeron": "green", "Coderre":"red"})
px.bar_polar(wind, r="value", theta="direction", color="strength", template="plotly_dark",
color_discrete_sequence= px.colors.sequential.Plotly[-2::-1])
px.scatter_matrix(iris, dimensions=["sepal_width", "sepal_length",
"petal_width", "petal_length"], color="species")
px.parallel_coordinates(iris, color='species_id', color_continuous_scale=['red','green', 'blue'])
import plotly_express as px
fig = px.scatter(px.data.iris(), x='sepal_width', y='sepal_length', color='species')
import plotly.graph_objs as go
fig.update(layout=dict(legend=dict(orientation='h',y=1.1,x=0.5),
annotations=[go.layout.Annotation
(text='this one is interesting', x=3.6, y=7.2)]))