I created a jupyter notebook with interactive plots using Bokeh.
An example notebook looks like this:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.charts import ColumnDataSource
from bokeh.io import output_file
from bokeh.models import HoverTool
df = pd.DataFrame(np.random.normal(0,5,(100,2)),columns=['x','y'])
output_notebook()
source = ColumnDataSource(df)
hover = HoverTool(
tooltips=[
("x", "#x"),
("y", "#y"),
]
)
p = figure(plot_width=800, plot_height=500, tools=[hover])
p.circle('x', 'y', size=7, fill_alpha=0.5,source=source)
show(p)
Things work on the notebook itself and the figure is interactive.
I'm using pelican static website generator with the pelican-ipynb plugin (https://github.com/danielfrg/pelican-ipynb) in order to convert the notebook to html. When the html is created the Bokeh plots don't show up. I can't seem to figure out how to get an html with the interactive Bokeh plots. I inspected the html and there is nothing after the show(p) line.
How do I get the Bokeh plot work with pelican?
Bokeh uses JavaScript for the client-side of the interactive part (BokehJS) and that JS code is not embedded when you export the notebook to HTML ouside Bokeh.
You need to export the Bokeh code to HTML using Bokeh’s own functions to generate standalone HTML.
To generate the HTML, simply add to your code:
from bokeh.resources import CDN
from bokeh.embed import file_html
p_html = file_html(p, CDN)
For an example, see this Jupyter Notebook with your original code and the generated HTML (too long to embed it here in SO, about 45 K characters for your simple example).
Related
I'm creating plots in a loop in Jupyter notebook. Is it possible to auto generate markdown heading within a loop before every figure so that the headings can show up in jupyter toc or vscode outline for easy navigation?
import matplotlib.pyplot as plt
import numpy as np
for i in range(1,5):
#add markdown heading here
plt.figure()
x = np.arange(10)*i
y = np.sin(x)
plt.plot(x, y)
You can change the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code. To use the Keyboard Shortcuts, hit the esc key. After that, you can change a cell to Markdown by hitting the m key, or you can change a cell to Code by hitting the y key
see this image for reference
In org-mode, I can name the output of a code block and include it elsewhere in the document.
Is it possible to do this (or something similar) in a colab .ipynb file, or within a Jupyter notebook in general?
For example, I can make a cell with some Python code that generates a plot:
import matplotlib.pyplot as plt
plt.plot([0,2,1,4,9])
After executing, the plot appears below the code cell. So far so good.
But how do I capture this plot and to use it elsewhere in the notebook?
My hope is there is some syntax so that I can include the plot in a markdown cell, for example:
# this is my title
As you can see, the numbers go up and down in the plot:
![][some_magic_link_here]
Here is some more text to explain the plot.
Does such a feature exist in colab?
Good news - embedding an image in another markdown cell is self-service. Here's an example:
Full notebook:
https://colab.research.google.com/drive/1PF-hT-m8eZN2CBzFkMp9Bvzj9pSBQVYn
The key bits are:
Using IPython.display.Markdown is order to programmatically construct the markdown.
In addition to rendering, save the matplotlib figure using savefig.
Include the image data in the markdown after base64 encoding.
I am creating some documentation using Sphinx and I want to use the bokeh.sphinxext to include Bokeh plots with the documentation. This seems like an easy thing to do given this example. However, the data that I want to use to generate the plot is stored within a CSV file. I've tried putting my CSV file into the same directory, and using .. include:: data.csv in the same RST document, but that didn't work.
How can I reference an external file when creating Bokeh plots using Sphinx?
.. include:: data.csv
.. bokeh-plot::
import pandas as pd
from bokeh.plotting import figure, output_file, show
output_file("example.html")
df = pd.read_csv('data.csv')
p = figure(title="example", plot_width=300, plot_height=300)
p.line(df['x'], df['y'], line_width=2)
show(p)
Just because of how the extension operates, the current working directory when the code executes (i.e. what is returned by the os.getcwd() function) is the top level of your Sphinx project. You will need to construct a path to your data file based on that. E.g. if your file is under the source/docs directory of your Sphinx project, it might be:
df = pd.read_csv(os.join('source', 'docs', 'data.csv')
To use a dataset in Bokeh, it is common to import pandas as well and use a Pandas DataFrame as data. It is also possible to create ColumnDataSource from Pandas DataFrame.
But is it possible to go straight from a csv file (or any other tabular source data) to Bokeh without creating using Pandas as a bridge, and how?
Bokeh ColumnDataSource are built directly from python dictionaries. Simply use:
ColumnDataSouce(data=dict(...))
Without knowing your specific data format, I'd refer to you to use many of the other answers on SO on reading a csv file and creating a Python dictionary. You can also take a look at the official docs on DictReader.
I have an ipython notebook and I want to write a function to display matrixes:
from IPython.display import display
import sympy
sympy.init_printing()
def print_matrix(a):
display(sympy.Matrix(a))
import numpy as np
print_matrix(np.random.random((20, 20)))
This is producing a mathml output which is very large on my screen. How to control the size? I don't want to truncate numbers, I want to decrease the size of the fonts / size of the output image.
I have accomplished it with
sympy.init_printing(use_latex='png', fontsize='5pt')
but I don't like the png output, I would like the default mathjax
I won't save with the notebook (you'll probably need to execute some Javascript in a cell to do that), but if you right click on an equation there is an option to scale it up or down (this is a MathJax feature, which has nothing to do with the IPython notebook).