python可视化plotly 图例(legend)设置大全,值得收藏!

文章目录

    • 一、图例(legend)
    • 二、update\_layout(legend={}) 相关参数及示例
      • 关于Python技术储备
        • 一、Python所有方向的学习路线
        • 二、Python基础学习视频
        • 三、精品Python学习书籍
        • 四、Python工具包+项目源码合集
        • ①Python工具包
        • ②Python实战案例
        • ③Python小游戏源码
        • 五、面试资料
        • 六、Python兼职渠道


一、图例(legend)

import plotly.io as pio
import plotly.express as px
import plotly.graph\_objects as go
from plotly.subplots import make\_subplots
import pandas as pd
import numpy as np

# 设置plotly默认主题
pio.templates.default = 'plotly\_white'

# 设置pandas打印时显示所有列
pd.set\_option('display.max\_columns', None)

二、update_layout(legend={}) 相关参数及示例

官方文档:https://plotly.com/python/reference/layout/#layout-showlegend

官方示例:https://plotly.com/python/legend/

  • showlegend:是否显示图例,以下任一种情况发生时,该参数默认值为 True:1. 两个及两个以上的 trace 2. 有饼图3. 有一个 trace 显式指定 showlegend=True
  • legend:图例相关设置,字典类型,可取属性如下:
    • bgcolor:设置图例的背景颜色
    • bordercolor:设置图例边框的颜色
    • borderwidth:设置图例边框的宽度
    • font:设置图例条目的文本字体,字典类型,可取属性如下:
    • color:字体颜色
    • family:字体,字符串,可以为 Arial、Balto、Courier New、Droid Sans、Droid Serif、Droid Sans Mono、Gravitas One、Old Standard TT、Open Sans、Overpass、PT Sans Narrow、Raleway、Times New Roman
    • size:字体大小
  • orientation:设置图例的方向。‘v’(默认值)表示竖直显示图例、'h’表示水平显示图例
  • title:设置图例的标题,字典类型,可取属性如下:

font:设置图例条目的文本字体,字典类型,可取属性如下:

  • color:字体颜色
  • family:字体,字符串,可以为 Arial、Balto、Courier New、Droid Sans、Droid Serif、Droid Sans Mono、Gravitas One、Old Standard TT、Open Sans、Overpass、PT Sans Narrow、Raleway、Times New Roman
  • size:字体大小

side:设置图例标题相对于条目的位置。当 orientation=‘v’ 时默认为 ‘top’、当 orientation='h’时默认为 ‘left’、当为 'top left’时可用于扩展图例的面积
text:设置图例标题

  • grouptitlefont:设置图例组名的文本字体,字典类型,可取属性如下:
    • color:字体颜色
    • family:字体,字符串,可以为 Arial、Balto、Courier New、Droid Sans、Droid Serif、Droid Sans Mono、Gravitas One、Old Standard TT、Open Sans、Overpass、PT Sans Narrow、Raleway、Times New Roman
    • size:字体大小
  • itemsizing:设置图例条目的符号是否跟其 ‘trace’ 有关,如果为 ‘constant’,则所有条目的符号大小一致。
    • 可取 ‘trace’、 ‘constant’
  • itemwidth:设置条目的宽度(除 title 以外的部分)
    • 大于等于30的浮点数,默认值为30
  • tracegroupgap:设置图例组之间的间隔
    • 大于等于0的浮点数,默认值为10

traceorder:设置图例条目的顺序。如果为 ‘normal’,条目将从上到下按照输入数据的顺序排列;如果为 ‘reversed’,则按照输入数据的逆序排列;如果为 ‘grouped’,条目按照组顺序显示(如果 trace 中的legendgroup 设定了);如果为 ‘grouped+reversed’,则与 'grouped’的顺序相反
valign:设置条目符号和对应文本的竖直对齐方式。
可取 ‘middle’(默认值)、‘top’、‘bottom’

python可视化plotly 图例(legend)设置大全,值得收藏!_第1张图片

df = px.data.gapminder().query("year==2007")
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent",
    size="pop", size\_max=45, log\_x=True)

fig.update\_layout(legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.01
))

fig.write\_image('../pic/legend\_1.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第2张图片

df = px.data.gapminder().query("year==2007")
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent",
    size="pop", size\_max=45, log\_x=True)

fig.update\_layout(legend=dict(
    orientation="h",
    yanchor="bottom",
    y=1.02,
    xanchor="center",
    x=0.5,
    title\_text=''
))

fig.write\_image('../pic/legend\_2.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第3张图片

df = px.data.gapminder().query("year==2007")
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent",
    size="pop", size\_max=45, log\_x=True)


fig.update\_layout(
    legend=dict(
        x=0,
        y=1,
        traceorder="reversed",
        title\_font\_family="Times New Roman",
        font=dict(
            family="Courier",
            size=12,
            color="black"
        ),
        bgcolor="LightSteelBlue",
        bordercolor="Black",
        borderwidth=2
    )
)

