How to extract data from a sunburst chart html file? - python

Below is the sample code that created the sunburst chart and exported the HTML file
import plotly.express as px
df = px.data.tips()
fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill')
fig.show()
fig.write_html("tips.html")
output:
With reference to the SO question I used the below code to extract the data, when I have the fig
fig.show("json")
print(fig)
output:
My current requirement is to load some sunburst chart HTML output and extract the hovertemplate data from it. I tried loading the HTML file using
from IPython.display import IFrame
IFrame(src='tips.html', width=700, height=700)
I don't know how to proceed after this step.
I have read so many articles and am not sure whether it is possible to do that. Any guidance would be much appreciated.

Related

Plotly Express choropleth map not showing in visual studio code

Below shown the syntax used to get a map visualized and plotted from Plotly Express - choropleth from a "csv" DataFrame.
import pandas as pd
import numpy as np
import plotly.express as px
df = "//location.csv"
fig = px.choropleth(data_frame = df,
locations= df["location"],
locationmode='country names',
color=df["location"],
hover_name=df["location"],
title = "Location Data",
color_continuous_scale = px.colors.sequential.Oranges)
fig["layout"].pop("updatemenus")
fig.show()
However, when I use the above syntax on the Visual Studio Code Jupyter Notebook, the map does not get visualized and plotted. Which is shown as below,
But when I run the same code on the Anaconda Jupyter Notebook, I do get the map visualized and plotted as shown below,
Why isn't the map not getting visualized and plotted on VS code, and is there any way to resolve this issue on VS code?
I was interested in this question because I usually work with jypyterLab. I ran it based on this answer, and when I ran it in vscode, it displayed correctly in my default browser. The code I ran was based on the code in the official reference.
import plotly.express as px
from plotly.offline import plot
df = px.data.gapminder().query("year==2007")
fig = px.choropleth(df, locations="iso_alpha",
color="lifeExp", # lifeExp is a column of gapminder
hover_name="country", # column to add to hover information
color_continuous_scale=px.colors.sequential.Plasma)
# fig.show()
plot(fig)

Plotly Express Heatmap not working properly

I'm a newbie in Python and i need your help for solving this tricky problem..
I created an heatmap graph with seaborn in order to show Project Managers allocation on projects, and it's owrking properly.
ax = sns.heatmap(result_grouped[spike_cols]*100, cmap=mycmap,vmin=0, vmax=200, linewidths=.05,
linecolor='lightgray', cbar_kws={'label': '% ore giorno'}) #"YlGnBu"
I tried to create the same graph with the same data with plotly express, but it seems that it doesn't represent all the data.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
spike_cols = [col for col in result_grouped.columns if '2021-' in col]
fig = px.imshow(result_grouped[spike_cols],
#x=spike_cols,
y=result_grouped.index,
width=900, height=800
)
fig.show()
Sample Data
PM IT|17/02/2021|18/02/2021|19/02/2021|22/02/2021|23/02/2021|24/02/2021|25/02/2021|26/02/2021
ito|0.753503|0.753503|0.752568|0.752568|0.752568|0.614762|0.610097|0.572597
nzi|0.716667|0.716667|0.716667|0.716667|0.716667|0.716667|0.658333|0.633333
Can you please help me?
Solved.
The spike_cols list was not sorted.
After sorting it works.

Stuck on How to Display Hover Data in Specific Format

I am testing some plotly code here.
import plotly.express as px
# find business profits
pd.options.display.float_format = '{:.2f}'.format
df_gains = df_rev_exp[((df_rev_exp.ltd_spending) < df_rev_exp.REV2)]
df_gains.tail()
# scatter plot of losses
import plotly.express as px
fig = px.scatter(df_gains, x="site_name",
y="gain_or_loss",
color="gain_or_loss",
size='REV2', hover_data=['site_name','REV2'])
fig.update_xaxes(tickangle=325)
fig.show()
Everything plots just fine but the REV2 is pretty large, and as such it is hard to read when I hover over the data points in the chart. I'm trying to figure out a way to show numbers as millions. For instance, In would like to see 1.25M and not 1257789.84, which is what I am seeing now. I tried playing around with fig.update but I couldn't get anything working. How can I modify the formatting on these plotly charts?
I'm on Plotly 4.14.3 and this version shows 2.2M straight out of the box when the source is x=[10000000, 22000000, 34000000]:
import numpy as np
import plotly.graph_objects as go
fig = go.Figure()
fig.add_traces(go.Scatter(x=[10*10**6, 22*10**6, 34*10**6],
y=[10,12,14]))
fig.show()
So two things come to mind:
Update Plotly.
Check that you're inputting your values as values and not strings

