variable with spaces in plotly


I know it is not ideal to have variable names with spaces. But I am curious if there is a way to do this for plotly in python easily.

So this worked for variables without spaces:

fig_gender =
    df_gender_grp, y="gender", x="index", 
    labels=dict(gender="gender", index="count"),

but the same approach for variables with spaces (e.g., age group) did not work:

fig_age_grp =
   df_age_grp, y="gender", x="index", 
   color="age group",
   labels=dict(df_age_grp.(age group)="age group", index="count")

I tried using the f-string as well as df.variable name approaches, but both did not work. The error message is "SyntaxError: invalid syntax"

Thank you.

Asked By: Beavis



As a solution to your problem, it is likely that plotly allows for {"age group": "age group", "index": "count"} as a valid argument for labels (I cannot test this since you do not provide a minimum reproducible example).

In your code snippet, you are passing dict(df_age_grp.(age group)="age group", index="count") as a value to parameter label. That is wrong for three reasons. First, expects a dictionary with string keys (name of the column) and string values (label for the variable). Instead, you seem to attempt to pass a Pandas series as a dictionary key. Second, in general, a series is not a permitted dictionary key because it is not hashable. Third, your method of accessing column age group is not allowed. This method is called attribute access, but it is only available for columns that have a valid Python identifier – that is, age group is not valid. While spaces in column names are indeed allowed, they are not attribute-accessible and you would have to resort to, e.g., df_age_grp['age group'] (however, not here in this case as explained before).

Answered By: harryhaller

In labels=... you map column names of your dataframe to text to be displayed in the legend or at the axes.

In your case, it seems that you want column age group to displayed as text age group (which would happen anyway) and column index to be displayed as count.


labels={"age group": "age group", "index": "count"}

Answered By: M. Spiller