Black Lives Matter. Please consider donating to Black Girls Code today.

Axis labels on scatter_matrix

I spent few hours seeing if I could replicate sns.pairplot with plotly in an easy way, but is problem after problem.
I gave up on try to do the histogram on the diagonal because I saw that is a work in progress (https://github.com/plotly/plotly_express/issues/42).

In seaborn I used:

sns.pairplot(data)

and I got in 16s:

In plotly after deciding not to use ff.create_scatterplotmatrix() because is deprecated and also need to play with the width and height, I tried with plotly express and got in 66ms:

px.scatter_matrix(data[numeric_cols])

Then I had to start playing with the size and the marker size because the points were too big to see the distribution, and I got this in 72ms:

fig = px.scatter_matrix(data[numeric_cols], height=800, width=800)
fig.update_traces(marker=dict(size=1, line=dict(width=1, color="DarkSlateGrey")))
fig.update_traces(diagonal_visible=False)
fig.show()

But still the axis labels are impossible to handle, I tried with fig.update_xaxes(tickangle=45) but it only works for the labels inside, and only the first graph. And even if works maybe it will not work for all axis.

So, my question is if there are some automatic way to fix the size without needing to start trying different width/height, axis labels not overlapping… or what is the best hotfix?

EDIT: Btw if someone wants to replicate the dataset is a modified version of https://www.kaggle.com/unsdsn/world-happiness

Your best bet would be to reduce the font size, with e.g. layout.font.size

1 Like

And is possible to rotate the axis labels? I mean, if I can rotate them 45 or 90 degrees I think it could work in combination with the font size.

Yes, you can alter any property on the axes, but unfortunately you can’t use update_xaxes() in this case, as the axes are implicitly created in the Javascript layer, so you’ll need to enumerate the axes you’re targeting for update:

import plotly.express as px

fig = px.scatter_matrix(px.data.tips())
fig.update_layout({"xaxis"+str(i+1): dict(tickangle = -45) for i in range(7)})
fig.show()

I just tried it, but this turns the numbers of each graph. I meant more the texts overlapping (‘Happiness Rank’, ‘Happiness Score’…) and the rest that we can read clearly in the last photo because they are overlapped.

I thought that with a 45 degrees angle on those texts, they would be mostly readable.

Gotcha. We don’t have an attribute for that, no. Really your only option is to reduce the font size relative to the figure size, either by reducing the former or increasing the latter.