溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

python可視化plotly圖例設(shè)置的示例分析

發(fā)布時(shí)間:2022-03-03 14:50:30 來(lái)源:億速云 閱讀:567 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹python可視化plotly圖例設(shè)置的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一、圖例(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è)置,字典類(lèi)型,可取屬性如下:

    • bgcolor:設(shè)置圖例的背景顏色

    • bordercolor:設(shè)置圖例邊框的顏色

    • borderwidth:設(shè)置圖例邊框的寬度

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

    • 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)題,字典類(lèi)型,可取屬性如下:

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

  • 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è)置圖例組名的文本字體,字典類(lèi)型,可取屬性如下:

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

python可視化plotly圖例設(shè)置的示例分析

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圖例設(shè)置的示例分析

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圖例設(shè)置的示例分析

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圖例設(shè)置的示例分析

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

python可視化plotly圖例設(shè)置的示例分析

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圖例設(shè)置的示例分析

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圖例設(shè)置的示例分析

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圖例設(shè)置的示例分析

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圖例設(shè)置的示例分析

以上是“python可視化plotly圖例設(shè)置的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI