<legend id='t81lM'><style id='t81lM'><dir id='t81lM'><q id='t81lM'></q></dir></style></legend>
      <tfoot id='t81lM'></tfoot>

        <i id='t81lM'><tr id='t81lM'><dt id='t81lM'><q id='t81lM'><span id='t81lM'><b id='t81lM'><form id='t81lM'><ins id='t81lM'></ins><ul id='t81lM'></ul><sub id='t81lM'></sub></form><legend id='t81lM'></legend><bdo id='t81lM'><pre id='t81lM'><center id='t81lM'></center></pre></bdo></b><th id='t81lM'></th></span></q></dt></tr></i><div id='t81lM'><tfoot id='t81lM'></tfoot><dl id='t81lM'><fieldset id='t81lM'></fieldset></dl></div>
        • <bdo id='t81lM'></bdo><ul id='t81lM'></ul>

      1. <small id='t81lM'></small><noframes id='t81lM'>

        Plotly:如何在 plotly express 折线图中更改图例的变量/标签名称?

        Plotly: How to change variable/label names for the legend in a plotly express line chart?(Plotly:如何在 plotly express 折线图中更改图例的变量/标签名称?)
      2. <legend id='rdsow'><style id='rdsow'><dir id='rdsow'><q id='rdsow'></q></dir></style></legend>
      3. <small id='rdsow'></small><noframes id='rdsow'>

          <tbody id='rdsow'></tbody>
        <tfoot id='rdsow'></tfoot>

            <i id='rdsow'><tr id='rdsow'><dt id='rdsow'><q id='rdsow'><span id='rdsow'><b id='rdsow'><form id='rdsow'><ins id='rdsow'></ins><ul id='rdsow'></ul><sub id='rdsow'></sub></form><legend id='rdsow'></legend><bdo id='rdsow'><pre id='rdsow'><center id='rdsow'></center></pre></bdo></b><th id='rdsow'></th></span></q></dt></tr></i><div id='rdsow'><tfoot id='rdsow'></tfoot><dl id='rdsow'><fieldset id='rdsow'></fieldset></dl></div>

                  <bdo id='rdsow'></bdo><ul id='rdsow'></ul>
                • 本文介绍了Plotly:如何在 plotly express 折线图中更改图例的变量/标签名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 python 中更改 plotly express 中的变量/标签名称.我首先创建一个情节:

                  I want to change the variable/label names in plotly express in python. I first create a plot:

                  import pandas as pd
                  import plotly.express as px
                  
                  d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]}
                  df = pd.DataFrame(data=d)
                  fig = px.line(df, x=df.index, y=['col1', 'col2'])
                  fig.show()
                  

                  产量:

                  我想将标签名称从 col1 更改为 hello 并从 col2 更改为 hi.我试过在图中使用标签,但我无法让它工作:

                  I want to change the label names from col1 to hello and from col2 to hi. I have tried using labels in the figure, but I cannot get it to work:

                  fig = px.line(df, x=df.index, y=['col1', 'col2'], labels={'col1': "hello", 'col2': "hi"})
                  fig.show()
                  

                  但这似乎什么也没做,同时不会产生错误.显然,我可以通过更改列名来实现我的目标,但我试图创建的实际情节实际上并不允许这样做,因为它来自几个不同的数据框.

                  But this seems to do nothing, while not producing an error. Obviously I could achieve my goals by changing the column names, but the actual plot i'm trying to create doesn't really allow for that since it comes from several different dataframes.

                  推荐答案

                  答案:

                  在不更改数据源的情况下,完全替换图例、图例组和悬停模板中的名称将需要:

                  The answer:

                  Without changing the data source, a complete replacement of names both in the legend, legendgroup and hovertemplate will require:

                  newnames = {'col1':'hello', 'col2': 'hi'}
                  fig.for_each_trace(lambda t: t.update(name = newnames[t.name],
                                                        legendgroup = newnames[t.name],
                                                        hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])
                                                       )
                                    )
                  

                  剧情:

                  使用

                  fig.for_each_trace(lambda t: t.update(name = newnames[t.name]))
                  

                  ...您可以更改图例中的名称,而无需使用 dict 更改源

                  ...you can change the names in the legend without ghanging the source by using a dict

                  newnames = {'col1':'hello', 'col2': 'hi'}
                  

                  ...并将新名称映射到图形结构以下部分中的现有 col1col2(对于您的第一个跟踪,col1):

                  ...and map new names to the existing col1 and col2 in the following part of the figure structure (for your first trace, col1):

                  {'hovertemplate': 'variable=col1<br>index=%{x}<br>value=%{y}<extra></extra>',
                  'legendgroup': 'col1',
                  'line': {'color': '#636efa', 'dash': 'solid'},
                  'mode': 'lines',
                  'name': 'hello',   # <============================= here!
                  'orientation': 'v',
                  'showlegend': True,
                  'type': 'scatter',
                  'x': array([0, 1, 2], dtype=int64),
                  'xaxis': 'x',
                  'y': array([1, 2, 3], dtype=int64),
                  'yaxis': 'y'},
                  

                  但正如您所见,这对 'legendgroup': 'col1''hovertemplate': 'variable=col1<br>index=%{ 没有任何作用x}<br>value=%{y}<extra></extra>' 根据你的图形的复杂性,这可能会造成问题.所以我会添加 legendgroup = newnames[t.name]hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name]).

                  But as you can see, this doesn't do anything with 'legendgroup': 'col1', nor 'hovertemplate': 'variable=col1<br>index=%{x}<br>value=%{y}<extra></extra>' And depending on the complexity of your figure, this can pose a problem. So I would add legendgroup = newnames[t.name] and hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])into the mix.

                  import pandas as pd
                  import plotly.express as px
                  from itertools import cycle
                  
                  d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]}
                  df = pd.DataFrame(data=d)
                  fig = px.line(df, x=df.index, y=['col1', 'col2'])
                  
                  newnames = {'col1':'hello', 'col2': 'hi'}
                  fig.for_each_trace(lambda t: t.update(name = newnames[t.name],
                                                        legendgroup = newnames[t.name],
                                                        hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])
                                                       )
                                    )
                  

                  这篇关于Plotly:如何在 plotly express 折线图中更改图例的变量/标签名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                • <legend id='uPPYX'><style id='uPPYX'><dir id='uPPYX'><q id='uPPYX'></q></dir></style></legend>

                  <small id='uPPYX'></small><noframes id='uPPYX'>

                • <tfoot id='uPPYX'></tfoot>

                      <bdo id='uPPYX'></bdo><ul id='uPPYX'></ul>
                        <i id='uPPYX'><tr id='uPPYX'><dt id='uPPYX'><q id='uPPYX'><span id='uPPYX'><b id='uPPYX'><form id='uPPYX'><ins id='uPPYX'></ins><ul id='uPPYX'></ul><sub id='uPPYX'></sub></form><legend id='uPPYX'></legend><bdo id='uPPYX'><pre id='uPPYX'><center id='uPPYX'></center></pre></bdo></b><th id='uPPYX'></th></span></q></dt></tr></i><div id='uPPYX'><tfoot id='uPPYX'></tfoot><dl id='uPPYX'><fieldset id='uPPYX'></fieldset></dl></div>
                            <tbody id='uPPYX'></tbody>