webapp using python doesnt render correlation plot

sabsbecool
sabsbecool Registered Posts: 3 ✭✭✭✭
edited July 16 in Using Dataiku

Hi Team,

I wish to use Python to create webapp. Though I do not see any error in the output I am unable to see the graph.

Please suggest.

import dataiku
from bokeh.io import curdoc
from bokeh.plotting import figure

import matplotlib
matplotlib.use("Agg")

from matplotlib import pyplot as plt

import pandas as pd, numpy as np 
import scipy.cluster.hierarchy as sch   # Used for reordering the correlation matrix
import seaborn as sns                   # Graphing
sns.set(style="white")                  # Tuning the style of charts
import warnings                         # Disable some warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)

         

# Take a handle on the dataset
mydataset = dataiku.Dataset("log_calculations")



# Please refer to the Dataiku Python API documentation for more information
df = mydataset.get_dataframe(
    limit = 100000)

# Get the column names
numerical_columns = list(df.select_dtypes(include=[np.number]).columns)
categorical_columns = list(df.select_dtypes(include=[object]).columns)
date_columns = list(df.select_dtypes(include=['<M8[ns]']).columns)

# Print a quick summary of what we just loaded
print "Loaded dataset"
print "   Rows: %s" % df.shape[0]
print "   Columns: %s (%s num, %s cat, %s date)" % (df.shape[1], 
                                                    len(numerical_columns), len(categorical_columns),
                                                    len(date_columns))

# Select variables to plot for the correlation matrix
corr_matrix_vars = numerical_columns[0:50]
print "Plotting the correlation matrix on the following variables : %s" % corr_matrix_vars

#-----COR METRICS----------
# Only select the requested columns
df_corr_matrix = df[corr_matrix_vars]

# This computes the Pearson coefficient for all couples
corr = df_corr_matrix.corr().fillna(0)


# Start drawing


# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

# Set up the matplotlib figure
size = max(10, len(corr.columns)/2.)
f, ax = plt.subplots(figsize=(size, size))

# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, square=True, linewidths=.5, cbar_kws={"shrink": 0.5}, ax=ax)

Best Answers

  • Alex_Combessie
    Alex_Combessie Alpha Tester, Dataiker Alumni Posts: 539 ✭✭✭✭✭✭✭✭✭
    Answer ✓

    Hi,

    This is specific to the way the Bokeh Python library works.

    In short, Bokeh favours its own plotting capabilities over other plotting libraries like matplotlib. In your case where you want to use seaborn, which is actually built on top of matplotlib.

    Unfortunately, Bokeh does not offer a direct way to convert a bokeh.plotting.figure into a matplotlib.figure or seaborn.heatmap. But if you can export your figure as an image, then you can display it with Bokeh See this documentation: https://docs.bokeh.org/en/latest/docs/user_guide/plotting.html#images

    Note that the specific implementation may depend on the Bokeh version you are using. I suggest updating to the latest stable version.

    Hope it helps,

    Alex

  • Alex_Combessie
    Alex_Combessie Alpha Tester, Dataiker Alumni Posts: 539 ✭✭✭✭✭✭✭✭✭
    Answer ✓

    Jupyter is very versatile, it can display a lot of different figures coming from different charting libraries: matplotlib, seaborn, plotly, ggplot, etc.

    Unfortunately, Bokeh web apps are not the same as Jupyter. Bokeh is very specific about the type of charts it can display: https://docs.bokeh.org/en/latest/docs/user_guide/plotting.html#userguide-plotting

    If a solution based on Jupyter is not satisfactory to your need, you will need to export the figure as an image for Bokeh to accept it.

Answers

  • sabsbecool
    sabsbecool Registered Posts: 3 ✭✭✭✭

    Thanks for the detail explanation. This is informative.

    In addition, I could see the plot using the same code in Jupyter. What could be the reason, that I am unable to see the same plot on webapp with the same Jupyter code? For your info, the Jupyter notebook was generated from the dataset via the buildlab.

    sabsbecool_0-1584379177678.png

    With slight modification ofcourse the jupyter had the below import section:

    get_ipython().magic(u'pylab inline')
    import dataiku # Access to Dataiku datasets
    import pandas as pd, numpy as np # Data manipulation
    import scipy.cluster.hierarchy as sch # Used for reordering the correlation matrix
    import seaborn as sns # Graphing
    sns.set(style="white") # Tuning the style of charts
    import warnings # Disable some warnings
    warnings.filterwarnings("ignore",category=DeprecationWarning)

  • ankitlathiya
    ankitlathiya Registered Posts: 1 ✭✭✭

    I agree with Alex. Jupyter Notebook is very versatile, it can display charts differently. Bokeh is very specific about the specific charts.

    Jupyter Notebook is built off of IPython, an interactive way of running Python code in the terminal using the REPL model (Read-Eval-Print-Loop). The IPython Kernel runs the computations and communicates with the Jupyter Notebook front-end interface. It also allows Jupyter Notebook to support multiple languages. Jupyter Notebooks extend IPython through additional features, like storing your code and output and allowing you to keep markdown notes.

Setup Info
    Tags
      Help me…