Python Plotly – 如何自定义图例?
在 plotly 中,我们可以通过更改顺序、更改方向来自定义图例,我们可以隐藏或显示图例以及其他修改,例如增加图例的大小、更改字体和颜色。在本文中,让我们看看自定义图例的不同方式。
要自定义图例,我们使用 update_layout() 方法。
Syntax: update_layout(dict1=None, overwrite=False, **kwargs)
The values in the input dict / keyword arguments are used to iteratively alter the parts of the original layout.
Parameters:
- dict1 (dict) – To be updated is a dictionary of properties.
- overwrite (bool) – If True, existing properties will be overwritten. If False, recursively apply updates to existing properties, retaining properties that are not specified in the update operation.
- kwargs – To be updated is a keyword/value pair of properties.
示例 1:显示和隐藏图例
隐藏图例:在下面的代码中,我们导入 plotly.express 包和 pandas 包。导入 CSV 文件,显示散点图,通过 update_layout() 方法进一步修改该图,并将参数 showlegend 设置为 False。
要访问 CSV 文件,请单击 iris
Python3
#import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# initializing showlegend to "False"
fig.update_layout(showlegend=False)
fig.show()
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
fig.show()
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# order of legend is reversed
fig.update_layout(legend_traceorder="reversed")
fig.show()
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# changing orientation of the legend
fig.update_layout(legend=dict(
orientation="h",
))
fig.show()
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# adding different style parameters to the legend
fig.update_layout(
legend=dict(
x=0,
y=1,
title_font_family="Times New Roman",
font=dict(
family="Courier",
size=12,
color="black"
),
bgcolor="LightBlue",
bordercolor="Black",
borderwidth=1
)
)
fig.show()
输出:不显示图例。
默认情况下 showlegend 参数为 true。当我们在情节图例中绘制情节时,总是会显示。
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
fig.show()
输出:
示例 2:更改图例的顺序
在下面的代码中,我们引入了一个新参数,legend_traceorder,initialize 为“reversed”,图例的顺序被颠倒了。
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# order of legend is reversed
fig.update_layout(legend_traceorder="reversed")
fig.show()
输出:
更改顺序前:
更改顺序后:
顺序 setosa, versicolor , verginica 更改为 virginica, versicolor , setosa。
示例 3:更改图例的方向
对于水平图例,将 layout.legend.orientation 属性设置为“h”。我们还把它放在了这里的绘图区域上。通常,图例是垂直显示的。
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# changing orientation of the legend
fig.update_layout(legend=dict(
orientation="h",
))
fig.show()
输出:
示例 4:更改图例的大小、字体和颜色
在此示例中,引入了许多其他参数,例如 title_font_family、为样式指定子参数字典的字体、背景颜色、边框颜色和边框宽度的 bgcolor。
Python3
# import packages
import plotly.express as px
import pandas as pd
# importing csv file
df = pd.read_csv("iris.csv")
# scatter plot using plotly
fig = px.scatter(df, x="sepal_length",
y="sepal_width",
color="species")
# adding different style parameters to the legend
fig.update_layout(
legend=dict(
x=0,
y=1,
title_font_family="Times New Roman",
font=dict(
family="Courier",
size=12,
color="black"
),
bgcolor="LightBlue",
bordercolor="Black",
borderwidth=1
)
)
fig.show()
输出: