[英]How to not show repeated values in a heatmap in plotly express (px.imshow)?
I'm trying to plot a matrix using a heatmap chart but I would like to avoid repeated values;我正在尝试使用热图 plot 矩阵,但我想避免重复值;
When using seaborn we can set a "mask" to avoid showing all values, but I can't find the equivalent on Plotly / Plotly Express;使用 seaborn 时,我们可以设置一个“掩码”以避免显示所有值,但我在 Plotly / Plotly Express 上找不到等效项;
I would like to see something like:我想看到类似的东西:
But at this moment, it is the below format:但是此时,它是以下格式:
Below is an MWE example of my data structure... Any reference or help to do this will be very welcome下面是我的数据结构的 MWE 示例......非常欢迎任何参考或帮助这样做
import pandas as pd
import plotly.express as px
heatmap_data=pd.DataFrame(
{'user1': {'user1': 1,
'user2': 0.5267109866774764,
'user3': 0.905914413030722},
'user2': {'user1': 0.5267109866774764,
'user2': 1,
'user3': 0.5160264783692895},
'user3': {'user1': 0.905914413030722,
'user2': 0.5160264783692895,
'user3': 1}
})
fig = px.imshow(heatmap_data, zmin=0, zmax=1,
text_auto=True,
color_continuous_scale="Plasma")
fig
Thank you in advantage谢谢你的好处
The plotly heatmap does not implement the functionality you would expect. plotly 热图未实现您期望的功能。 Also, matrix diagrams such as scatter plots have the ability to hide the top half.此外,散点图等矩阵图具有隐藏上半部分的能力。 See this for examples .请参阅此示例。 So I take advantage of the fact that null values are not displayed and replace unwanted data with null values in the original data.因此,我利用未显示 null 值这一事实,将原始数据中不需要的数据替换为 null 值。 The default style then remains, so we change the theme and hide the axis lines.然后保留默认样式,因此我们更改主题并隐藏轴线。 Finally, the height of the color bar is adjusted.最后,调整颜色条的高度。
import pandas as pd
import plotly.express as px
heatmap_data=pd.DataFrame(
{'user1': {'user1': 1,
'user2': 0.5267109866774764,
'user3': 0.905914413030722},
'user2': {'user1': 0.5267109866774764,
'user2': 1,
'user3': 0.5160264783692895},
'user3': {'user1': 0.905914413030722,
'user2': 0.5160264783692895,
'user3': 1}
})
heatmap_data.loc['user1','user2']=None
heatmap_data.loc['user1','user3']=None
heatmap_data.loc['user2','user3']=None
fig = px.imshow(heatmap_data,
zmin=0,
zmax=1,
text_auto=True,
color_continuous_scale="Plasma",
template='simple_white'
)
fig.update_xaxes(showline=False)
fig.update_yaxes(showline=False)
fig.update_layout(autosize=False, width=400, coloraxis=dict(colorbar=dict(len=0.8)))
fig
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.