情节:如何在情节快速的折线图中更改图例的变量/标签名称? [英] Plotly: How to change variable/label names for the legend in a plotly express line chart?
问题描述
我想在python中以绘图方式更改变量/标签名称.我首先创建一个情节:
将pandas导入为pd导入plotly.express为pxd = {'col1':[1,2,3],'col2':[3,4,5]}df = pd.DataFrame(data = d)无花果= px.line(df,x = df.index,y = ['col1','col2'])图show()
哪个产量:
我想将标签名称从 col1 更改为 hello ,从 col2 更改为 hi .我尝试使用图中的标签,但无法正常工作:
fig = px.line(df,x = df.index,y = ['col1','col2'],labels = {'col1':"hello",'col2':嗨"}}图show()
但是这似乎什么都不做,同时不会产生错误.显然,我可以通过更改列名来实现自己的目标,但是我尝试创建的实际图并不能真正做到这一点,因为它来自多个不同的数据框.
简介
在
答案1的完整代码:
将pandas导入为pd导入plotly.express为px从itertools导入周期d = {'col1':[1,2,3],'col2':[3,4,5]}df = pd.DataFrame(data = d)无花果= px.line(df,x = df.index,y = ['col1','col2'])名称=循环(['hello!','hi!'])fig.for_each_trace(lambda t:t.update(name = next(names)))图show()
答案2的完整代码:
将pandas导入为pd导入plotly.express为pxd = {'col1':[1,2,3],'col2':[3,4,5]}df = pd.DataFrame(data = d)无花果= px.line(df,x = df.index,y = ['col1','col2'])def customLegend(fig,nameSwap):对于我来说,dat枚举(fig.data):对于dat中的elem:如果elem =='名称':fig.data [i] .name = nameSwap [fig.data [i] .name]返回(图)fig = customLegend(fig = fig,nameSwap = {'col1':'hello!','col2':'hi!'})图show()
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()
Which yields:
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.
Intro
A similar question has been asked and answered under Plotly: How to change legend for a go.pie chart without changing data source?. But the solution will, to my knowledge, depend on the type of figure you're using. So the answers there will not work for your use case.
Caveat
Change the name of the traces without changing the names of the trace sources
Answer 1 - Order of trace names is known: list
approach
In this case, the most concise approach is arguably:
names = cycle(['hello!', 'hi!'])
fig.for_each_trace(lambda t: t.update(name = next(names)))
Answer 2 - Order of trace name is unknown: dict
approach
You can change the legend name for your first trace directly through fig.data[0].name
. But to make things a bit more dynamic, I've set up a function that takes an existing fig
as an argument along with a dict nameSwap
that contains old and new names in key, value pairs.
Function
def customLegend(fig, nameSwap):
for i, dat in enumerate(fig.data):
for elem in dat:
if elem == 'name':
print(fig.data[i].name)
fig.data[i].name = nameSwap[fig.data[i].name]
return(fig)
Plot
Complete code for answer 1:
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'])
names = cycle(['hello!', 'hi!'])
fig.for_each_trace(lambda t: t.update(name = next(names)))
fig.show()
Complete code for answer 2:
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'])
def customLegend(fig, nameSwap):
for i, dat in enumerate(fig.data):
for elem in dat:
if elem == 'name':
fig.data[i].name = nameSwap[fig.data[i].name]
return(fig)
fig = customLegend(fig=fig, nameSwap = {'col1': 'hello!', 'col2':'hi!'})
fig.show()
这篇关于情节:如何在情节快速的折线图中更改图例的变量/标签名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!