Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of python Visualization plotly Legend setting

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of python visual plotly legend setting, which is very detailed and has a certain reference value. Interested friends must read it!

Legend (legend) import plotly.io as pioimport plotly.express as pximport plotly.graph_objects as gofrom plotly.subplots import make_subplotsimport pandas as pdimport numpy as np# sets plotly default theme pio.templates.default = 'plotly_white'# sets pandas to display all columns pd.set_option (' display.max_columns', None) II, update_layout (legend= {}) related parameters and examples when printing

Official document: https://plotly.com/python/reference/layout/#layout-showlegend

Official example: https://plotly.com/python/legend/

Showlegend: whether to display the legend, when any of the following occurs, the default value of this parameter is True:1. Two or more trace 2. There is a pie figure 3. There is a trace that explicitly specifies showlegend=True

Legend: legend-related settings, dictionary type, and desirable attributes are as follows:

Bgcolor: sets the background color of the legend

Bordercolor: setting the color of the legend border

Borderwidth: sets the width of the legend border

Font: sets the text font and dictionary type of the legend entry. The desirable attributes are as follows:

Color: font color

Family: font, string, which can be 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: font siz

Orientation: sets the orientation of the legend.' V'(default) for vertical display legend,'h' for horizontal display legend

Title: set the title and dictionary type of the legend. The preferred attributes are as follows:

Font: sets the text font and dictionary type of the legend entry. The desirable attributes are as follows:

Color: font color

Family: font, string, which can be 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: font siz

Side: sets the position of the legend title relative to the entry. Defaults to 'top' when orientation='v',' left' when orientation='h', and can be used to expand the area of the legend when it is' top left'

Text: setting legend title

Grouptitlefont: sets the text font and dictionary type of the legend group name. The desirable attributes are as follows:

Color: font color

Family: font, string, which can be 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: font siz

Itemsizing: sets whether the symbol of the legend entry is related to its' trace'. If it is' constant', the symbol size of all entries is the same.

It is advisable to take 'trace',' constant'

Itemwidth: sets the width of the entry (except for title)

A floating point number greater than or equal to 30, with a default value of 30

Tracegroupgap: sets the interval between legend groups

A floating point number greater than or equal to 0, with a default of 10

Traceorder: sets the order of the legend entries. If the 'normal', entries are arranged in the order of input data from top to bottom, if' reversed', the input data is arranged in reverse order, if the 'grouped', entries are displayed in group order (if legendgroup in trace is set), if the' grouped+reversed', is set, the order of the input data is reversed.

Valign: sets the vertical alignment of the entry symbol and the corresponding text.

Preferably 'middle' (default),' 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", YYZ 0.99, xanchor= "left", XTX 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", xanchor= "center", xylene 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 () # use the name parameter to specify the entry text Legendrank specifies the order fig.add_trace (go.Bar (name= "fourth", x = ["a", "b"], y = [2jue 1], legendrank=4)) fig.add_trace (go.Bar (name= "second", x = ["a", "b"], y = [2jue 1], legendrank=2)) fig.add_trace (go.Bar (name= "first", x = ["a", "b"], y = [1jor2], legendrank=1) fig.add_trace (go.Bar (name= "third", x = ["a") "b"], y = [1jue 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 legendgroup", 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 legendgroup-average", mode= "lines", line=dict (color= "Crimson")) fig.add_trace (x = [1,2,3], y = [4,9,2], legendgroup= "group2", legendgrouptitle_text= "Second Group Title", name= "second legendgroup", mode= "markers", marker=dict (color= "MediumPurple", size=10)) fig.add_trace (x = [1) 2, 3], y = [5,5,5], legendgroup= "group2", name= "second legendgroup-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 ()

The above is all the contents of the article "sample Analysis of python Visualization plotly Legend Settings". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report