How to change the background color of labels or otherwise make them more readable?

Viewed 200

I use plotly express and want to add background color to my text for better readability. This is my code:

fig = px.parallel_categories(df,
                             dimensions=["batch_size", "out_channels", "num_layers",
                                         "aggr", "bias", "binned_" + continous_col],
                             color=continous_col,
                             color_continuous_scale= px.colors.sequential.Rainbow,
                             labels={"batch_size": "Batch Größe",
                                     "out_channels": "n",
                                     "num_layers": "Sequenzlänge L",
                                     "aggr": "Aggregatsfunktion",
                                     "bias": "Bias",
                                     "binned_" + continous_col: "TPR"}
                )
fig.show()

And this is the result:

enter image description here

It is a bit difficult to read the numbers or words in the plot because of all the colors. I would like them to be displayed on a white background. Or I could also imagine making the text bigger or bold. Do you know how I could achieve that?

1 Answers

There isn't much to be done with the color of a background since it seems the ticklabels for the dimensions of px.parallel_categories() do not have a background attribute:

'tickfont': {'color': '#2a3f5f', 'family': '"Open Sans", verdana, arial, sans-serif', 'size': 10}

Plot 1

enter image description here

To make the previous plot more readable though, you can adjust the size of the font through:

fig.data[0].tickfont.size = 24

Plot 2

enter image description here

And there's not a straight-forward way to do it, but the following snippet will set all ticklabels to a bold font:

for d in fig.data[0].dimensions:
    d.values = ['<b>'+str(e)+'</b>' for e in d.values] 
fig.show()

Plot 3

enter image description here

Complete code:

import plotly.express as px

df = px.data.tips()
fig = px.parallel_categories(df)
f = fig.full_figure_for_development(warn=False)
# fig.data[0].labelfont.size = 24
fig.data[0].tickfont.size = 24

for d in fig.data[0].dimensions:
    d.values = ['<b>'+str(e)+'</b>' for e in d.values] 

fig.show()
Related