Issue with GridspecLayout (Ipywidgets) on Colaboratory - google-colaboratory

I'm trying to place two graphs in individual cells of the grid, created using GridspecLayout from Ipywidgets. However, the plots appear to the left of the interactive controls, instead of below as supposed to.
This picture here shows the issue in Colaboratory, while this picture here shows how I expect it to look (from Jupyter Notebook via Anaconda)
The code below reproduces both pictures, depending on the front end used.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import GridspecLayout,HTML,Layout,interactive_output
grid = GridspecLayout(5,8)
testTable = pd.DataFrame({'a':np.random.randn(10),
'b':np.random.randn(10),
'c':np.random.randn(10),
'd':np.random.randn(10),
'e':np.random.randn(10)})
def plotData(x):
plt.figure()
if len(x) > 1:
plt.plot(testTable.loc[list(x)])
else:
plt.plot(testTable.loc[x])
plt.show()
# header
grid[0,:] = widgets.HTML(value = f"<center><font size = 6><b><font color='red'>{'Historical Overview'}</b></center>")
# Sub-headlines
grid[1,2] = widgets.HTML(value = f"<center><font size = 2><u><font color='red'>{'Historical Development Graph'}</u></center>")
grid[1,4] = widgets.HTML(value = f"<center><font size = 2><u><font color='red'>{'Upper Pie Chart'}</u></center>")
grid[1,6] = widgets.HTML(value = f"<center><font size = 2><u><font color='red'>{'Lower Pie Chart'}</u></center>")
# Interactive controls and descriptions
grid[2,1] = widgets.HTML(value = f"<center><font color='red'>{'Categories:'}")
grid[2,2] = widgets.SelectMultiple(options=testTable.index,
disabled=False,
value = [0])
grid[2,3] = widgets.HTML(value = f"<center><font color='red'>{'Time periods:'}")
grid[2,4] = widgets.SelectMultiple(options=testTable.index,
value = [0],
disabled = False)
grid[2,5] = widgets.HTML(value = f"<center><font color='red'>{'Time periods:'}")
grid[2,6] = widgets.Select(options=testTable.columns,
value = 'a',
disabled = False)
# Graphs
grid[3,1:4] = widgets.interactive_output(plotData, {'x':grid[2,2]});
grid[3,4:8] = interactive_output(plotData, {'x':grid[2,4]});
display(grid)
I'm working on Colaboratory and Jupyter Notebook via Anaconda, both via Chrome on Windows 10.
This has been tested on JupyterLab via Firefox on Mac as well, which is why I suspect it to be an issue with Colaboratory.
In advance, thank you.

If I copy/paste your code here
https://colab.research.google.com/notebooks/empty.ipynb
It displays as you wanted (I believe)
However I modified the grid a bit, as
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import GridspecLayout,HTML,Layout,interactive_output
grid = GridspecLayout(10,3)
testTable = pd.DataFrame({'a':np.random.randn(10),
'b':np.random.randn(10),
'c':np.random.randn(10),
'd':np.random.randn(10),
'e':np.random.randn(10)})
def plotData(x):
plt.figure()
if len(x) > 1:
plt.plot(testTable.loc[list(x)])
else:
plt.plot(testTable.loc[x])
plt.show()
# header
grid[0,:] = widgets.HTML(value = f"<center><font size = 6><b><font color='red'>{'Historical Overview'}</b></center>")
# Sub-headlines
grid[1,0] = widgets.HTML(value = f"<center><font size = 2><u><font color='red'>{'Historical Development Graph'}</u></center>")
grid[1,1] = widgets.HTML(value = f"<center><font size = 2><u><font color='red'>{'Upper Pie Chart'}</u></center>")
grid[1,2] = widgets.HTML(value = f"<center><font size = 2><u><font color='red'>{'Lower Pie Chart'}</u></center>")
# Interactive controls and descriptions
grid[2,0] = widgets.HTML(value = f"<center><font color='red'>{'Categories:'}")
grid[3,0] = widgets.SelectMultiple(options=testTable.index,
disabled=False,
value = [0])
grid[2,1] = widgets.HTML(value = f"<center><font color='red'>{'Time periods:'}")
grid[3,1] = widgets.SelectMultiple(options=testTable.index,
value = [0],
disabled = False)
grid[2,2] = widgets.HTML(value = f"<center><font color='red'>{'Time periods II:'}")
grid[3,2] = widgets.SelectMultiple(options=testTable.index, value = [0], disabled = False)
#widgets.Select(options=testTable.columns, value = 'a', disabled = False)
# Graphs
grid[4:,0] = interactive_output(plotData, {'x':grid[3,0]});
grid[4:,1] = interactive_output(plotData, {'x':grid[3,1]});
grid[4:,2] = interactive_output(plotData, {'x':grid[3,1]});
display(grid)
And it shows

