Hello everyone,
I have a problem with a colorbar:
I have a stackedbar and a colorbar that I want to create or delete by clicking
on a button.. but when the colorbar is deleted I want to change the dimension
of the axes, resizing it even in the place occupied by the colorbar..
I need to create the colorbar by using the function "make_axes_locatable" and
not others function..
Someone can help me?
this is the code:
from matplotlib.figure import Figure
from mpl_toolkits.axes_grid.axes_divider import make_axes_locatable
from matplotlib import mpl, colorbar
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as
FigureCanvas
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class Test(QWidget):
def __init__(self):
QWidget.__init__(self)
self.resize(1000, 700)
self.setGeometry(20,30,700,500)
self.fig = Figure(figsize=(100,100), dpi=75)
self.canvas = FigureCanvas(self.fig)
self.axes = self.fig.add_subplot(111)
b = self.axes.bar([0,1,2,3],[5,4,9,6])
self.colorbar = True
self.Colorbar()
self.old_size = self.axes.get_position()
self.btn = QPushButton()
self.btn.resize(20,20)
self.btn.clicked.connect(self.OnBtnClicked)
self.Layout = QHBoxLayout()
spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding,
QSizePolicy.Minimum)
self.Layout.addItem(spacerItem)
self.Layout.addWidget(self.canvas)
self.Layout.addWidget(self.btn)
self.setLayout(self.Layout)
def OnBtnClicked(self):
self.colorbar = not self.colorbar
self.Colorbar()
def Colorbar(self):
if self.colorbar :
self.divider = make_axes_locatable(self.axes)
self.cax = self.divider.append_axes("right", size="3%", pad=0.1)
self.cmap = mpl.colors.ListedColormap(['b'])
self.cb = mpl.colorbar.ColorbarBase(self.cax,
cmap=self.cmap,
orientation='vertical')
else:
if hasattr(self, "cb") :
fig = self.axes.get_figure()
fig.delaxes(self.cb.ax)
del self.cb
self.axes.set_position(self.old_size)
self.fig.canvas.draw()
app = QApplication(sys.argv)
win = Test()
win.show()
sys.exit(app.exec_())
thanks
Erika