Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Plotly in Power BI

I'm trying to build a Power BI tool for some data analysis, and one of the plots I need is an inverse quantile plot (quantiles on x axis, values on y axis). Power BI does not have this, and I can't find one on the app marketplace, so am using Python to code up what I need.

The static plot from pandas.DataFrame.plot() works fine but lacks the pinache of an interactive plot. I've coded up the plot I need using plotly, and ran it with py.iplot(), but Power BI tells me

No image was created. The Python code didn't result in creation of any visuals. Make sure your Python script results in a plot to the Python default device

There was no error, and I confirmed the code is fine by running the plot using py.plot(), and viewed the result in the browser. My code is:

import plotly.plotly as py
import plotly.graph_objs as go

# get the quantiles and reshape
qs = dataset.groupby(by='HYDROCARBON_TYPE').Q42018_AbsDevi.quantile(q=[0.01,0.05,0.1,0.2,0.25,0.5,0.75,0.8,0.9,0.95,0.99]).unstack().transpose()
# plot it
traces = []
for col in qs.columns:
    traces.append(go.Scatter(x=qs.index, y=qs[col], name=col))
py.plot(traces,filename='basic-line')

Why would this not be working?

like image 721
Dr. Andrew Avatar asked Jan 11 '19 12:01

Dr. Andrew


Video Answer


1 Answers

I wasn't able to find a solution using PowerBI, Plotly and Python, nor was I able to reproduce your errors. Regarding your errors, I ended up with visualizations that were either timed out or reporting a data type error. But we can get back to that if that's still interesting after another suggested solution, because I have been able to produce an interactive q-plot using PowerBI, plotly, ggplot and an R script visual like this:

enter image description here

Assuming that your main priorities are to make an interactive quantile plot in PowerBI, and that Python as a tool comes second, just follow the steps outlined in this post, and replace the R script with this:

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
####################################################

################### Actual code ####################
df <- data.frame(y = Values$Data)

# Build basic ggplot
g <- ggplot(df, aes(sample = y))

# Add quantile details
g = g + stat_qq() + stat_qq_line()

############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################

That should do the trick. Don't hesitate to let me know if this does not work for you.

like image 182
vestland Avatar answered Oct 22 '22 02:10

vestland