fig.write\_image('../pic/legend\_3.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第4张图片

fig = go.Figure()

# 使用 name 参数指定条目文本,legendrank 指定顺序
fig.add\_trace(go.Bar(name="fourth", x=\["a", "b"\], y=\[2,1\], legendrank=4))
fig.add\_trace(go.Bar(name="second", x=\["a", "b"\], y=\[2,1\], legendrank=2))
fig.add\_trace(go.Bar(name="first", x=\["a", "b"\], y=\[1,2\], legendrank=1))
fig.add\_trace(go.Bar(name="third", x=\["a", "b"\], y=\[1,2\], legendrank=3))

fig.write\_image('../pic/legend\_4.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第5张图片

fig = go.Figure()

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3\],
    y=\[2, 1, 3\],
    legendgroup="group",  # this can be any string, not just "group"
    legendgrouptitle\_text="First Group Title",
    name="first legend group",
    mode="markers",
    marker=dict(color="Crimson", size=10)
))

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3\],
    y=\[2, 2, 2\],
    legendgroup="group",
    name="first legend group - average",
    mode="lines",
    line=dict(color="Crimson")
))

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3\],
    y=\[4, 9, 2\],
    legendgroup="group2",
    legendgrouptitle\_text="Second Group Title",
    name="second legend group",
    mode="markers",
    marker=dict(color="MediumPurple", size=10)
))

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3\],
    y=\[5, 5, 5\],
    legendgroup="group2",
    name="second legend group - average",
    mode="lines",
    line=dict(color="MediumPurple")
))

fig.update\_layout(title="Try Clicking on the Legend Items!")

fig.write\_image('../pic/legend\_5.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第6张图片

fig = go.Figure()

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3, 4, 5\],
    y=\[1, 2, 3, 4, 5\],
))

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3, 4, 5\],
    y=\[5, 4, 3, 2, 1\],
    visible='legendonly'
))

fig.write\_image('../pic/legend\_6.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第7张图片

fig = go.Figure()

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3, 4, 5\],
    y=\[1, 2, 3, 4, 5\],
    showlegend=False
))


fig.add\_trace(go.Scatter(
    x=\[1, 2, 3, 4, 5\],
    y=\[5, 4, 3, 2, 1\],
))

fig.update\_layout(showlegend=True)

fig.write\_image('../pic/legend\_7.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第8张图片

fig = go.Figure()

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3, 4, 5\],
    y=\[1, 2, 3, 4, 5\],
    mode='markers',
    marker={'size':10}
))

fig.add\_trace(go.Scatter(
    x=\[1, 2, 3, 4, 5\],
    y=\[5, 4, 3, 2, 1\],
    mode='markers',
    marker={'size':100}
))

fig.update\_layout(legend= {'itemsizing': 'trace'})

fig.write\_image('../pic/legend\_8.png', scale=2)
fig.show()

python可视化plotly 图例(legend)设置大全,值得收藏!_第9张图片


关于Python技术储备

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家分享一份全套的 Python 学习资料,希望提供给想学习 Python 的小伙伴们一点帮助!

保存图片微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

python可视化plotly 图例(legend)设置大全,值得收藏!_第10张图片
一、Python所有方向的学习路线

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
在这里插入图片描述

二、Python基础学习视频

② 路线对应学习视频

还有很多适合0基础入门的学习视频,有了这些视频,轻轻松松上手Python~在这里插入图片描述
在这里插入图片描述

③练习题

每节视频课后,都有对应的练习题哦,可以检验学习成果哈哈!
在这里插入图片描述
因篇幅有限,仅展示部分资料

三、精品Python学习书籍

当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。
在这里插入图片描述

四、Python工具包+项目源码合集
①Python工具包

学习Python常用的开发软件都在这里了!每个都有详细的安装教程,保证你可以安装成功哦!
在这里插入图片描述

②Python实战案例

光学理论是没用的,要学会跟着一起敲代码,动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。100+实战案例源码等你来拿!
在这里插入图片描述

③Python小游戏源码

如果觉得上面的实战案例有点枯燥,可以试试自己用Python编写小游戏,让你的学习过程中增添一点趣味!
在这里插入图片描述

五、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
在这里插入图片描述
在这里插入图片描述

六、Python兼职渠道

而且学会Python以后,还可以在各大兼职平台接单赚钱,各种兼职渠道+兼职注意事项+如何和客户沟通,我都整理成文档了。
在这里插入图片描述

这份完整版的Python全套学习资料已经上传CSDN,朋友们如果需要可以保存图片微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

python可视化plotly 图例(legend)设置大全,值得收藏!_第11张图片

你可能感兴趣的:(python,plotly,开发语言,信息可视化,计算机网络,学习,经验分享)