英文:
Change the grid color or change spacing of cells for Plotly imshow
问题
我生成了一个Plotly imshow图,但我觉得它对我的数据有点难以阅读,因为我有很多类别。
我想要更改网格颜色以帮助定义单元格,或者作为首选选项,添加单元格之间的间距,以使图更容易阅读(在颜色之间添加空白,使它们不紧挨在一起)。
我尝试使用以下代码:
fig.update_xaxes(showgrid=True,
gridwidth=1,
gridcolor='blue')
但它不起作用,同样的情况也发生在以下代码:
fig.update_xaxes(visible=True)
我不知道如何在单元格之间添加空白(我不确定是否可能,但我想问一下)
我的完整代码如下:
import pandas as pd
import plotly.express as px
# 示例数据
df = pd.DataFrame({'cat':['A','B','C'],
'att1':[1,0.55,0.15],
'att2':[0.55,0.3,0.55],
'att3':[0.55,0.55,0.17]
})
df = df.set_index('cat')
# 绘图
fig = px.imshow(df, text_auto=False, height=800, width=900,
color_continuous_scale=px.colors.diverging.Picnic,
aspect='auto')
fig.update_layout(xaxis={'side': 'top'})
fig.update_xaxes(tickangle=-45,
showgrid=True,
gridwidth=1,
gridcolor='blue')
fig.update(layout_coloraxis_showscale=False)
fig.update_xaxes(visible=True)
fig.show()
英文:
I have generated a Plotly imshow figure, but I feel like it's a bit hard to read for my data since I have numerous categories.
I would like to either change the gird colours to help to define the cells or, as a perfered option, to add space between the cells to make the plot easier to read (add white space in-between the colours so they are not right beside each other).
I tried to use
fig.update_xaxes(showgrid=True,
gridwidth=1,
gridcolor='blue')
but it doesn't work, neither does
fig.update_xaxes(visible=True)
I have no idea how I would add write space in between the cells (I'm not sure it's possible, but, I thought I'd ask)
My full code is below
import pandas as pd
import plotly.express as px
# Sample data
df = pd.DataFrame({'cat':['A','B','C'],
'att1':[1,0.55,0.15],
'att2':[0.55,0.3,0.55],
'att3':[0.55,0.55,0.17]
})
df =df.set_index('cat')
# Graphing
fig = px.imshow(df, text_auto=False,height=800, width=900,
color_continuous_scale=px.colors.diverging.Picnic,
aspect='auto')
fig.update_layout(xaxis={'side': 'top'})
fig.update_xaxes(tickangle=-45,
showgrid=True,
gridwidth=1,
gridcolor='blue')
fig.update(layout_coloraxis_showscale=False)
fig.update_xaxes(visible=True)
fig.show()
答案1
得分: 1
一个相对不太优雅的解决方案是使用 scatter
而不是 imshow
。这意味着需要修改输入的数据框,以便将 att1
、att2
和 att3
重新整形为一个单独的 Pandas 系列:
df = pd.DataFrame({'cat':['A','B','C','A','B','C','A','B','C'],
'position' : [1, 1, 1, 2, 2, 2, 3, 3, 3],
'att':[1,0.55,0.15,0.55,0.3,0.55,0.55,0.55,0.17],
})
然后,您可以调用 scatter
并更新轨迹以使用方形符号代替圆形:
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
fig = px.scatter(data_frame=df, x='position', y='cat', color='att', color_continuous_scale=px.colors.diverging.Picnic , width=800, height=800)
fig.update_traces(marker=dict(size=160, symbol = 'square',line=dict(width=2,color='DarkSlateGrey')))
这段代码返回:
英文:
A rather inelegant solution would be to use scatter
instead of imshow
. This implies modifying your input dataframe so that att1
, att2
and att3
are reshaped a single pandas series:
df = pd.DataFrame({'cat':['A','B','C','A','B','C','A','B','C'],
'position' : [1, 1, 1, 2, 2, 2, 3, 3, 3],
'att':[1,0.55,0.15,0.55,0.3,0.55,0.55,0.55,0.17],
})
You may then call scatter
and update the traces to use square symbols instead of circles:
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
fig = px.scatter(data_frame=df, x='position', y='cat', color='att',color_continuous_scale=px.colors.diverging.Picnic , width=800, height=800)
fig.update_traces(marker=dict(size=160, symbol = 'square',line=dict(width=2,color='DarkSlateGrey')))
This snippet returns:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论