Related

Interference between the Matplotlib Graphs in Tkinter GUI

I learned how to embed a matploblib graph into a Tkinter GUI via this post https://matplotlib.org/examples/user_interfaces/embedding_in_tk.html. It seems working fine.
However, I had a problem with getting multiple matplotlib graphs to work correctly simultaneously, let me explain a bit more here. The code below generates two buttons, each links to a new window with two new buttons (load data and plot data). You may generate some dummy two column data, say data1.txt, data2.txt, data3.txt, data4.txt, for plotting. What I observed is (1) If I invoke the window separately, I have no problem loading and plotting data1.txt, data2.txt, data3.txt, data4.txt. However, if I open the two windows at the same time, I can only plot freely and correctly in the second window, while the first window plots nothing. It seems as if the first window were suppressed by the existence of the second.
Can anyone help me understand what is happening here. My matplotlib version is 2.0.2. Tkinter version is $Revision:81008$. Python version 2.7.15. Thank you!
from Tkinter import *
import Tkinter as tk
import ttk
import tkFileDialog
import numpy
##loading matplotlib modules
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.gridspec as gridspec
class Overall_Look:
def __init__(self, master):
self.master = master
self.top_frame = ttk.Frame(self.master, padding = (10, 10))
self.top_frame.pack()
##here are the layout for step 1, load structure files
ttk.Button(self.top_frame, text = "Button_1", command = self.plot_one,
style = "TButton").grid(row = 1, column = 0, columnspan = 2, padx = 5, sticky = "sw")
ttk.Button(self.top_frame, text = "Button_2",command = self.plot_two,
style = "TButton").grid(row = 1, column = 2, columnspan = 2, padx = 5, sticky = "sw")
def plot_one(self):
self.plot_one = tk.Toplevel(self.master)
self.GUI = Plot_One(self.plot_one)
def plot_two(self):
self.plot_two = tk.Toplevel(self.master)
self.GUI = Plot_Two(self.plot_two)
class Plot_One():
def __init__(self, master):
self.master = master
self.top_frame = ttk.Frame(self.master, padding = (10, 10))
self.top_frame.pack()
##here are the layout for step 1, load structure files
ttk.Button(self.top_frame, text = "Load Data 1", command = self.load_data_1,
style = "TButton").grid(row = 1, column = 0, columnspan = 2, padx = 5, sticky = "sw")
ttk.Button(self.top_frame, text = "Plot Data 1",command = self.start_plot_one,
style = "TButton").grid(row = 1, column = 2, columnspan = 2, padx = 5)
self.bottom_frame = ttk.Frame(self.master, padding = (10, 10))
self.bottom_frame.pack()
self.fig_1 = plt.figure(figsize=(5, 5), dpi=100) ##create a figure; modify the size here
self.fig_1.add_subplot(111)
self.fig_1.tight_layout()
self.canvas = FigureCanvasTkAgg(self.fig_1, master = self.bottom_frame)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.bottom_frame)
self.toolbar.update()
self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def load_data_1(self):
self.data_1 = tkFileDialog.askopenfilename(defaultextension = ".txt",
filetypes = [("Text Documents", "*.txt")])
def start_plot_one(self):
data = numpy.loadtxt(self.data_1).transpose()
x = data[0]
y = data[1]
self.fig_1.clf()
self.fig_1.add_subplot(111)
plt.plot(x, y, 'b-', lw=2)
self.fig_1.tight_layout()
self.canvas.draw()
class Plot_Two():
def __init__(self, master):
self.master = master
self.top_frame = ttk.Frame(self.master, padding = (10, 10))
self.top_frame.pack()
##here are the layout for step 1, load structure files
ttk.Button(self.top_frame, text = "Load Data 2", command = self.load_data_2,
style = "TButton").grid(row = 1, column = 0, columnspan = 2, padx = 5, sticky = "sw")
ttk.Button(self.top_frame, text = "Plot Data 2",command = self.start_plot_two,
style = "TButton").grid(row = 1, column = 2, columnspan = 2, padx = 5)
self.bottom_frame = ttk.Frame(self.master, padding = (10, 10))
self.bottom_frame.pack()
self.fig_2 = plt.figure(figsize=(5, 5), dpi=100) ##create a figure; modify the size here
self.fig_2.add_subplot(111)
self.fig_2.tight_layout()
self.canvas = FigureCanvasTkAgg(self.fig_2, master = self.bottom_frame)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.bottom_frame)
self.toolbar.update()
self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def load_data_2(self):
self.data_2 = tkFileDialog.askopenfilename(defaultextension = ".txt",
filetypes = [("Text Documents", "*.txt")])
def start_plot_two(self):
data = numpy.loadtxt(self.data_2).transpose()
x = data[0]
y = data[1]
self.fig_2.clf()
self.fig_2.add_subplot(111)
plt.plot(x, y, 'b-', lw=2)
self.fig_2.tight_layout()
self.canvas.draw()
def main():
root = Tk()
GUI = Overall_Look(root)
root.mainloop()
if __name__ == "__main__": main()

