Commit 946c3c02 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

Merge branch 'BUG-aliases-now-defined-before-addAxes' of...

Merge branch 'BUG-aliases-now-defined-before-addAxes' of gitlab.aip.de:yfournier/myplotlib into BUG-aliases-now-defined-before-addAxes
parents ab0f7538 cdaaaa2d
......@@ -28,7 +28,7 @@ D_OFORMAT = 'png' # default format for ouput
D_DEBUG = True # default debug value
# PLOTTING CONFIGURATION -----------------------------------------------
# BACKEND: u'TKAgg', u'GTKAgg', u'WXAgg'
# BACKEND: u'TKAgg', u'GTKAgg', u'WXAgg', u'Qt4Agg', u'MacOSX'
rcParams['backend'] = u'TkAgg'
# FONT
......
......@@ -28,7 +28,7 @@ D_OFORMAT = 'png' # default format for ouput
D_DEBUG = True # default debug value
# PLOTTING CONFIGURATION -----------------------------------------------
# BACKEND: u'TKAgg', u'GTKAgg', u'WXAgg'
# BACKEND: u'TKAgg', u'GTKAgg', u'WXAgg', u'Qt4Agg', u'MacOSX'
rcParams['backend'] = u'GTKAgg'
# FONT
......
......@@ -33,9 +33,17 @@
#
#
# IMPORT ---------------------------------------------------------------
from .config import INFO, WARN, SEVR, DBUG, SPCE
import os as os
import numpy as np
# Test mpl version:
from matplotlib import __version__ as mplvers
if int(mplvers.replace('.','')[:3]) < 143 :
print('\n\n' + WARN + 72*'=' + '\n' + SPCE + 'The matplotlib version you are using is not supported.\n' + SPCE + 'Most of myplotlib should work, but some stuff may not.\n' + SPCE + 'ex: expect an error with test203\n' + SPCE + 72*'=' + '\n\n')
# matplotlib
from matplotlib.pyplot import figure
from matplotlib.pyplot import rc
......@@ -56,7 +64,6 @@ G_RAWDATAS = {'current': MyData()} # raw data Object
_G_WINDOWS = []
# CONFIGURATION --------------------------------------------------------
from .config import INFO, WARN, SEVR, DBUG, SPCE
from .config import D_FIGNUM, D_FIGSIZE, D_REFORMAT, D_FORMATTED
from .config import D_RAWDATA, D_INPUTARG
from .config import D_IPATH
......@@ -78,6 +85,8 @@ elif rcParams['backend'] == u'GTKAgg':
from .myWin_GTKAgg import MyWin_GTKAgg as MyWin
elif rcParams['backend'] == u'WXAgg':
from .myWin_WXAgg import MyWin_WXAgg as MyWin
elif rcParams['backend'] == u'Qt4Agg':
from .myWin_Qt4Agg import MyWin_Qt4Agg as MyWin
elif rcParams['backend'] == u'MacOSX':
from .myWin_MacOSx import MyWin_MacOSx as MyWin
else:
......
......@@ -33,7 +33,7 @@
# IMPORT ---------------------------------------------------------------
from . import INFO, SPCE, DBUG
from . import myTest, testList
from . import __dict__
from .test import __dict__ as availTests
import sys
import getopt
......@@ -76,8 +76,8 @@ for opt, arg in opts:
tests = arg.split(', ')
testList = []
for test in tests:
if test in __dict__.keys():
testList.append(__dict__[test])
if test in availTests.keys():
testList.append(availTests[test])
if len(testList) == 0:
print(" > sevr-: you need to give valid tests to be tested.")
......
......@@ -28,7 +28,7 @@ D_OFORMAT = 'png' # default format for ouput
D_DEBUG = True # default debug value
# PLOTTING CONFIGURATION -----------------------------------------------
# BACKEND: u'TKAgg', u'GTKAgg', u'WXAgg', u'MacOSX'
# BACKEND: u'TKAgg', u'GTKAgg', u'WXAgg', u'Qt4Agg', u'MacOSX'
rcParams['backend'] = u'WXAgg'
# FONT
......
......@@ -114,8 +114,19 @@ class MyAxes(Axes):
# Because matplotlib.axes.update expect kwargs and not **kwargs ... (stupid!!)
if args: # catch matplotlib kwargs
kwargs = args[0]
kw_for_axes = {key: value for (key, value) in args[0].items() if key not in self.keywords}
# kw_for_axes = {key: value for (key, value) in args[0].items() if key not in self.keywords} # Not compatible with python2.6
kw_for_axes = {}
for (key, value) in args[0].items():
if key not in self.keywords:
kw_for_axes.update({key: value})
Axes.update(self, kw_for_axes) # update matplotlib.Axes
# myplotlib update
self.keywords.update({key: value for (key, value) in kwargs.items() if key in self.keywords})
# self.keywords.update({key: value for (key, value) in kwargs.items() if key in self.keywords}) # Not compatible with python2.6
for (key, value) in kwargs.items():
if key in self.keywords:
self.keywords.update({key: value})
......@@ -97,6 +97,7 @@ class MyFig(Figure):
self.debug = kwargs.pop('debug', D_DEBUG)
self.formatted = kwargs.pop('formatted', D_FORMATTED)
self.aliases = {}
self.FIGSIZE = kwargs.pop('figsize', self.FIGSIZE)
# parent constructor
Figure.__init__(self, figsize=self.FIGSIZE)
......@@ -179,11 +180,11 @@ class MyFig(Figure):
# This overwrites MPL add_axes. It gives a name to the axis added so that it is easier to refer to
def add_axes(self, ax, name) :
if isinstance(name, str) and isinstance(ax, MyAxes):
ax.name = name
else:
print(SEVR + " there is an error with the input type of add_axes()")
return False
# if isinstance(name, str) and issubclass(ax.__class__, MyAxes): # This lead to an error is ax is not in the same namespace
ax.name = name
# else:
# print(SEVR + " there is an error with the input type of add_axes()")
# return False
# test if an axes with that name is already present
for pax in self.get_axes() :
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
# myplotlib v3.0.1,
#
# @file myWin_Qt4Agg.py
# @author Yori 'AGy' Fournier
# @licence CC-BY-SA
#
# MyWin_Qt4Agg class: Overlay of matplotlib FigureManager
# class. It allows a cleaner and more confortable
# usage of windows.
#
# @Class MyWin
#
# @Constructor
#
# fig: the MyFig object that need to be drawn
# show: a flag to show the window just after
# creation. (default: True)
# fignum: number of both the Window and the Figure.
# debug: flag for debugging, more verbatile.
# (kw)args: user defined arguments and keywords.
#
# @section Functions
#
# - _boundFigure_: bound a figure to the window (priv.)
#
# - _unboundFigure_: unbound a figure to the window (priv.)
#
# - set_figure: a clean way of setting a new figure
# to the window.
#
# - refresh: redraw the window
# - clean the figure
# - plot the figure
# - draw the canvas
#
# - drawFigure: draw a given figure on the window
# - set the figure of needed
# - refresh
#
# - close: close the window
#
# ======================================================================
#
# IMPORT ---------------------------------------------------------------
from . import INFO, SEVR, WARN, DBUG, SPCE
from . import rcParams
from . import _G_WINDOWS
from . import np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg, FigureManagerQT
# Class MyWin Overwriting the disgusting Matplotlib.FigureManager class
class MyWin_Qt4Agg( FigureManagerQT):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, fig, show=True, *args, **kwargs):
# set the dpi to 75 (correct value for screen)
if(fig.dpi != 75.):
fig.dpi = 75.
# Get the debug value
debug = kwargs.get('debug', False)
# Get the user specified ID of the window or set it to 0
if 'fignum' in kwargs.keys():
num = kwargs['fignum']
fignumSpecifiedByUser = True
else:
num = 0
fignumSpecifiedByUser = False
# if ID already used
if(num in [(window.num) for window in _G_WINDOWS]):
# if user specified fignum then close former window and create new one
if(fignumSpecifiedByUser):
if(debug):
print(DBUG + "The fignum you specified already exists, I'll close the former window")
# Close the window with the specified number
_G_WINDOWS[np.where([(window.num == num) for window in _G_WINDOWS])[0][0]].close()
else:
# If user didn't specified a fignum
# increase ID until found a free one
while(num in [(window.num) for window in _G_WINDOWS]):
num = num + 1
# If debug print the ID of the window
if(debug):
print(DBUG + "You choosed num = " + str(num))
# before creating anything verify that the fig is not already
# bounded to another window
if fig.boundedToWin:
# if it is the case close the bounded window.
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# Call FigureManagerGTK class
# (already wondering if this is not a mistake...)
if(debug):
print(INFO + "Used GTKAgg backend.")
# create the canvas
canvas = FigureCanvasQTAgg(fig)
FigureManagerQT.__init__(self, canvas, num)
# bound the figure to the window
self.canvas.figure.boundedToWin = True
self.canvas.figure.fignum = num
# add the window in the global variable _G_WINDOWS
# this guarenty that you never loose a window
# (see myTool.py getWindow(num))
_G_WINDOWS.append(self)
# if show is on, then show the window
if show:
# For GTKAgg and TKAgg ---------------------------------
self.refresh()
self.show()
# BOUND ------------------------------------------------------------
def _boundFigure_(self, fig):
fig.canvas = self.canvas
self.canvas.figure = fig
self.canvas.figure.boundedToWin = True
self.canvas.figure.fignum = self.num
# UNBOUND ----------------------------------------------------------
def _unboundFigure_(self):
self.canvas.figure.boundedToWin = False
self.canvas.figure.fignum = -1
# SETTERS ----------------------------------------------------------
# SET FIGURE -------------------------------------------------------
def set_figure(self, fig):
# first unbound the former figure
self._unboundFigure_()
# if the new figure is already bounded close
# its former container.
if fig.boundedToWin:
win = _G_WINDOWS[np.where([(window.num == fig.fignum) for window in _G_WINDOWS])[0][0]]
win.close()
# bound the new figure
self._boundFigure_(fig)
# REFRESH ----------------------------------------------------------
def refresh(self):
# in the case the figure got another canvas
# rebound the figure. This situation should not happend!!
if self.canvas != self.canvas.figure.canvas:
print(WARN + "Something weird happend, the canvas of the figure have been changed")
self.set_figure(self.canvas.figure)
# refresh
self.canvas.figure.plot()
self.canvas.draw()
return(True)
# DRAW -------------------------------------------------------------
def drawFigure(self, fig):
# if the figure is not the current one
if(fig.fignum != self.num):
# cleanly set the new figure
self.set_figure(fig)
# and refresh
self.refresh()
# CLOSE ------------------------------------------------------------
def close(self):
self._unboundFigure_()
self.destroy()
del _G_WINDOWS[np.where([(window == self) for window in _G_WINDOWS])[0][0]]
# remark if you create the window like:
# win = MyWin(fig)
# then win is still refering the self
# and therefore not yet freed
# del win would free it entirely.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment