目录

2023-12-07-python可视化plotly-图例legend设置大全,值得收藏

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

文章目录


一、图例(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={}) 相关参数及示例

官方文档:

官方示例:

  • 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’

https://i-blog.csdnimg.cn/blog_migrate/02abfb70524bccb7d8c155f1326b3dc4.png

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()

https://i-blog.csdnimg.cn/blog_migrate/f179539cfbcdad447a51ddff86b84ca6.png

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()

https://i-blog.csdnimg.cn/blog_migrate/3ffa30f38887d6fb240d4908e621aa05.png

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()

https://i-blog.csdnimg.cn/blog_migrate/dceef381f01a9b4f7c2e9b379a8eae46.png

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()

https://i-blog.csdnimg.cn/blog_migrate/f4a0b2a33b756b8e7a42ea302aaddf31.png

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()

https://i-blog.csdnimg.cn/blog_migrate/66fdf1a731a771eb32f2c1a43045c729.png

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()

https://i-blog.csdnimg.cn/blog_migrate/0007c054afbc8742617a91c970edc1a8.png

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()

https://i-blog.csdnimg.cn/blog_migrate/4790ae6be55d4a1986a854b9b2c9dc1b.png

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()

https://i-blog.csdnimg.cn/blog_migrate/5ce2e0b593bef63fc018986619a583b2.png


关于Python技术储备

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

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

https://i-blog.csdnimg.cn/blog_migrate/9faee9b4ec0714b9e3c32c0649991360.png

一、Python所有方向的学习路线

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

https://i-blog.csdnimg.cn/blog_migrate/da2d9255f3b9b543b4f3f60386ec4aa9.jpeg#pic_center

二、Python基础学习视频

② 路线对应学习视频

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

https://i-blog.csdnimg.cn/blog_migrate/624aa4955c13ad0accebc7d43a5a0425.png

③练习题

每节视频课后,都有对应的练习题哦,可以检验学习成果哈哈!

https://i-blog.csdnimg.cn/blog_migrate/f6ee4e27d1ff29e6cbfa19aab22d05ec.gif#pic_center

因篇幅有限,仅展示部分资料

三、精品Python学习书籍

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

https://i-blog.csdnimg.cn/blog_migrate/0acaa1549a8aa41db0848c355128ce2c.png

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

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

https://i-blog.csdnimg.cn/blog_migrate/1d8f550aca26fc1d9b937d983338c210.png#pic_center

②Python实战案例

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

https://i-blog.csdnimg.cn/blog_migrate/549bb4fc64eaf6bb8aae9337edddd87e.png#pic_center

③Python小游戏源码

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

https://i-blog.csdnimg.cn/blog_migrate/98fc1415b71d65c78f59cd19c58e7985.png#pic_center

五、面试资料

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

https://i-blog.csdnimg.cn/blog_migrate/76297f91d14514672f4a1b141f32f260.png

https://i-blog.csdnimg.cn/blog_migrate/facf3133e795134ba197a63052378fd8.png

六、Python兼职渠道

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

https://i-blog.csdnimg.cn/blog_migrate/42a767bc20c2cfe7b1a3407c2196953b.png#pic_center

https://i-blog.csdnimg.cn/blog_migrate/386a0ef3c1396b9545b931c483606bc9.gif#pic_center

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

https://i-blog.csdnimg.cn/blog_migrate/9faee9b4ec0714b9e3c32c0649991360.png

68747470733a2f2f626c6f:672e6373646e2e6e65742f323330315f38303233393930382f:61727469636c652f64657461696c732f313334383533343830