Python 3.x 在MacOS上的新窗口中渲染时,Sankey打印标签未对齐

Python 3.x 在MacOS上的新窗口中渲染时,Sankey打印标签未对齐,python-3.x,macos,jupyter-notebook,plotly,sankey-diagram,Python 3.x,Macos,Jupyter Notebook,Plotly,Sankey Diagram,使用iplot将以内联方式显示图表,这是完美的(除了新窗口的大小调整灵活性) “使用绘图”将在新窗口中打开,并且具有重复和未对齐的标签背景(所有尺寸和所有数据量) 此问题发生在MacOS上的Safari和Chrome中,但在Windows上的Opera中不存在 不同的浏览器-没有效果 不同的操作系统-好:没有问题 更改渲染模块-无效果 在数据框中获取一些MySQL数据,在这里做一个透视并展平成一个数据框。基本上源、目标、计数作为表结果 df = df4[df4.Cnt > 120]

使用iplot将以内联方式显示图表,这是完美的(除了新窗口的大小调整灵活性)

“使用绘图”将在新窗口中打开,并且具有重复和未对齐的标签背景(所有尺寸和所有数据量)

此问题发生在MacOS上的Safari和Chrome中,但在Windows上的Opera中不存在

  • 不同的浏览器-没有效果
  • 不同的操作系统-好:没有问题
  • 更改渲染模块-无效果

在数据框中获取一些MySQL数据,在这里做一个透视并展平成一个数据框。基本上源、目标、计数作为表结果

df = df4[df4.Cnt > 120] 
过滤到附加屏幕截图的最小数量

cat_cols=['Source', 'Target']
labelList = []
colorNumList = []
value_cols='Cnt'

for catCol in cat_cols:
labelListTemp =  list(set(df[catCol].values))
colorNumList.append(len(labelListTemp))
labelList = labelList + labelListTemp
从labelList中删除重复项

labelList = list(dict.fromkeys(labelList))
将df转换为源-目标对

for i in range(len(cat_cols)-1):
if i==0:
    sourceTargetDf = df[[cat_cols[i],cat_cols[i+1],value_cols]]
    sourceTargetDf.columns = ['source','target','count']
else:
    tempDf = df[[cat_cols[i],cat_cols[i+1],value_cols]]
    tempDf.columns = ['source','target','count']
    sourceTargetDf = pd.concat([sourceTargetDf,tempDf])
    sourceTargetDf = sourceTargetDf.groupby(['source','target']).agg({'count':'sum'}).reset_index()
sourceTargetDf['sourceID'] = sourceTargetDf['source'].apply(lambda x: labelList.index(x))
sourceTargetDf['targetID'] = sourceTargetDf['target'].apply(lambda x: labelList.index(x))

fig = go.Figure(data=[go.Sankey(
node = dict(
    pad = 15,
    thickness = 20,
    line = dict(color = "black", width = 0.5),
    label = labelList
),
link = dict(
    source = sourceTargetDf['sourceID'],
    target = sourceTargetDf['targetID'],
    value = sourceTargetDf['count']
))])
fig.update_layout(title_text='Test')
为源-目标对添加索引

for i in range(len(cat_cols)-1):
if i==0:
    sourceTargetDf = df[[cat_cols[i],cat_cols[i+1],value_cols]]
    sourceTargetDf.columns = ['source','target','count']
else:
    tempDf = df[[cat_cols[i],cat_cols[i+1],value_cols]]
    tempDf.columns = ['source','target','count']
    sourceTargetDf = pd.concat([sourceTargetDf,tempDf])
    sourceTargetDf = sourceTargetDf.groupby(['source','target']).agg({'count':'sum'}).reset_index()
sourceTargetDf['sourceID'] = sourceTargetDf['source'].apply(lambda x: labelList.index(x))
sourceTargetDf['targetID'] = sourceTargetDf['target'].apply(lambda x: labelList.index(x))

fig = go.Figure(data=[go.Sankey(
node = dict(
    pad = 15,
    thickness = 20,
    line = dict(color = "black", width = 0.5),
    label = labelList
),
link = dict(
    source = sourceTargetDf['sourceID'],
    target = sourceTargetDf['targetID'],
    value = sourceTargetDf['count']
))])
fig.update_layout(title_text='Test')
此Plotly需要导入Plotly,即使导入了其他模块

import plotly 
这将打开一个新窗口,并且标签模糊

plotly.offline.plot(fig, validate=False)
这些可以工作,但是是内联的

plotly.offline.iplot(fig, validate=False)
pio.show(fig)
fig.show()

plotly.offline.iplot(fig, validate=False)
pio.show(fig)
fig.show()