Interactive matplotlib figures in Google Colab
Below is an example of creating interactive iplot()
in Plotly and cufflinks()
on Google Colab Notebook. Used functions and suggestions from the answer [1, 2]
The key seems to be to include configure_plotly_browser_state()
in the cell that does the plotting.
Code below should work:
Import libraries
import datetime
from datetime import date
import pandas as pd
import numpy as np
from plotly import __version__
%matplotlib inline
import plotly.offline as pyo
import plotly.graph_objs as go
from plotly.offline import iplot
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
cf.go_offline()
Set notebook to false
init_notebook_mode(connected=False)
Create function for Colab
copied from: [1, 2]
def configure_plotly_browser_state():
import IPython
display(IPython.core.display.HTML('''
<script src="/static/components/requirejs/require.js"></script>
<script>
requirejs.config({
paths: {
base: '/static/base',
plotly: 'https://cdn.plot.ly/plotly-1.5.1.min.js?noext',
},
});
</script>
'''))
Create sample dataframe
Data source: Annual rainfuall data for the Peachtree City, GA from National Weather Service [3].
df = pd.DataFrame({
'month': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
'Year_2018': [3.26, 6.11, 4.86, 6.53, 4.45, 3.86, 8.04, 7.59, 1.48, 4.75, 7.27, 11.83],
'Year_1996': [8.26, 3.82, 6.42, 2.91, 2.12, 1.70, 2.14, 4.66, 4.32, 0.89, 3.22, 4.14]
}
)
df
Create an interactive iplot
configure_plotly_browser_state()
df.iplot(kind='line',x='month',y=['Year_2018', 'Year_1996'], color=['white', 'gold'],
theme='solar', mode='markers+lines',title='Annual Rainfall in the city Peachtree City, GA')
plt.show()
Output:
[Note: The x, y, titles do not show up! at the moment.]
Problem with Logging Module in Google Colab
Perhaps you've reconfigured your environment somehow? (Try Runtime menu -> Reset all runtimes...) Your snippets works exactly as written for me --
Problem using AddMoleculeColumnToFrame on Google Colab
Seems like this is a problem with all pandas versions above 0.25.0, So I guess for now the easiest fix is to downgrade pandas. Or you can use this method which seemed to work for me:
from IPython.display import HTML
HTML(df.to_html())
https://colab.research.google.com/drive/1eP8VZdr61DIoYXRz3PfwmVeG71GRzsRG
I am not quite sure why but this also seems to work:
def display_mol(x):
if isinstance(x, Mol):
return x
return x
df.style.format(display_mol)
Screenshot:
Related Topics
Distorted Spacing Between Div Elements After Sorting with Jqui_Sortable
Internet Explorer 10+ Media Queries and Responsive Break Points
How to Check If a Browser Supports Shadow Dom
Align Checkboxgroupinput Vertically and Horizontally
Use the ::Before Generated Content Selector on an Iframe Element
Checking Hardware Acceleration Availability? (Testing Available)
CSS Selectors - Selecting a Specific Child Element
Best Replacement for Font Tag in HTML
Div Table-Cell Vertical Align Not Working
Angular Incorrect Margin-Left Applied to Sidenav Content
CSS Attribute Selector + Descendant Gives a Bug in Webkit
How to Create a Div with a Diagonal (Or Angled) Top Line
Cannot View the Source Image File on a Website
Margin to Apply to Position Div After Skewing with CSS Transform