Search code examples
pythonmatplotlibpowerbiseabornpowerbi-custom-visuals

How to make python chart in Power Bi website version look like in desktop version?


I am preparing heatmap in Power Bi using python and seaborn. It is looking quite fine in desktop version of this software but when I publish report it cutting of part of Y axis labels and changing size of chart.

Power Bi Desktop: desktop version

Online report: online report

Here is my code:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

if not dataset.empty:
    dataset['Skill Name'] = dataset['Skill Name'].str.replace(r"\(.*\)","")
    dataset['Skill Name'] = dataset['Skill Name'].str.replace(r"\-\-.*","")

    skill_name_dir = {
        "including":"inc",
        "Documentation":"Doc",
        "Design":"Dsgn",
        "Process":"Proc",
        "Architecture":"Arch",
        "Frequency":"Freq",
        "Engineering":"Engr",
        "Hardware":"Hdw",
        "Software":"Sw",
        "Power":"Pwr",
        "Termination":"Term",
        "Electromechanical":"Elmech",
        "Requirements":"Req"
    }

    for word, initial in skill_name_dir.items():
        dataset['Skill Name'] = dataset['Skill Name'].str.replace(word,initial)

    dataset['Category'] = dataset['Category'].str.replace(r"^Domain[A-Za-z\s\S]*", "Dom", regex=True)
    dataset['Category'] = dataset['Category'].str.replace(r"^Process[A-Za-z\s\S]*", "Proc", regex=True)
    dataset['Category'] = dataset['Category'].str.replace(r"^Product[A-Za-z\s\S]*", "Prod", regex=True)
    dataset['Category'] = dataset['Category'].str.replace(r"^Tools[A-Za-z\s\S]*", "Tool", regex=True)
    dataset['Category'] = dataset['Category'].str.replace(r"^Organization[A-Za-z\s\S]*", "Org", regex=True)
    dataset['Category'] = "(" + dataset['Category'] + ")"

    dataset['Manager Name'] = dataset['Manager Name'].str.replace('[^A-Z]', '', regex=True)
    dataset['Manager Name'] = "(" + dataset['Manager Name'] + ")"
    dataset['Employee Name'] = dataset['Manager Name'] + dataset['Employee Name']

    dataset = dataset.sort_values(['Category', 'Skill Name', 'Employee Name'], ascending=[True, True, False])
    dataset['Skill Name'] = dataset['Category'] + dataset['Skill Name']
    dataset['Score'] = dataset['Score'].replace(-1,np.nan)

    heatmap_data = pd.pivot_table(dataset, values='Score', index=['Skill Name'], columns='Employee Name')

    #plt.figure(figsize=(30,15))
    fig = plt.gcf()
    figsize = fig.get_size_inches()
    fig.set_size_inches(figsize*1.4)

    hm = sns.heatmap(
        heatmap_data,
        vmin=0,
        vmax=5,
        #annot=True,
        linewidths=0.01,
        square=True,
        cmap="RdYlGn"
    )

    hm.xaxis.set_ticks_position('top')

    hm.set_xticklabels(
        hm.get_xticklabels(),
        rotation=60,
        horizontalalignment='left',
        fontsize = 7,
        wrap = True
    )

    hm.set_yticklabels(
        hm.get_yticklabels(),
        fontsize = 7,
        #wrap = True
    )
    hm.set_ylabel('')    
    hm.set_xlabel('')
else:
    fig = plt.figure(figsize=(5, 1.5))
    text = fig.text(0.5, 0.5, 'No data to display.\nPlease change filters \nto display chart.', ha='center', va='center', size=20)
plt.tight_layout()
plt.show()

Any ideas why chart is different than in desktop version?


Solution

  • Issue was caused by version 3.7 of python. Change to version 3.5 fixed this.