Using iplot will show the chart inline and is perfect (except for the resizing flexibility of a new window)
plotly.offline.iplot(fig, validate=False)
Using plot will open in a new window and has label backgrounds that are duplicated and misaligned (at all sizes and with all data quantities)
plotly.offline.plot(fig, validate=False)
This issue occurs in Safari and Chrome on MacOS but not in Opera on Windows
- Different browsers - no effect
- Different OS - Good: no issue
- Changing rendering modules - no effect
import pandas as pd
import plotly
import chart_studio
import chart_studio.plotly as py
import mysql.connector
from mysql.connector import Error
import plotly.graph_objects as go
Get some MySQL Data in data frame, do a pivot and flatten into a dataframe here. Basically Source, Target, Count as the table results.
df = df4[df4.Cnt > 120]
Filtered to a minimum amount for the screenshot attached.
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
Remove duplicates from labelList
labelList = list(dict.fromkeys(labelList))
Transform df into a source-target pair
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()
add index for source-target pair
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')
This Plotly needs importation of plotly even though there are other modules imported
import plotly
This opens a new window and has blurred labels
plotly.offline.plot(fig, validate=False)
These work but are inline
plotly.offline.iplot(fig, validate=False)
pio.show(fig)
fig.show()