plotly-express-21-theme and Figurewidget

486 阅读1分钟

plotly-express-21-theme and Figurewidget

本文中介绍了Plotly中的多种图形的效果,自己一般都是默认的主题;Figurewidget是一个比较好的功能,可以往空白图中一个添加图形。

如果是Figure的话,是先把数据处理添加进入后,一次性生成全部的图形,FigureWidget是逐个加入。

不同的主题

主题

主要的主题包含下面几种:

import plotly.io as pio
pio.templates


Templates configuration
-----------------------
    Default template: 'plotly'
    Available templates:
        ['ggplot2', 'seaborn', 'simple_white', 'plotly',
         'plotly_white', 'plotly_dark', 'presentation', 'xgridoff',
         'ygridoff', 'gridon', 'none']

实例

import plotly.express as px

df = px.data.gapminder()
df_2007 = df.query("year==2007")

for template in ["plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "simple_white", "none"]:
    fig = px.scatter(df_2007,
                     x="gdpPercap", y="lifeExp", size="pop", color="continent",
                     log_x=True, size_max=60,
                     template=template, title="Gapminder 2007: '%s' theme" % template)
    fig.show()

FigureWidget

原理

FigureWidget是最近发现的Plotly的功能,其功能可以概括为:conf生成的空白图开始,添加给定数据,再生成需要的图形

  • 生成一个空白图
  • 添加数据,生成指定的图形
  • 修改数据,更新图形

demo

生成空白的图形
添加折线图
添加柱状图
添加标题
更新数据

本文使用 mdnice 排版