脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python可视化plotly 图例(legend)设置

python可视化plotly 图例(legend)设置

2022-10-05 16:03Eloik Python

这篇文章主要介绍了python可视化plotly 图例(legend)设置,主要介绍了关于python 的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={}) 相关参数及示例

官方文档: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)设置

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)设置

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)设置

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)设置

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)设置

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)设置

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)设置

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)设置

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)设置

到此这篇关于python可视化plotly 图例(legend)设置的文章就介绍到这了,更多相关plotly 图例(legend)设置内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_45826022/article/details/122912525

延伸 · 阅读

精彩推荐
  • Pythonpython 多进程和多线程使用详解

    python 多进程和多线程使用详解

    这篇文章主要介绍了python 多进程和多线程使用详解,帮助大家更好得理解和学习使用python,感兴趣的朋友可以了解下...

    酸萝卜别吃11822021-09-29
  • Pythonpython 常见的反爬虫策略

    python 常见的反爬虫策略

    这篇文章主要介绍了python反爬虫策略,帮助大家更好的理解和使用python 爬虫,感兴趣的朋友可以了解下...

    松鼠爱吃饼干3502020-09-28
  • Pythonpython docx的超链接网址和链接文本操作

    python docx的超链接网址和链接文本操作

    这篇文章主要介绍了python docx的超链接网址和链接文本操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    追梦小狂魔6822021-09-15
  • PythonPython 模拟员工信息数据库操作的实例

    Python 模拟员工信息数据库操作的实例

    下面小编就为大家带来一篇Python 模拟员工信息数据库操作的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    doreimi1792020-12-12
  • Python快速解决安装python没有scripts文件夹的问题

    快速解决安装python没有scripts文件夹的问题

    下面小编就为大家分享一篇快速解决安装python没有scripts文件夹的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    yedoubushishen15192021-01-27
  • PythonPython提取Linux内核源代码的目录结构实现方法

    Python提取Linux内核源代码的目录结构实现方法

    下面小编就为大家带来一篇Python提取Linux内核源代码的目录结构实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来...

    Python教程网2442020-08-29
  • Python简单了解Django ORM常用字段类型及参数配置

    简单了解Django ORM常用字段类型及参数配置

    这篇文章主要介绍了简单了解Django ORM常用字段类型及参数配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    子钦加油3132020-05-05
  • PythonPython爬虫之网页图片抓取的方法

    Python爬虫之网页图片抓取的方法

    最近小编一直在学习python的东西,今天小编给大家分享基于python写的一个爬虫程序,能实现简单的网页图片下载,具体实例代码大家参考下本文...

    JentZhang17162021-03-18