更改Plotly imshow的网格颜色或更改单元格间距

huangapple go评论67阅读模式
英文:

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。这意味着需要修改输入的数据框,以便将 att1att2att3 重新整形为一个单独的 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')))

这段代码返回:

更改Plotly imshow的网格颜色或更改单元格间距

英文:

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:

更改Plotly imshow的网格颜色或更改单元格间距

huangapple
  • 本文由 发表于 2023年2月24日 07:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定