how can i make my figure made by matplotlib response to a mouse click event?

I read the document of matplotlib and write the following code, it supposed to capture my mouse event and move the grey line position when i clicked. I read this code in jupiter notebook online, it stop to show the coordinate of my cursor as it usually do, What's happend? Can anyone help me?
import pandas as pd
import numpy as np
import matplotlib.colors as mcol
import matplotlib.cm as cm
from scipy import stats
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import scipy.spatial as spatial
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(33500,150000,3650),
np.random.normal(41000,90000,3650),
np.random.normal(41000,120000,3650),
np.random.normal(48000,55000,3650)],
index=[1992,1993,1994,1995])
fig, ax = plt.subplots()
year_avg = df.mean(axis = 1)
year_std = df.std(axis = 1)
yerr = year_std / np.sqrt(df.shape[1]) * stats.t.ppf(1-0.05/2, df.shape[1]-1)
bars=ax.bar(range(df.shape[0]), year_avg, yerr = yerr, color = 'lightslategrey')
threshold=42000
line=plt.axhline(y = threshold, color = 'grey', alpha = 0.5)
cm1 = mcol.LinearSegmentedColormap.from_list("CmapName",["yellow", "orange", "red"])
cpick = cm.ScalarMappable(cmap=cm1)
percentages = []
cpick.set_array([])
def setColor(bars, yerr,threshold):
for bar, yerr_ in zip(bars, yerr):
low = bar.get_height() - yerr_
high = bar.get_height() + yerr_
percentage = (high-threshold)/(high-low)
if percentage>1: percentage = 1
if percentage<0: percentage=0
percentages.append(percentage)
cpick.to_rgba(percentages)
bars = ax.bar(range(df.shape[0]), year_avg, yerr = yerr, color = cpick.to_rgba(percentages))
return bars
line=plt.axhline(threshold, color = 'grey', alpha = 0.5)
setColor(bars, yerr,threshold)
plt.colorbar(cpick, orientation='horizontal')
plt.xticks(range(df.shape[0]), df.index)
fig = plt.figure()
plt.show()
def onclick(event):
print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
('double' if event.dblclick else 'single', event.button,
event.x, event.y, event.xdata, event.ydata))
line.set_ydata(event.ydata)
#plt.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)

How to change the tick labels on a a seaborn joint plot [duplicate]

