用 plotly express scattermatrix
import plotly.express as px
df = px.data.iris()
fig = px.scatter_matrix(df)
fig.show()
我想旋转 y 轴的轴标签(不是刻度标签),它们对应于数据框中的列,以便它们是水平的。因为,对于更长的名称和更多的子图,标签很快就会开始重叠。目前可行吗?
用 plotly express scattermatrix
import plotly.express as px
df = px.data.iris()
fig = px.scatter_matrix(df)
fig.show()
我想旋转 y 轴的轴标签(不是刻度标签),它们对应于数据框中的列,以便它们是水平的。因为,对于更长的名称和更多的子图,标签很快就会开始重叠。目前可行吗?
import plotly.express as px
df = px.data.iris()
fig = px.scatter_matrix(df)
# rotate text in yaxes, if text
fig.update_layout(
{
f"yaxis{n+1 if n>0 else ''}": {"tickangle": 45}
for n in range(len(fig.data[0].dimensions))
if isinstance(fig.data[0].dimensions[n].values[0], str)
}
)
# get labels of dimensions in splom
labels = [d.label for d in fig.data[0].dimensions]
# replace labels in splom with identifier
fig.update_traces(
dimensions=[d.update(label=chr(ord("a")+n)) for n, d in enumerate(fig.data[0].dimensions)]
)
# add a key for labels
for n, l in enumerate(labels):
fig.add_annotation(
x=(n) / (len(labels)-1),
y=1.2,
xref="paper",
yref="paper",
text=f"{chr(ord('a')+n)}. {l}",
align="center",
showarrow=False,
)
fig