欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python可視化plotly?圖例(legend)設(shè)置

 更新時(shí)間:2022年02月17日 14:27:36   作者:Eloik  
這篇文章主要介紹了python可視化plotly?圖例(legend)設(shè)置,主要介紹了關(guān)于python?的legend圖例,參數(shù)使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,需要的朋友可以參考下賣你具體內(nèi)容

一、圖例(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

# 設(shè)置plotly默認(rèn)主題
pio.templates.default = 'plotly_white'

# 設(shè)置pandas打印時(shí)顯示所有列
pd.set_option('display.max_columns', None)

二、update_layout(legend={}) 相關(guān)參數(shù)及示例

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

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

  • showlegend:是否顯示圖例,以下任一種情況發(fā)生時(shí),該參數(shù)默認(rèn)值為 True:1. 兩個(gè)及兩個(gè)以上的 trace 2. 有餅圖3. 有一個(gè) trace 顯式指定 showlegend=True
  • legend:圖例相關(guān)設(shè)置,字典類型,可取屬性如下:
    • bgcolor:設(shè)置圖例的背景顏色
    • bordercolor:設(shè)置圖例邊框的顏色
    • borderwidth:設(shè)置圖例邊框的寬度
    • font:設(shè)置圖例條目的文本字體,字典類型,可取屬性如下:
    • 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:設(shè)置圖例的方向。'v'(默認(rèn)值)表示豎直顯示圖例、'h'表示水平顯示圖例
  • title:設(shè)置圖例的標(biāo)題,字典類型,可取屬性如下:

font:設(shè)置圖例條目的文本字體,字典類型,可取屬性如下:

  • 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:設(shè)置圖例標(biāo)題相對(duì)于條目的位置。當(dāng) orientation='v' 時(shí)默認(rèn)為 'top'、當(dāng) orientation='h'時(shí)默認(rèn)為 'left'、當(dāng)為 'top left'時(shí)可用于擴(kuò)展圖例的面積
text:設(shè)置圖例標(biāo)題

  • grouptitlefont:設(shè)置圖例組名的文本字體,字典類型,可取屬性如下:
    • 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:設(shè)置圖例條目的符號(hào)是否跟其 ‘trace’ 有關(guān),如果為 'constant',則所有條目的符號(hào)大小一致。
    • 可取 'trace'、 'constant'
  • itemwidth:設(shè)置條目的寬度(除 title 以外的部分)
    • 大于等于30的浮點(diǎn)數(shù),默認(rèn)值為30
  • tracegroupgap:設(shè)置圖例組之間的間隔
    • 大于等于0的浮點(diǎn)數(shù),默認(rèn)值為10

traceorder:設(shè)置圖例條目的順序。如果為 'normal',條目將從上到下按照輸入數(shù)據(jù)的順序排列;如果為 'reversed',則按照輸入數(shù)據(jù)的逆序排列;如果為 'grouped',條目按照組順序顯示(如果 trace 中的legendgroup 設(shè)定了);如果為 'grouped+reversed',則與 'grouped'的順序相反
valign:設(shè)置條目符號(hào)和對(duì)應(yīng)文本的豎直對(duì)齊方式。
可取 'middle'(默認(rèn)值)、'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 參數(shù)指定條目文本,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()

到此這篇關(guān)于python可視化plotly 圖例(legend)設(shè)置的文章就介紹到這了,更多相關(guān)plotly 圖例(legend)設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Jupyter 無法下載文件夾如何實(shí)現(xiàn)曲線救國

    Jupyter 無法下載文件夾如何實(shí)現(xiàn)曲線救國

    這篇文章主要介紹了Jupyter 無法下載文件夾如何實(shí)現(xiàn)曲線救國?今天小編就為大家?guī)砹私鉀Q方法,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python圖像處理之邊緣檢測(cè)原理詳解

    Python圖像處理之邊緣檢測(cè)原理詳解

    邊緣檢測(cè)是圖像處理和計(jì)算機(jī)視覺當(dāng)中的基本問題,邊緣檢測(cè)的目的是標(biāo)識(shí)數(shù)字圖像中亮度變化明顯的點(diǎn)。本文將通過示例和大家介紹一下邊緣檢測(cè)的原理,希望對(duì)大家有所幫助
    2022-12-12
  • Python 將Matrix、Dict保存到文件的方法

    Python 將Matrix、Dict保存到文件的方法

    今天小編就為大家分享一篇Python 將Matrix、Dict保存到文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Tensorflow 查看變量的值方法

    Tensorflow 查看變量的值方法

    今天小編就為大家分享一篇Tensorflow 查看變量的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Pytorch各種維度變換函數(shù)總結(jié)

    Pytorch各種維度變換函數(shù)總結(jié)

    本文對(duì)于PyTorch中的各種維度變換的函數(shù)進(jìn)行總結(jié),包括reshape()、view()、resize_()、transpose()、permute()、squeeze()、unsqeeze()、expand()、repeat()函數(shù)的介紹和對(duì)比,感興趣的可以了解一下
    2024-02-02
  • Django框架中間件定義與使用方法案例分析

    Django框架中間件定義與使用方法案例分析

    這篇文章主要介紹了Django框架中間件定義與使用方法,結(jié)合具體案例形式分析了Django框架中間件相關(guān)定義、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-11-11
  • 基于DATAFRAME中元素的讀取與修改方法

    基于DATAFRAME中元素的讀取與修改方法

    今天小編就為大家分享一篇基于DATAFRAME中元素的讀取與修改方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python數(shù)據(jù)處理Filter函數(shù)高級(jí)用法示例

    Python數(shù)據(jù)處理Filter函數(shù)高級(jí)用法示例

    本文將詳細(xì)介紹filter函數(shù)的使用方法,并提供豐富的示例代碼,幫助你深入理解如何利用它來處理數(shù)據(jù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 使用python創(chuàng)建Excel工作簿及工作表過程圖解

    使用python創(chuàng)建Excel工作簿及工作表過程圖解

    這篇文章主要介紹了使用python創(chuàng)建Excel工作簿及工作表,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • pyhthon繪制超炫酷的心形線星形線擺線

    pyhthon繪制超炫酷的心形線星形線擺線

    這篇文章主要為大家介紹了如何用pyhthon繪制各種超炫酷的擺線,本文主要實(shí)現(xiàn)了心形線和星形線也就是外擺線和內(nèi)擺線兩種,有需要的朋友可以借鑒參考下
    2021-10-10

最新評(píng)論