Chloropleth map using python won't show

I am a new user to Python. I am attempting to create a US county level chloropleth map. To get started I've been reading tutorials on how to do this here . The problem is when I execute the code exactly as it is written in the tutorial, I can't actually see the finished figure. A figure-object is created, but no plot. At the end of the day I am really looking to take this example and apply it to my own data.
Here is the code:
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str})
import plotly.express as px
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="Viridis",
range_color=(0, 12),
mapbox_style="carto-positron",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.5,
labels={'unemp':'unemployment rate'})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
In case you are running your code from a standalone script, you can set the default renderer for plotly by adding the following lines at the beginning of your script:
import plotly.io as pio
pio.renderers.default = "browser"
In this case, your default browser will be used as a renderer. Other renders are available (take a look here).
Moreover, if you are running your code in a jupyter notebook, it is enough to have fig as last command of the cell and the figure will be displayed.

Plotly world Choropleth Map offline in python?

I'm trying to recreate the world Choropleth map given in plotlys example page here: https://plot.ly/python/choropleth-maps/ with the intention of reusing some of the code, but changing the column which informs the shadings and the labelling.
However when I run the exact code given in the example I receive the following error.
plotly.exceptions.PlotlyError: Because you didn't supply a 'file_id' in the call, we're assuming you're trying to snag a figure from a url. You supplied the url, '', we expected it to start with 'https://plot.ly'.
Run help on this function for more information.
I have no idea where this error arises from and my question really is how do I adapt the code so that it produces the said figure offline? Secondly is there a simple method for saving the figure directly to a png? Apologies if this is trivial I'm completely new to the package.
Here is the code:
import plotly.plotly as py
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
data = [dict(
type='choropleth',
locations=df['CODE'],
z=df['GDP (BILLIONS)'],
text=df['COUNTRY'],
colorscale=[[0, "rgb(5, 10, 172)"], [0.35, "rgb(40, 60, 190)"], [0.5, "rgb(70, 100, 245)"],\
[0.6, "rgb(90, 120, 245)"], [0.7, "rgb(106, 137, 247)"], [1, "rgb(220, 220, 220)"]],
autocolorscale=False,
reversescale=True,
marker=dict(
line=dict(
color='rgb(180,180,180)',
width=0.5
)),
colorbar=dict(
autotick=False,
tickprefix='$',
title='GDP<br>Billions US$'),
)]
layout = dict(
title='2014 Global GDP<br>Source:\
<a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
CIA World Factbook</a>',
geo=dict(
showframe=False,
showcoastlines=False,
projection=dict(
type='Mercator'
)
)
)
fig = dict(data=data, layout=layout)
py.iplot(fig,validate=False, filename='d3-world-map')
You need to import the offline specific functions, which allow you to plot inline in a jupyter notebook:
import plotly.figure_factory as ff
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
# All of your code
# ....
# Just change the last line from py.iplot to iplot
iplot(fig,validate=False, filename='d3-world-map')
This renders the image inline in a jupyter notebook, and there's a button that allows you to Download plot as a png in the upper right, along with other functionality.
If you need to save the image as a png, you can try changing the last line to:
plot(fig, validate=False, filename='d3-world-map.html', image='png')
This actually creates an .html file and will open a browser. You can then manually save this as a .png. The last step can be automated with other libraries like selenium, but not sure there is a simple way around it given their documentation:
Note that you must generate the graph and open the file to save the
image.

Categories

Resources