I am trying to use a log scale as the margin plots for my seaborn jointplot. I am usings set_xticks() and set_yticks(), but my changes do not appear. Here is my code below and the resulting graph:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import seaborn as sns
import pandas as pd
tips = sns.load_dataset('tips')
female_waiters = tips[tips['sex']=='Female']
def graph_joint_histograms(df1):
g=sns.jointplot(x = 'total_bill',y = 'tip', data = tips, space = 0.3,ratio = 3)
g.ax_joint.cla()
g.ax_marg_x.cla()
g.ax_marg_y.cla()
for xlabel_i in g.ax_marg_x.get_xticklabels():
xlabel_i.set_visible(False)
for ylabel_i in g.ax_marg_y.get_yticklabels():
ylabel_i.set_visible(False)
x_labels = g.ax_joint.get_xticklabels()
x_labels[0].set_visible(False)
x_labels[-1].set_visible(False)
y_labels = g.ax_joint.get_yticklabels()
y_labels[0].set_visible(False)
y_labels[-1].set_visible(False)
g.ax_joint.set_xlim(0,200)
g.ax_marg_x.set_xlim(0,200)
g.ax_joint.scatter(x = df1['total_bill'],y = df1['tip'],data = df1,c = 'y',edgecolors= '#080808',zorder = 2)
g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips, c= 'c',edgecolors= '#080808')
ax1 =g.ax_marg_x.get_axes()
ax2 = g.ax_marg_y.get_axes()
ax1.set_yscale('log')
ax2.set_xscale('log')
ax1.set_yscale('log')
ax2.set_xscale('log')
ax2.set_xlim(1e0, 1e4)
ax1.set_ylim(1e0, 1e3)
ax2.xaxis.set_ticks([1e0,1e1,1e2,1e3])
ax2.xaxis.set_ticklabels(("1","10","100","1000"), visible = True)
plt.setp(ax2.get_xticklabels(), visible = True)
colors = ['y','c']
ax1.hist([df1['total_bill'],tips['total_bill']],bins = 10, stacked=True,log = True,color = colors, ec='black')
ax2.hist([df1['tip'],tips['tip']],bins = 10,orientation = 'horizontal', stacked=True,log = True,color = colors, ec='black')
ax2.set_ylabel('')
Any ideas would be much appreciated.
Here is the resulting graph:
You should actually get an error from the line g.ax_marg_y.get_axes() since an axes does not have a get_axes() method.
Correcting for that
ax1 =g.ax_marg_x
ax2 = g.ax_marg_y
should give you the desired plot. The ticklabels for the log axis are unfortunately overwritten by the histogram's log=True argument. So you can either leave that out (since you already set the axes to log scale anyways) or you need to set the labels after calling hist.
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
def graph_joint_histograms(tips):
g=sns.jointplot(x = 'total_bill',y = 'tip', data = tips, space = 0.3,ratio = 3)
g.ax_joint.cla()
g.ax_marg_x.cla()
g.ax_marg_y.cla()
for xlabel_i in g.ax_marg_x.get_xticklabels():
xlabel_i.set_visible(False)
for ylabel_i in g.ax_marg_y.get_yticklabels():
ylabel_i.set_visible(False)
x_labels = g.ax_joint.get_xticklabels()
x_labels[0].set_visible(False)
x_labels[-1].set_visible(False)
y_labels = g.ax_joint.get_yticklabels()
y_labels[0].set_visible(False)
y_labels[-1].set_visible(False)
g.ax_joint.set_xlim(0,200)
g.ax_marg_x.set_xlim(0,200)
g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips,
c = 'y',edgecolors= '#080808',zorder = 2)
g.ax_joint.scatter(x = tips['total_bill'],y = tips['tip'],data = tips,
c= 'c',edgecolors= '#080808')
ax1 =g.ax_marg_x
ax2 = g.ax_marg_y
ax1.set_yscale('log')
ax2.set_xscale('log')
ax2.set_xlim(1e0, 1e4)
ax1.set_ylim(1e0, 1e3)
ax2.xaxis.set_ticks([1e0,1e1,1e2,1e3])
ax2.xaxis.set_ticklabels(("1","10","100","1000"), visible = True)
plt.setp(ax2.get_xticklabels(), visible = True)
colors = ['y','c']
ax1.hist([tips['total_bill'],tips['total_bill']],bins = 10,
stacked=True, color = colors, ec='black')
ax2.hist([tips['tip'],tips['tip']],bins = 10,orientation = 'horizontal',
stacked=True, color = colors, ec='black')
ax2.set_ylabel('')
graph_joint_histograms(tips)
plt.show()

Matplotlib - plot wont show up

I want to plot a function graph (using matplotlib) when a button is pressed, to do so I wrote the following code:
##--IMPORT
#Tkinter
from tkinter import Tk, ttk
from tkinter import Frame, LabelFrame, Button
from tkinter import FALSE
#Numpy
from numpy import linspace
#Sympy
from sympy import symbols,sympify,diff,N,log
#MathPlotLib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
_x = symbols("x")
_sympyFunction = None
_SP_mainSubPlot = None
def pr_draw(plotToDrawTo):
_sympyFunction = sympify("log(x) + x")
valuesRange = linspace(0.01, 3, 100)
x = []
y = []
#Calculate y and x values
for i in range(0, len(valuesRange)):
tempValue = N(_sympyFunction.subs(_x,valuesRange[i]))
x.append(float(valuesRange[i]))
y.append(float(tempValue))
#Draw function graph
plotToDrawTo.plot(x,y)
##--MAIN
if __name__== "__main__":
_root = Tk()
_root.title("Grafico Approsimativo")
_root.resizable(width = FALSE, height = FALSE)
_mainFrame = Frame(_root, bg = "black")
_mainFrame.pack(fill = "both", expand = True)
#Frames
#Main Left
_F_LeftMainFrame = Frame(_mainFrame)
_F_LeftMainFrame.grid(row = 0, column = 0, sticky = "nw")
_F_RightMainFrame = Frame(_mainFrame, bg = "violet")
_F_RightMainFrame.grid(row = 0, column = 2, sticky = "ne")
#Left Content--------------------------
_B_calculate = Button(_F_LeftMainFrame, text = "Draw", command = lambda: pr_draw(_SP_mainSubPlot))
_B_calculate.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = "w")
#Right Content--------------------------
_F_mainPlotWindow = Figure(figsize = None, dpi = 100)
_SP_mainSubPlot = _F_mainPlotWindow.add_subplot(111)
_SP_mainSubPlot.grid(True)
#HERE
#Set master frame for Figure Obj
canvas = FigureCanvasTkAgg(_F_mainPlotWindow, master = _F_RightMainFrame)
canvas.get_tk_widget().pack()
The problem here is that when the button is pressed, nothing shows up in the plot window, the only way I could get this to work is by calling pr_draw(_SP_mainSubPlot) where I inserted the #HERE line: If the function is called there it will work, but not from the button., why?
You would need to redraw the canvas after you have plotted to it.
Adding the line
plotToDrawTo.figure.canvas.draw_idle()
at the end of your pr_draw function should do that.
Note that I also had to add _root.mainloop() at the end of the script to actually show the window.

Matplotlib SOMETIMES updating in tkinter

I have a program with a GUI including multiple sliders and a graph. The sliders set parameters of a function, which is supposed to be plotted on the graph. I began by following the directions in http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html, changed from pack to grid, and have experimented ad nauseum. What I have now updates the plot if I move one slider, but not if I move the other slider. I don't see a difference between the two.
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from Tkinter import *
domain_min = 1
domain_max = 10
order_min = 0
order_max = 3
fig = Figure(figsize=(10,5), dpi=50)
def module(x):
global domain, order, output_message, fig, a, x_vals, y_vals
domain = float(domain_slide.get())
order = int(order_slide.get())
output_message = 'f(1) = %i\nf(2) = %i\nf(3) = %i\nf(4) = %i\nf(5) = %i\nf(6) = %i\n\
f(7) = %i\nf(8) = %i\nf(9) = %i\nf(10)= %i'%(1**order,2**order,3**order,4**order,5**order,\
6**order,7**order,8**order,9**order,10**order)
output_message_text.set(output_message)
x_vals = np.linspace(0,domain,100)
y_vals = x_vals**order
a = fig.add_subplot(111)
a.clear()
a.plot(x_vals,y_vals,color='blue')
#GUI
root = Tk()
domain_slide = DoubleVar()
order_slide = DoubleVar()
output_message_text = StringVar()
ds = Scale(root, variable = domain_slide, from_ = domain_min, to = domain_max, command = module)
ds.grid(column = 0, row = 0)
o_s = Scale(root, variable = order_slide, from_ = order_min, to = order_max, command = module)
o_s.grid(column = 1, row = 0)
out_message = Message(root, textvariable = output_message_text, width = 300, bg = 'white').grid(column = 0, row = 1, rowspan = 3)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().grid(column=3,row=1)
canvas.show()
label = Label(root)
label.grid()
root.mainloop()
What difference does python see in the two sliders? In a more in-depth version of the program, none of the sliders update the plot.
You need to add a
fig.cavas.draw()
to your callback function.
I am not quite sure why it was ever redrawing the canvas with out this.