Problems creating a categorical histogram in Bokeh and Pandas - python

I am trying to produce a simple histogram of categorical data with the following code
import pandas as pd
from bokeh.plotting import figure, show
# some fake categories and count data
counts = pd.Series({'Cat0':1599, 'Cat1':1357, 'Cat2':671,
'Cat3':610, 'Cat4':446, 'Cat5':210})
# pull out the categories from the index
cats = list(counts.keys())
plt = figure(x_range=cats)
plt.vbar(cats, top=list(counts.values), width=2, line_color='green')
show(plt)
but instead of a plot I get
Javascript error adding output!
Error: Error rendering Bokeh model: could not find tag with id: e7346df5-7d3d-4f34-92e2-9e59eb36ec41
See your browser Javascript console for more details.
Is this a bug or have I specified something wrong?
I am using Firefox 54.0 running on Ubuntu (kernel 4.10.0). Other Bokeh plots run without a problem. I am outputting them inline to a Jupyter notebook.
Bokeh is bokeh-0.12.4-py3.6.

Not sure what is causing the problem, but re-executing
from bokeh.plotting import figure, show, output_notebook
and re-running
output_notebook()
solves the problem and plots again show in the notebook.

Related

Unable to insert altair visualisation as popup in folium map

I'm relatively new to python and this is the first project that I'm working on. I'm trying to plot a simple multi line chart using altair onto a folium marker's popup. I followed the examples on this page https://github.com/python-visualization/folium/blob/master/examples/Popups.ipynb. But when I click on the marker, it's just plain white. The map and markers are plotted fine except the popup.
This is what my whole dataset looks like :
Here is the code:
import pandas as pd
import folium
import altair as alt
import json
chart = alt.Chart(df).mark_line().encode(
x='index',
y='Ambala',
color='Variable')
chart_2 = json.loads(chart.to_json())
m = folium.Map([30,-80],zoom_start=2)
popup = folium.Popup(max_width=650)
folium.Vega(chart_2, height=350, width=650).add_to(popup)
folium.Marker([30, -80], popup=popup).add_to(m)
m
Note that this just a sample of code. I'll be implementing this on a larger scale in my project.
I tried to plot a visualization (which I found here : https://github.com/python-visualization/folium/blob/master/examples/data/vis1.json) and this seemed to work fine. I just don't understand why the same code works for their plot but not for mine even though altair produces a fine looking chart from my code. Sorry if this is sounds silly, I'm still a beginner and python is my first language. Thanks
If you want to use an Altair chart inside a folium map, you need to use a VegaLite object instead of a Vega object. You just need to change the line with the Vega object like that : folium.features.VegaLite(chart_2, height=350, width=650).add_to(popup)

Update plotly chart in jupyter notebook

I'm creating a plotly chart in jupyter notebook. Because I'm testing some algorithm I want to add data after the initial fig2.show(). But when I update the data and call fig2.show again a new chart is being rendered. How can I update the chart instead of creating a new chart?
This should be an easy task - but it's way to hard to find for me in the documentation.
import plotly.offline as pyo
import plotly.graph_objs as go
import numpy as np
# Set notebook mode to work in offline
pyo.init_notebook_mode()
fig2 = go.Figure(data=go.Scatter(x=moreX2, y=moreY2))
fig2.show()
Then I'm using update:
moreX2.append(2)
moreY2.append(5)
fig2.data[0].update({"x": moreX2, "y": moreY2})
The chart isn't being rerendered and I tried to call fig.show() again, which just creates a new chart.
This answer worked for me, and seems like the ideal answer to this question as well. I've voted to mark this as a duplicate.
To summarize: by wrapping the Figure in a FigureWidget, we can now do exactly what the OP wanted. Although on Colab I did have to give some extra permission by calling the following:
from google.colab import output
output.enable_custom_widget_manager()

How to save a Plotly graph in a ipynb file?

I understand that it is possible to export a Plotly graph, and that I can display it.
While sharing notebooks, the matplotlib plots remain intact in the Jupyter Notebooks, however, the Plotly graphs do not. They simply disappear
I understand that the Plotly graph is browser rendered, but is there any way I can store the graph in the ipynb file when I export it?
Is there any way that I can display the Plotly graph, just like the matplotlib graph?
Edit: As suggested in an answer, I tried to save it to a figure object, and display that, but no luck there either :/
If you put the code for a series of graphs in a single cell, execute it and save it, I think it will be displayed the next time you open it.
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()

Holoview plots do not show/appear/display in jupyter notebooks

I'm trying to make a Holoviews Sankey plot in Jupyter notebook (running Jupyter notebook using Pycharm), but the Sankey plot does not appear after the block is executed.
First I import required packages and set 'bokeh' as Holoviews extension. Using holoviews 1.10.9 and jupyterlab 0.35.4.
import pandas as pd
import holoviews as hv
hv.extension('bokeh')
After performing some data wrangling, I prepare a dataframe 'private_investments_grouped' with 3 columns (source, target, value).
I then execute the following code to make the Sankey plot for a sub-dataframe:
hv.Sankey(private_investments_grouped.loc[1:7]).options(label_position='left')
No plot appears in the output block, no pop-ups, no error messages. It appears the code executes, but no visualization appears.
I can use the 'bokeh' renderer in a Python file in Pycharm and store the Sankey plot as a html file, which works fine. However, I'd like to see the plot in Jupyter notebook.

Seaborn Plot doesn't show up

I am creating a bar chart with seaborn, and it's not generating any sort of error, but nothing happens either.
This is the code I have:
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
data = pandas.read_csv('fy15crime.csv', low_memory = False)
seaborn.countplot(x="primary_type", data=data)
plt.xlabel('crime')
plt.ylabel('amount')
seaborn.plt.show()
I added "seaborn.plt.show() in an effort to have it show up, but it isn't working still.
You should place this line somewhere in the top cell in Jupyter to enable inline plotting:
%matplotlib inline
It's simply plt.show() you were close. No need for seaborn
I was using PyCharm using a standard Python file and I had the best luck with the following:
Move code to a Jupyter notebook (which can you do inside of PyCharm by right clicking on the project and choosing new - Jupyter Notebook)
If running a chart that takes a lot of processing time it might not have been obvious before, but in Jupyter mode you can easily see when the cell has finished processing.

Categories