python可视化plotly 图例(legend)设置
作者:Eloik 发布时间:2021-05-17 11:17:56
一、图例(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 Romansize
:字体大小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 Romansize
:字体大小
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 Romansize
:字体大小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'
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()
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()
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()
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()
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()
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()
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()
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://blog.csdn.net/weixin_45826022/article/details/122912525
猜你喜欢
- Math 对象js 给我们提供了一些操作数字的方法也是一种数据类型 是复杂数据类型Math对象的通用语法: Math.xxx()random
- 阅读目录tcp协议:流式协议(以数据流的形式通信传输)、安全协议(收发信息都需收到确认信息才能完成收发,是一种双向通道的通信)tcp协议在O
- /*Bresenham画圆算法*/var arc = function(x0,y0,r){/*起点坐标x0,y
- 前言关于mockjs,官网描述的是1.前后端分离2.不需要修改既有代码,就可以拦截 Ajax 请求,返回模拟的响应数据。3.数据类型丰富4.
- 最近,带领我的学生进行一个URTP项目设计,需要进行人脸识别。由于现在的OpenCV已经到了2.X版本,因此就不想用原来的1.X版本的代码,
- 不正确地调用Windows应用程序接口可能会产生一些意想不到的副作用,以及潜在地对一个应用程序的代码及数据段的破坏。正确地使用一个空的32位
- 前言Tree一直是大家熟知的组件,做一些大型的后台管理系统都会用到。使用树组件可以完整的展现其中的层级关系,并具有展开收起选择等交互功能。效
- 如下所示:import dateutildef before_month_lastday(ti): today=dateutil
- 一、无镜像安装 pip install 库名打开命令提示符【win + r】,输入cmd,在命令提示窗口输入pip install 库名,
- 摘要数据分析与建模的时候大部分时间在数据准备上,包括对数据的加载、清理、转换以及重塑。pandas提供了一组高级的、灵活的、高效的核心函数,
- 本文主要介绍了Python pandas 重命名索引和列名称的实现,分享给大家,具体如下:df=pd.DataFrame(np.arange
- 如果用户输入的是直接插入到一个SQL语句中的查询,应用程序会很容易受到SQL注入,例如下面的例子:$unsafe_variable = $_
- 本文实例讲述了Python使用迭代器捕获Generator返回值的方法。分享给大家供大家参考,具体如下:用for循环调用generator时
- 创建一个SpringBoot项目其他不赘叙了,引入MyBaties、MySql依赖创建mysql表CREATE TABLE sp_users
- 本文实例为大家分享了Tensorflow实现神经网络拟合线性回归的具体代码,供大家参考,具体内容如下一、利用简单的一层神经网络拟合一个函数
- 跨平台的事件EventUtil对象 EventUtil: var EventUtil={ addEventHandler:function(
- 这个函数用于储存图片,将数组保存为图像此功能仅在安装了Python Imaging Library(PIL)时可用。版本也比较老了,新的替代
- 简介zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。安装pip instal
- 本文实例讲述了golang使用sort接口实现排序的方法。分享给大家供大家参考,具体如下:今天看见群里再讨论排序的sort.Interfac
- keras中卷积层Conv2D的学习关于卷积的具体操作不细讲,本文只是自己太懒了不想记手写笔记。由于自己接触到的都是图像处理相关的工作,因此