How to zoom in graphs in colab - python

I am able to get an interactive graph in Google colab with the code:
!pip install mpld3
%matplotlib notebook
mpld3.enable_notebook()
df.plot(x = 'Time', y = 'Data')
but the plot is really small.
In R you can click on the graph and it will reopen in the new window. Is there a similar method to do this with colab? Thanks.

google-colaboratory does not provide any native data visualization, but it does support a variety of third-party visualization packages (matplotlib, altair, bokeh, plotly, etc.) You should look in the documentation of the library you are using to see how to adjust the size of figures.
In your example, you appear to be using pandas matplotlib plotting API. As the documentation mentions, you can adjust plot sizes with the figsize argument:
df.plot(x='Time', y='Data', figsize=(16, 10))

mpld3 is an interactive module for matplotlib.
You can understand its working here.
A simplifies answer to the same could be
import matplotlib.pyplot as plt
import mpld3
from mpld3 import plugins
fig, ax = plt.subplots()
ax.grid(True, alpha=0.3)
plt.plot(x, y)
mpld3.display()
where x & y are arrays of corresponding axis values.

Related

How to close matplotlib figures in jupyter notebook without conflicting with the backend?

I have written a small library, whose functions generate plots using this structure:
from matplotlib import pyplot as plt, rcParams, rc_context
with rc_context():
print(f'Figure backend {plt.get_backend()}')
x = [1,2,3,4,5]
y = [1,2,3,4,5]
fig, ax = plt.subplots()
ax.scatter(x,y)
plt.show()
plt.close(fig)
I would like this library to be compatible with jupyter notebooks but I am having issues with the backends.
For example: In "qt" backends the plot window is closed inmediatly and in "nbAgg" backends the plot is deleted. This code reproduces the issue.
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt, rcParams, rc_context
%matplotlib notebook
with rc_context():
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
ts = ts.cumsum()
ts.plot()
plt.close()
One solution is removing the plt.close(fig) but that would leave the figures opened.
Another option is adding some criteria to keep/close the figures depending on the backend, but from what I have seen the nomenclature changes from one OS to another, or if running from a notebook. The latter option is not easy though.
I wonder if anyone would please share their experience to keep the matplotlib figures in notebooks.

How to make group bars on jupyter notebook?

I am trying to plot multiple bars on the same plot on my jupyter notebook. However, due to some reason, it does not work on the notebook,but it works on a normal python editor. Here is the code that I have used.
import matplotlib.pyplot as plt
plt.bar(['A','B','C'],[72,73,88])
plt.bar(['A','B','C'],[98,77,98])
plt.show()
Any help will be highly appreciated.
(Edit)
I am looking for somthing like this on my jupyter notebook.
I presume you want to have both bar plots side by side. So here you go:
import matplotlib.pyplot as plt
fig,axs = plt.subplots(1,2)
axs[0].bar(['A','B','C'],[72,73,88])
axs[1].bar(['A','B','C'],[98,77,98])
plt.show()

Scale plot size of matplotlib plots in Qubole Notebook

Is there a possibility of increasing the size of the plot plotted using z.showplot() in qubole notebooks.
import matplotlib as plt
plt.figure()
plt.bar(pandas_df_hr_sg[:]['hour'],pandas_df_hr_sg[:]['count'])
plt.title('Hourly wise user visits')
plt.xlabel('Hour')
plt.ylabel('Visit Count')
z.showplot(plt)
Could you try setting the plot size by:
plt.figure(figsize=(8, 8))
This should ideally help in case of Zeppelin notebook in Qubole; let me know how this goes!

%matplotlib notebook shows blank icons in Jupyter notebook

I'm trying to create a simple interactive plot with sliders and matplotlib in Jupyter, however, the icons are simply showing a blank square instead? Figure below:
Blank icons using notebook backend
Am I missing a dependency? I believe I'm using default settings for everything.
A simplified version of my code is shown below:
%matplotlib notebook
import matplotlib.pyplot as plt
from ipywidgets import FloatSlider, interact
fig = plt.figure(figsize=(10,3))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('time [s]')
ax.set_ylabel('displacement [cm]')
x1, = ax.plot(t,x)
def update(P_l = FloatSlider(min=0,max=4000,step=50,value=1500)):
x1.set_ydata(x)
fig.canvas.draw()
interact(update)
Thanks in advance!
Jupyter Notebook tends to minimize heavy graphs and tables when it exceeds a specific size. Not sure about the accurate answer, But did you try double clicking the left edge of graph? It does the job in my windows PC.

Pyplot/Subplot APIs Matplotlib

I'm making something using Matplotlib where I have multiple subplots on a figure. It seems to me like the subplot API is limited compared to the PyPlot API: for example, I can't seem to make custom axes labels in my subplot although it is possible using PyPlot.
My question is: Is there a richer subplot API besides the tiny one on the PyPlot page (http://matplotlib.org/api/pyplot_api.html), and/or is there a way to get the full functionality of a PyPlot on a subplot?
Basically, what is a subplot? I can't find it in the documentation. Even more generally, when should I use a figure vs an axis vs a subplot? They all seem to do essentially the same thing.
Consider the following code:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
Then ax is an axis? Can I use the pyplot API to customize ax?
Thanks for your help.
While i suggest that use the axes methods, there is the plt.sca function (set current axes).
So
plt.sca(ax)
does what you want, i think.

Categories

Resources