Files
scikit-image/skimage/viewer/plugins/plotplugin.py
T
2012-11-15 23:53:47 -05:00

59 lines
1.8 KiB
Python

import numpy as np
try:
from PyQt4 import QtGui
except ImportError:
print("Could not import PyQt4 -- skimage.viewer not available.")
try:
import matplotlib.pyplot as plt
except ImportError:
print("Could not import matplotlib -- skimage.viewer not available.")
from ..utils import MatplotlibCanvas
from .base import Plugin
class PlotCanvas(MatplotlibCanvas):
"""Canvas for displaying images.
This canvas derives from Matplotlib, and has attributes `fig` and `ax`,
which point to Matplotlib figure and axes.
"""
def __init__(self, parent, height, width, **kwargs):
self.fig, self.ax = plt.subplots(figsize=(height, width), **kwargs)
super(PlotCanvas, self).__init__(parent, self.fig, **kwargs)
self.setMinimumHeight(150)
class PlotPlugin(Plugin):
"""Plugin for ImageViewer that contains a plot canvas.
Base class for plugins that contain a Matplotlib plot canvas, which can,
for example, display an image histogram.
See base Plugin class for additional details.
"""
def attach(self, image_viewer):
super(PlotPlugin, self).attach(image_viewer)
# Add plot for displaying intensity profile.
self.add_plot()
def redraw(self):
"""Redraw plot."""
self.canvas.draw_idle()
def add_plot(self, height=4, width=4):
self.canvas = PlotCanvas(self, height, width)
self.fig = self.canvas.fig
#TODO: Converted color is slightly different than Qt background.
qpalette = QtGui.QPalette()
qcolor = qpalette.color(QtGui.QPalette.Window)
bgcolor = qcolor.toRgb().value()
if np.isscalar(bgcolor):
bgcolor = str(bgcolor / 255.)
self.fig.patch.set_facecolor(bgcolor)
self.ax = self.canvas.ax
self.layout.addWidget(self.canvas, self.row, 0)