Graphvis output is not shown in Colab - google-colaboratory

Colab is not showing the output of a Graphviz plot (for example https://graphviz.readthedocs.io/en/stable/examples.html). How can I achieve it simply in Colab? I even tried to plot it's generated PDF file but it's not straightforward.

You don't need to call g.view(). Just end the code with g is enough, like this:
g = Digraph('G')
g.edge('Hello', 'World')
g
Here's a minimal example notebook

Related

How to increase length of ouput table or dataframe in Jupyter Notebook?

I am working on the Jupyter notebook and have been facing issues in increasing the length of the output of the Jupyter Notebook. I can see the output as follows:
I tried increasing the default length of the columns in pandas with no success. Can you please help me with it?
If you were using the typical way to view a dataframe in Jupyter (see my puzzelment about your screenshot in my comments to your original post) it would be things like this:
adapted from answer to 'Pretty-print an entire Pandas Series / DataFrame'
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
display(df)
(Note that will work with the text-based viewing, too. Note it uses print(df) in the answer to 'Pretty-print an entire Pandas Series / DataFrame'.
Adjust the 'display.max_colwidth' if you want the entire column text to show:
with pd.option_context('display.max_rows', None, 'display.max_columns', None,'display.max_colwidth', -1):
display(df)
(If you prefer text like you posted, replace display() with print()
Generally with the solutions above the view window in Jupyter will get scrollbars so you can navigate to view all still.
You can also set the number of rows to show to be lower to save space, see example here.
You may also be interested in Pandas dataframe hide index functionality? or Using python / Jupyter Notebook, how to prevent row numbers from printing?.
As pointed out here, setting some some global options is covered in the Pandas Documentation for top-level options.
For display() to work these days you don't need to do anything extra. But if your are using old Jupyter or it doesn't work then try adding towards the top of your notebook file and running the following as a cell first:
from IPython.display import display

How to keep visualizations (i.e. draw('mpl')) displayed while other code is called

I would like to be able to save and call a visualization, then have it show in-between other code. I know with MatPlotLib, you can do plt.show(), is there anything like that for Qiskit?
def test():
return plot_bloch_multivector(circ)
print("This is what the plot looks like")
test()
print("See? Anyway...")
# more code
Output:
This is what the plot looks like
See? Anyway...
Desired output:
This is what the plot looks like
# O O O (displayed bloch multivector)
See? Anyway...
I am using Google Colab (ver. 3.7.15).
In any jupyter notebook (including Google Colab), you can display an object with display
def test():
return plot_bloch_multivector(circ)
print("This is what the plot looks like")
display(test()) # <----------------------
print("See? Anyway...")

Using ggplot2 with Julia pluto notebook

I am working with a Pluto.jl notebook. I would like to use the ggplot2 R library to make some plots.
Following this example, if I run the following code in the Julia REPL then I can get a ggplot2 graph output.
using RCall
#rlibrary ggplot2
using DataFrames
df = DataFrame(v = [3,4,5], w = [5,6,7], x = [1,2,3], y = [4,5,6], z = [1,1,2])
ggplot(df, aes(x=:x,y=:y)) + geom_line()
Now, when I use the same code in a pluto.jl notebook (with each line being a separate cell), then I get the following error message:
Is there a way to get the ggplot2 image to appear inside the pluto notebook?
Similarly, if I just enter ggplot() into a cell, I get the same error, but ggplot not defined.
With #library Pluto.jl seems to be unable to find the R package.
However Pluto can handle this format:
#rimport ggplot2 as ggplot2
I managed to see the picture after clicking the "play" button 3 or 4 times. That is the end of good news - the Plut-RCall integration is kind of unstable. The graph shows in a separate Window that seems to hang - this is perhaps a story for opening an issue.
However what you can try to do is to save the image to a file and than visualize it:
begin
ggplot2.ggplot(df, ggplot2.aes(x=:x, y=:y)) + ggplot2.geom_line()
ggplot2.ggsave("myplot.png")
im1 = Images.load("myplot.png")
end
As a workaround, it is possible to override Base.show manually (see Pluto.jl/sample/test1.jl) with
function Base.show(io::IO, ::MIME"image/png", p::RObject{VecSxp})
(path, _) = mktemp()
R"ggsave($path, plot=$p, device = 'png')"
im = read(path)
rm(path)
write(io, im)
end
After that, cells which output anything of the type RObject{VecSxp} will show a PNG image:

folium choropleth and geojson not rendering in jupyter

I cannot get a folium map to display in jupyter when all 33 London boroughs are included in the geojson file
but
I can get the folium map to display if fewer boroughs are included in the geojson file. (up to 23)
If I save the map as an html file and open it separately it works just fine.
here is the version of the code that works (just using the first 23 boroughs).
m = folium.Map(location=[51.5, -0.1], zoom_start=10)
m.choropleth(
geo_data={"type":geo_london["type"],"features":geo_london["features"][:23]}, # 23 of the boroughs
data=df["Underground"],
columns=["LA",'Underground'],
key_on='feature.properties.name',
fill_color='BuPu',
fill_opacity=0.9,
line_opacity=0.2,
legend_name='Underground Useage',
highlight=True
)
Here is the version that doesn't work:
m = folium.Map(location=[51.5, -0.1], zoom_start=10)
m.choropleth(
geo_data= geo_london, # all 33 boroughs
data=df["Underground"],
columns=["LA",'Underground'],
key_on='feature.properties.name',
fill_color='BuPu',
fill_opacity=0.9,
line_opacity=0.2,
legend_name='Underground Useage',
highlight=True
)
Other things to note:
I parsed the geojson file using json within python so geo_london is a
dictionary
if I do m.save('mymap.html') and open the map the second
version also works fine.
I have the same problem if I don't use the data in a
chorepleth but instead use folium.GeoJson(geo_london).add_to(m)
folium 0.5.0
the data is a pandas data series
Probably you are describing the bug explained here https://github.com/python-visualization/folium/issues/768 (Folium displays nothing if number of overlaid images > 80 on Chrome).
Try to use a different browser like Firefox or Safari.

Some inline plots won't print from Ipython noteboook

I am using Ipython Notebook to generate some bar plots.
The code cell is this:
kcount =0;for k, v in pledge.groupby(['Date','Break']).sum().Amount.iteritems():
if k[0] <> kcount:
kcount=k[0]
pledge[pledge.Date==k[0]].groupby(['Break','Progcode'])['Amount'].sum().plot(kind='bar')
plt.title(k[0])
plt.figure()
This gives me a bar plot for every day of our pledge drive, showing how each show within that day did. 24 charts in all. They display great as output on the screen, but when I use the Print button in Ipython Notebook, it only prints enough graphs to fill the last page, which can vary from 3 to 6 graphs depending on the printer used. One printer used reported that it required 11x17 paper for the print job (not something I set anywhere) and when I manually set it for 8 1/2 x 11, it again only printed out the first 3 pages. I am at a loss as to what to do at this point.
As a workaround, can you can use plt.savefig('filename.png') (or .jpg, or .whatever) to save an image file and then print the files out manually?
I ended up saving these pages to a multipage PDF file and then printing them from there.
Consult the docs http://matplotlib.org/api/backend_pdf_api.html
To see how to save several figures to a multipage PDF file.
This also looks like a good resource. http://blog.marmakoide.org/?p=94