python可视化plotly图例设置的示例分析
python可视化plotly图例设置的示例分析
这篇文章主要介绍python可视化plotly图例设置的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
一、图例(legend)
importplotly.ioaspioimportplotly.expressaspximportplotly.graph_objectsasgofromplotly.subplotsimportmake_subplotsimportpandasaspdimportnumpyasnp#设置plotly默认主题pio.templates.default='plotly_white'#设置pandas打印时显示所有列pd.set_option('display.max_columns',None)
二、update_layout(legend={}) 相关参数及示例
官方文档:https://plotly.com/python/reference/layout/#layout-showlegend
官方示例:https://plotly.com/python/legend/
showlegend:是否显示图例,以下任一种情况发生时,该参数默认值为 True:1. 两个及两个以上的 trace 2. 有饼图3. 有一个 trace 显式指定 showlegend=True
legend:图例相关设置,字典类型,可取属性如下:
bgcolor
:设置图例的背景颜色bordercolor
:设置图例边框的颜色borderwidth
:设置图例边框的宽度font
:设置图例条目的文本字体,字典类型,可取属性如下: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
:设置图例的方向。'v'(默认值)表示竖直显示图例、'h'表示水平显示图例title
:设置图例的标题,字典类型,可取属性如下:
font:设置图例条目的文本字体,字典类型,可取属性如下:
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
:设置图例标题相对于条目的位置。当 orientation='v' 时默认为 'top'、当 orientation='h'时默认为 'left'、当为 'top left'时可用于扩展图例的面积text
:设置图例标题
grouptitlefont:设置图例组名的文本字体,字典类型,可取属性如下:
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:设置图例条目的符号是否跟其 ‘trace’ 有关,如果为 'constant',则所有条目的符号大小一致。
可取 'trace'、 'constant'
itemwidth:设置条目的宽度(除 title 以外的部分)
大于等于30的浮点数,默认值为30
tracegroupgap:设置图例组之间的间隔
大于等于0的浮点数,默认值为10
traceorder:设置图例条目的顺序。如果为 'normal',条目将从上到下按照输入数据的顺序排列;如果为 'reversed',则按照输入数据的逆序排列;如果为 'grouped',条目按照组顺序显示(如果 trace 中的legendgroup 设定了);如果为 'grouped+reversed',则与 'grouped'的顺序相反
valign:设置条目符号和对应文本的竖直对齐方式。
可取 'middle'(默认值)、'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="TimesNewRoman",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参数指定条目文本,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",#thiscanbeanystring,notjust"group"legendgrouptitle_text="FirstGroupTitle",name="firstlegendgroup",mode="markers",marker=dict(color="Crimson",size=10)))fig.add_trace(go.Scatter(x=[1,2,3],y=[2,2,2],legendgroup="group",name="firstlegendgroup-average",mode="lines",line=dict(color="Crimson")))fig.add_trace(go.Scatter(x=[1,2,3],y=[4,9,2],legendgroup="group2",legendgrouptitle_text="SecondGroupTitle",name="secondlegendgroup",mode="markers",marker=dict(color="MediumPurple",size=10)))fig.add_trace(go.Scatter(x=[1,2,3],y=[5,5,5],legendgroup="group2",name="secondlegendgroup-average",mode="lines",line=dict(color="MediumPurple")))fig.update_layout(title="TryClickingontheLegendItems!")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()
以上是“python可视化plotly图例设置的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注恰卡编程网行业资讯频道!
推荐阅读
-
Python 3.12 新特性解析:模式匹配增强与性能优化实战
-
Lightly IDE 深度评测:轻量级 Python 开发工具是否适合团队协作?
-
VS Code 自定义配置:JSON 文件修改、代码片段与任务自动化脚本
-
Python 虚拟环境选择:venv、conda、poetry 的适用场景对比
-
PyCharm+GitHub Copilot:Python 开发中 AI 辅助编码的最佳实践
-
PyCharm 无法识别虚拟环境?5 步排查 Python 解释器配置问题
-
数据科学工具链:Jupyter Notebook+RStudio+Python 的协同工作流
-
Python 3.12 新特性:模式匹配增强与性能改进实战
-
Lightly IDE 适合谁?轻量级 Python 开发工具深度评测
-
Python IDE 终极对比:PyCharm vs VS Code vs Jupyter Notebook