python可視化plotly?圖例(legend)設(shè)置
一、圖例(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 Romansize
:字體大小
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 Romansize
:字體大小
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 Romansize
:字體大小
- 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)曲線救國?今天小編就為大家?guī)砹私鉀Q方法,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python數(shù)據(jù)處理Filter函數(shù)高級(jí)用法示例
本文將詳細(xì)介紹filter函數(shù)的使用方法,并提供豐富的示例代碼,幫助你深入理解如何利用它來處理數(shù)據(jù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11使用python創(chuàng)建Excel工作簿及工作表過程圖解
這篇文章主要介紹了使用python創(chuàng)建Excel工作簿及工作表,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05