Plotly Express 是 Python 交互式库 Plotly 的高级组件,受 Seaborn 和 ggplot2 的启发,它专门设计为具有简洁,一致且易于学习的 API ,只需一次导入Pandas dataframe,您就可以在一个函数调用中创建丰富的交互式绘图。
散点图
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
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()
折线图
import plotly.express as px
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.show()
条形图
import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
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
fig = px.pie(df, values='pop', names='country', title='Population of European continent')
fig.show()
直方图
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill")
fig.show()
提琴图
import plotly.express as px
df = px.data.tips()
fig = px.violin(df, y="total_bill")
fig.show()
2D密度热力图
import plotly.express as px
df = px.data.tips()
fig = px.density_heatmap(df, x="total_bill", y="tip")
fig.show()
3D散点图
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species')
fig.show()
3D折线图
import plotly.express as px
df = px.data.gapminder().query("country=='Brazil'")
fig = px.line_3d(df, x="gdpPercap", y="pop", z="year")
fig.show()
多维散点图矩阵
import plotly.express as px
df = px.data.iris()
fig = px.scatter_matrix(df,
dimensions=["sepal_length", "sepal_width", "petal_length", "petal_width"],
color="species")
fig.show()
结束语
Ploty Express语言简洁,只需很简短的代码就能实现可视化,而且它的另一个优势就是交互性功能非常强大,由于上面的图都是通过截图实现的,大家可以自己运行代码体验下。
欢迎扫码关注: