can embed matplotlib bar graph in wxPython panel but not pie chart - matplotlib

I'm brand new to matplotlib. I have found many examples of embedding bar graphs and plots into a wxPython panel. When I try to replace a graph or plot with a pie chart, the pie chart displays and only when it closes does the panel displays. It doesn't embed. I've been searching for 2 days.
I would like to embed this or this pie chart into this example.
I can't take the code for the pie chart and replace the code for the graph in the example. (Which means I'm missing something pretty big)
This is what I have tried:
import wx
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
class MainFrame(wx.Frame):
"""docstring for MainFrame"""
def __init__(self, parent):
wx.Frame.__init__(self, parent, title=">>>> by www.UmarYusuf.com", size=(800, 580))
# Add SplitterWindow panels
self.split_win = wx.SplitterWindow(self)
self.top_split = MatplotPanel(self.split_win)
self.bottom_split = wx.Panel(self.split_win, style=wx.SUNKEN_BORDER)
self.split_win.SplitHorizontally(self.top_split, self.bottom_split, 480)
# Add some contrls/widgets (StaticText and Buttons)
# Add Text control to the bottom_split window
self.text1 = wx.StaticText(self.bottom_split, -1, u"You can also plot from file", size=(250, 30), pos=(510, 10), style=wx.ALIGN_CENTER)
self.text1.SetBackgroundColour('Gray')
font = wx.Font(15, wx.SWISS, wx.NORMAL, wx.NORMAL)
self.text1.SetFont(font)
# Add Buttons to the bottom_split window and bind them to plot functions
self.Button1 = wx.Button(self.bottom_split, -1, "Plot1", size=(80, 40), pos=(10, 10))
self.Button1.Bind(wx.EVT_BUTTON, self.plot1)
self.Button2 = wx.Button(self.bottom_split, -1, "Plot2", size=(80, 40), pos=(110, 10))
self.Button2.Bind(wx.EVT_BUTTON, self.plot2)
self.Button3 = wx.Button(self.bottom_split, -1, "Plot3", size=(80, 40), pos=(210, 10))
self.Button3.Bind(wx.EVT_BUTTON, self.plot3)
def plot1(self, event):
pass
def plot2(self, event):
pass
def plot3(self, event):
pass
def plot4(self, event):
pass
def plot5(self, event):
pass
class MatplotPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.figure = Figure()
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
self.canvas = FigureCanvas(self, -1, self.figure)
app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()
Thank you.

There is a question about embedding matplotlib into wxpython, where the answer is showing a minimal example. Several examples are also available on the matplotlib examples page.
The main problem you are facing here is that you are creating two different figures. The first one, self.figure = Figure(), is the one you connect to the canvas, but the second one, fig1 is the one you plot your graph to. The second one is the one shown in a new window when plt.show() gets called.
However, calling plt.show() does not make any sense when embedding into GUIs since the GUI should take care of showing the figure.
Therefore the MatplotPanel class from the question should look like this:
class MatplotPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.figure = Figure()
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
ax1 = self.figure.add_subplot(111)
ax1.pie(sizes, labels=labels, autopct='%1.1f%%')
ax1.axis('equal')
self.canvas = FigureCanvas(self, -1, self.figure)

Related

How to create a user input to update the y-axes of the Matplotlib graph on Canvas in PysimpleGUI

I want to create a field in PysimpleGUI where the user can choose the y-axis value of the Matplotlib graph and that the program update the graph. I started learning PysimpleGUI and I am not very experienced with this. I didn't find the answer via google.
I didn't try too much as I am not very experienced and didn't find a solution via google. I expect to get an idea how to create such an user input which updates the y-axis of the graph in PysimpleGUI
Example code to embed matplotlib into PySimpleGUI.
import math, random
from pathlib import Path
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import PySimpleGUI as sg
# 1. Define the class as the interface between matplotlib and PySimpleGUI
class Canvas(FigureCanvasTkAgg):
"""
Create a canvas for matplotlib pyplot under tkinter/PySimpleGUI canvas
"""
def __init__(self, figure=None, master=None):
super().__init__(figure=figure, master=master)
self.canvas = self.get_tk_widget()
self.canvas.pack(side='top', fill='both', expand=1)
# 2. create PySimpleGUI window, a fixed-size Frame with Canvas which expand in both x and y.
font = ("Courier New", 11)
sg.theme("DarkBlue3")
sg.set_options(font=font)
layout = [
[sg.Input(expand_x=True, key='Path'),
sg.FileBrowse(file_types=(("ALL CSV Files", "*.csv"), ("ALL Files", "*.*"))),
sg.Button('Plot')],
[sg.Frame("", [[sg.Canvas(background_color='green', expand_x=True, expand_y=True, key='Canvas')]], size=(640, 480))],
[sg.Push(), sg.Button('Exit')]
]
window = sg.Window('Matplotlib', layout, finalize=True)
# 3. Create a matplotlib canvas under sg.Canvas or sg.Graph
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot()
canvas = Canvas(fig, window['Canvas'].Widget)
# 4. initial for figure
ax.set_title(f"Sensor Data")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_xlim(0, 1079)
ax.set_ylim(-1.1, 1.1)
ax.grid()
canvas.draw() # do Update to GUI canvas
# 5. PySimpleGUI event loop
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'Plot':
"""
path = values['Path']
if not Path(path).is_file():
continue
"""
# 6. Get data from path and plot from here
ax.cla() # Clear axes first if required
ax.set_title(f"Sensor Data")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.grid()
theta = random.randint(0, 359)
x = [degree for degree in range(1080)]
y = [math.sin((degree+theta)/180*math.pi) for degree in range(1080)]
ax.plot(x, y)
canvas.draw() # do Update to GUI canvas
# 7. Close window to exit
window.close()
You can add Input elements with different keys, get the value of element by values[key] when plot to update your y-axis.
Update - Example Code
Add Input elements for y-axis data
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import PySimpleGUI as sg
class Canvas(FigureCanvasTkAgg):
"""
Create a canvas for matplotlib pyplot under tkinter/PySimpleGUI canvas
"""
def __init__(self, figure=None, master=None):
super().__init__(figure=figure, master=master)
self.canvas = self.get_tk_widget()
self.canvas.pack(side='top', fill='both', expand=1)
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
sg.set_options(font=("Courier New", 16))
layout = [
[sg.Text("Income last week")],
[sg.Canvas(size=(640, 480), background_color='green', expand_x=True, expand_y=True, key='Canvas')],
[sg.Text(day, size=10, justification='center') for day in days],
[sg.Input(size=10, justification='right', key=('Day', i)) for i in range(len(days))],
[sg.Push(), sg.Button('Plot')]
]
window = sg.Window('Title', layout, finalize=True)
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot()
canvas = Canvas(fig, window['Canvas'].Widget)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Plot':
# x = [i for i in range(len(days))]
y = []
for i in range(len(days)):
try:
value = float(values[('Day', i)])
except:
value = 0
y.append(value)
ax.cla()
ax.grid()
ax.plot(days, y)
canvas.draw()
window.close()

How do I stop closed Matplotlib plots coming back to haunt me?

I have a written a large data processing and plotting application using PyQt5 with Matplotlib and I am plagued by old plots reappearing when creating new ones. I have seen many questions answered regarding this but none of the suggested methods work for me.
I have reduced the problem down to the following simplified code:
from PyQt5.QtWidgets import (QApplication, QMainWindow, QDialog, QPushButton,
QVBoxLayout, QGroupBox)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import sys
class PlotCanvas(FigureCanvas):
""" Subclass of the MPL FigureCanvas class. """
def __init__(self, parent=None):
self.parent = parent
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.fig.set_tight_layout(True)
FigureCanvas.__init__(self, self.fig)
class PlotWindow(QDialog):
""" Create plot in window and plot supplied data. """
def __init__(self, parent, x, y):
super().__init__()
self.parent = parent
self.x = x
self.y = y
self.set_up_gui()
self.l2d = self.canvas.ax.plot(self.x, self.y)[0]
def set_up_gui(self):
""" Create GUI. """
vbl = QVBoxLayout()
self.canvas = PlotCanvas(self)
vbl.addWidget(self.canvas)
self.setLayout(vbl)
def closeEvent(self, event):
""" Delete figure on closing window. """
plt.close(self.canvas.fig)
super(PlotWindow, self).closeEvent(event)
class MainApp(QMainWindow):
""" My main application class """
def __init__(self, parent=None):
super(QMainWindow, self).__init__()
# Create some arbitrary data
self.x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.y = [2, 4, 6, 4, 2, 4, 6, 8, 6]
self.set_up_gui()
def set_up_gui(self):
""" Create GUI with 2 buttons for creating 2 plots. """
btn1 = QPushButton()
btn1.setText('Button 1')
btn1.clicked.connect(self.plot1)
btn2 = QPushButton()
btn2.setText('Button 2')
btn2.clicked.connect(self.plot2)
vbl = QVBoxLayout()
vbl.addWidget(btn1)
vbl.addWidget(btn2)
gb = QGroupBox()
gb.setLayout(vbl)
self.setCentralWidget(gb)
def plot1(self):
""" Plot using my PlotWindow class. """
p = PlotWindow(self, self.x, self.y)
p.show()
def plot2(self):
""" Plot using regular MPL plot call. """
fig = plt.figure()
plt.plot([10, 20, 30], [4, 8, 2])
plt.show()
plt.close(fig)
if __name__=='__main__':
app = QApplication(sys.argv)
main = MainApp()
main.show()
sys.exit(app.exec_())
The odd behavior is this:
Press button 1, plot 1 appears, press red X to close window.
Press button 2, plot 2 appears, press red X to close window.
Press button 1, plot 1 appears, press red X to close window.
Press button 2, plot 2 appears but so does plot 1, press red X to close plot 2, plot 1 also closes.
From here on, button 1 brings up plot 1, and button 2 brings up both plots.
Based on answers to similar questions (such as matplotlib.pyplot will not forget previous plots - how can I flush/refresh?), I have tried various things, such as following plt.show() with plt.cla() plt.clf() and plt.close() but none of them fix the problem. As you can see in my code, I have added the the plt.close() to the PyQt5 window close event and also the regular MPL plot but this seems to do nothing.
EDIT: Now that the question is answered, I have cleaned up some of the musings that followed and reduced it to the pertinent information.
I tried this code out on my work Linux machine (originally developed on Mac) and don't get the same double plot behavior. Which shows it's platform-dependent.
On Linux I can actually remove the plt.close() from the plot2 method and I can remove the closeEvent method from the PlotWindow class and it works fine without any double plots showing.
Ok, through trial end error, I have fixed this problem. I don't claim to know exactly the mechanics of how it is fixed. Perhaps seeing how I fixed it, some clever person can explain why it fixes it.
If I change the closeEvent method in PlotCanvas to the following (where I have marked the added lines with asterisks):
def closeEvent(self, event):
""" Delete figure on closing window. """
self.canvas.ax.cla() # ****
self.canvas.fig.clf() # ****
plt.close(self.canvas.fig)
super(PlotWindow, self).closeEvent(event)
and change the plot2 method to the following:
def plot2(self):
""" Plot using regular MPL call. """
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([10, 20, 30], [4, 8, 2])
plt.show()
ax.cla() # ****
fig.clf() # ****
plt.close(fig)
I now get no multiple plots appearing. I would have thought that axes would be closed along with the figure container when it is closed, but apparently not. I would also hope that I would get the same behavior independent of platform but it seems the MacOSX backend acts differently to others.

Colorbar disappear during animation of image when changing colormap

I created a subplot in a figure for displaying an image with imshow, and I add a colorbar. With no animation, I can change the colormap changing the value of the ComboBox and the colorbar is updated correctly.
But if I add an animation, the colorbar disappear each time I change the colormap. I have to click on another window (other software, etc) or resize the GUI to see the colorbar again.
Here is an example to understand the problem :
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib import animation
class FenetrePrincipale(QWidget):
def __init__(self, parent=None):
super(FenetrePrincipale, self).__init__(parent)
self.setupUi(self)
# Fonction de configuration de la classe
def setupUi(self, Form):
self.Form = Form
Form.setMinimumSize(1220, 850)
self.creation_GUI()
self.creation_figure()
self.creation_layout()
self.tabWidget.setCurrentIndex(0)
self.Bouton_quitter.clicked.connect(self.close)
self.anim = animation.FuncAnimation(self.figure, self.animate, interval=10, blit=True)
self.Widget_choixPalette_ComboBox.currentIndexChanged.connect(self.changementPalette)
def changementPalette(self, onglet):
self.image.set_cmap('binary')
self.canvas.draw()
def animate(self, i):
# a = self.thread_1.img
self.image.set_array(self.imageInit)
return [self.image]
def resizeEvent(self, QResizeEvent):
self.tabWidget.setMinimumSize(QSize(self.width() - 20, self.height() - 60))
def creation_GUI(self):
self.tabWidget = QTabWidget()
self.tab1 = QWidget()
self.tabWidget.addTab(self.tab1, " Tab1 ")
self.Widget_choixPalette_Label = QLabel(self.tab1)
self.Widget_choixPalette_Label.setText("Text1")
self.Widget_choixPalette_ComboBox = QComboBox(self.tab1)
self.Widget_choixPalette_ComboBox.addItem("Try1")
self.Widget_choixPalette_ComboBox.addItem("Try2")
self.Bouton_quitter = QPushButton(self.tab1)
self.Bouton_quitter.setText("Quit")
def creation_layout(self):
LayoutForm = QGridLayout(self)
LayoutForm.addWidget(self.tabWidget, 0, 0, 1, 1)
LayoutTab1 = QGridLayout(self.tab1)
LayoutTab1.addWidget(self.Widget_choixPalette_Label, 0, 1, 1, 1)
LayoutTab1.addWidget(self.Widget_choixPalette_ComboBox, 1, 1, 1, 1)
self.Widget_choixPalette_ComboBox.setMinimumWidth(200)
LayoutTab1.addWidget(self.canvas, 2, 0, 1, 3)
LayoutTab1.addWidget(self.Bouton_quitter, 2, 3, 1, 1, Qt.AlignRight | Qt.AlignBottom)
LayoutTab1.setRowStretch(2, 1)
LayoutTab1.setColumnStretch(0, 1)
LayoutTab1.setColumnStretch(2, 1)
def creation_figure(self):
# Create figure (transparent background)
self.figure = plt.figure()
# self.figure.patch.set_facecolor('None')
self.canvas = FigureCanvas(self.figure)
self.canvas.setStyleSheet("background-color:transparent;")
# Adding one subplot for image
self.axe0 = self.figure.add_subplot(111)
self.axe0.get_xaxis().set_visible(False)
self.axe0.get_yaxis().set_visible(False)
# Data for init image
self.imageInit = [[255] * 320 for i in range(240)]
self.imageInit[0][0] = 0
# Init image and add colorbar
self.image = self.axe0.imshow(self.imageInit, interpolation='none')
divider = make_axes_locatable(self.axe0)
cax = divider.new_vertical(size="5%", pad=0.05, pack_start=True)
self.colorbar = self.figure.add_axes(cax)
self.figure.colorbar(self.image, cax=cax, orientation='horizontal')
plt.subplots_adjust(left=0, bottom=0.05, right=1, top=1, wspace=0, hspace=0)
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
# QApplication.setStyle(QStyleFactory.create("plastique"))
form = FenetrePrincipale()
form.show()
sys.exit(app.exec_())
When I change of colormap selecting any choice in combobox :
What I am waiting to see :
Operating system: Windows 7 Pro
Matplotlib version: 2.1.0
Matplotlib backend: Qt5Agg
Python version: 3.6
a] blit=False
Use blit=False in the animation. Otherwise only the image itself will be updated and the redrawing of the complete canvas is posponed to some event that makes this redrawing necessary, e.g. a resize event.
b] pause animation
In case you cannot affort using blit=False, you can pause the animation, change the colormap, draw the canvas, then continue the animation.
def changementPalette(self, onglet):
self.anim.event_source.stop()
if onglet==0:
self.image.set_cmap('viridis')
else:
self.image.set_cmap('binary')
self.canvas.draw_idle()
self.anim.event_source.start()

Dynamic matplotlib pyside widget not displaying

I'm writing a python application using pyside and matplotlib. Following a combination of this tutorial and this SO post, I have created a matplotlib widget that I can successfully add to a parent. However when I go to actually add data to it, nothing seems to get displayed.
If I add static data like the SO post had, it shows up, but when I change it to update on the fly (currently every second on a timer, but it will eventually be using a signal from another class), I never get anything but the empty axes to appear. I suspect that I'm missing a call to force a draw or invalidate or that there is something wrong with the way I'm calling update_datalim (though the values that get passed to it seem correct).
from PySide import QtCore, QtGui
import matplotlib
import random
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.patches import Rectangle
from collections import namedtuple
DataModel = namedtuple('DataModel', ['start_x', 'start_y', 'width', 'height'])
class BaseWidget(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# We want the axes cleared every time plot() is called
self.axes.hold(False)
self.axes.set_xlabel('X Label')
self.axes.set_ylabel('Y Label')
self.axes.set_title('My Data')
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class DynamicWidget(BaseWidget):
def set_data(self, the_data):
self.axes.clear()
xys = list()
cmap = plt.cm.hot
for datum in the_data:
bottom_left = (datum.start_x, datum.start_y)
top_right = (bottom_left[0] + datum.width, bottom_left[1] + datum.height)
rect = Rectangle(
xy=bottom_left,
width=datum.width, height=datum.height, color=cmap(100)
)
xys.append(bottom_left)
xys.append(top_right)
self.axes.add_artist(rect)
self.axes.update_datalim(xys)
self.axes.figure.canvas.draw_idle()
class RandomDataWidget(DynamicWidget):
def __init__(self, *args, **kwargs):
DynamicWidget.__init__(self, *args, **kwargs)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.generate_and_set_data)
timer.start(1000)
def generate_and_set_data(self):
fake_data = [DataModel(
start_x=random.randint(1, 100),
width=random.randint(20, 40),
start_y=random.randint(80, 160),
height=random.randint(20, 90)
) for i in range(100)]
self.set_data(fake_data)
Edit: I'm suspecting that there's an issue with updating the limits of the plot. When running the above code, the plot opens with limits of 0 and 1 on both the x and y axis. Since none of my generated data falls into that range, I created another subclass of DynamicWidget that plots only data between 0 and 1 (the same data from the linked SO post). When instantiating the class below, the data shows up successfully. Do I need to do something more than calling update_datalim to get the graph to re-bound itself?
class StaticWidget(DynamicWidget):
def __init__(self):
DynamicWidget.__init__(self)
static_data = [
DataModel(0.5, 0.05, 0.2, 0.05),
DataModel(0.1, 0.2, 0.7, 0.2),
DataModel(0.3, 0.1, 0.8, 0.1)
]
self.set_data(static_data)
Yes, update_datalim only updates the bounding box that is kept internally by the axes. You also need to enable auto scaling for it to be used. Add self.axes.autoscale(enable=True) after the self.axes.clear() statement and it will work. Or you can set the axes' range to a fixed value by using self.axes.set_xlim and self.axes.set_ylim.
edit: here is my code, which works for me
from PySide import QtCore, QtGui
import matplotlib
import random, sys
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib import pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.patches import Rectangle
from collections import namedtuple
DataModel = namedtuple('DataModel', ['start_x', 'start_y', 'width', 'height'])
class BaseWidget(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# We want the axes cleared every time plot() is called
self.axes.hold(False)
#self.axes.autoscale(enable=True)
self.axes.set_xlabel('X Label')
self.axes.set_ylabel('Y Label')
self.axes.set_title('My Data')
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class DynamicWidget(BaseWidget):
def set_data(self, the_data):
self.axes.clear()
self.axes.autoscale(enable=True)
#self.axes.set_xlim(0, 300)
#self.axes.set_ylim(0, 300)
xys = list()
cmap = plt.cm.hot
for datum in the_data:
print datum
bottom_left = (datum.start_x, datum.start_y)
top_right = (bottom_left[0] + datum.width, bottom_left[1] + datum.height)
rect = Rectangle(
xy=bottom_left,
width=datum.width, height=datum.height, color=cmap(100)
)
xys.append(bottom_left)
xys.append(top_right)
self.axes.add_artist(rect)
self.axes.update_datalim(xys)
self.axes.figure.canvas.draw_idle()
class RandomDataWidget(DynamicWidget):
def __init__(self, *args, **kwargs):
DynamicWidget.__init__(self, *args, **kwargs)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.generate_and_set_data)
timer.start(1000)
def generate_and_set_data(self):
fake_data = [DataModel(
start_x=random.randint(1, 100),
width=random.randint(20, 40),
start_y=random.randint(80, 160),
height=random.randint(20, 90)) for i in range(100)]
self.set_data(fake_data)
print "done:...\n\n"
def main():
qApp = QtGui.QApplication(sys.argv)
aw = RandomDataWidget()
aw.show()
aw.raise_()
sys.exit(qApp.exec_())
if __name__ == "__main__":
main()

PyQt4 and matplotlib: Legend outside of the plot is cut off

I've been googling a lot, but I haven't found a solution.
The problem is the same as here: Moving matplotlib legend outside of the axis makes it cutoff by the figure box
But I don't want to save the figure, I just want to have the legend inside the whole figure. I've also tried tight_layout but that didn't work. I'm totally new to matplotlib and can't figure it out. Here is my source code (I'm embedding a matplot into PyQt4):
class GraphACanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=5, dpi=100):
self.fig = Figure(figsize=(width, height), facecolor=backColorHexName, dpi=dpi)
self.axes = self.fig.add_subplot(1, 1, 1, autoscale_on=False)
self.axes.set_xticks(arange(-0.1, 1.4, 0.1))
self.axes.set_yticks(arange(-0.1, 1.4, 0.1))
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, Gui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def computeFigure(self):
col = []
for i in xrange(0, 100):
x = i/100
y = y/100
line, = self.axes.plot(x, y, 'o', color=blue, label='label')
col.append(line.get_c())
self.axes.set_xlabel('x', labelpad=10, rotation=0)
self.axes.set_ylabel('y', labelpad=10, rotation=0)
leg = self.axes.legend(loc=2, bbox_to_anchor=(1.0, 0.5), borderaxespad=0., fontsize='x-small', framealpha=0.25, markerscale=0.5, ncol=1, numpoints=1)
for color,text in zip(col,leg.get_texts()):
text.set_color(color)
test = GraphACanvas()
test.computeFigure()
I've put in here just dummy values. But in the application, a user can select nodes, and depending on how many nodes he select, the bigger/smaller is the legend. I would like to shrink the x-axis side to have more place for the legend.
--> The subplot should not fill out 100% (width/height) of the figure, rather 100% height and 80% width, so that my legend has enough space.
This line:
self.axes = self.fig.add_subplot(1, 1, 1, autoscale_on=False)
Will create an axes that fills out the whole figure (more or less) as you describe.
Try:
self.axes = self.fig.add_axes([0.1, 0.1, 0.7, 0.8])
The elements for that list are:
[left_edge, bottom_edge, width, height] where all values are fractions of the total figure dimensions.