def testErrors(self): """A test of MplStyle error conditions.""" # Setup the style style = S.MplStyle("Test Errors Style") self.assertRaises(Exception, style.apply, "bogusObject", msg="Failed to throw on invalid apply object.") try: style.bogus except BaseException: pass else: fail("Failed to throw on accessing an invalid property.") style._subStyle = None try: style.bogus = "invalid" except BaseException: pass else: fail("Failed to throw on setting an invalid property.") class MplDerivedStyle(S.MplStyle): def __init__(self, *args, **kwargs): S.MplStyle.__init__(self, *args, **kwargs) self.bgColor = 'white' self.fgColor = 'black' self.newProperty = None # Make sure that construction doesn't throw tmpStyle = MplDerivedStyle("tmpStyle")
def testDefaults(self): """Test that MplStyle falls through to defaults properly.""" fig, ax = matplotlib.pyplot.subplots() ax.set_title("This is the Title") line = ax.plot([1, 2, 3, 4], [2, 4, 6, 8]) style = S.MplStyle("Big Title") #style.axes.title.font.family = "sans-serif" fname = "MplStyle_testDefaults_001.png" fig.savefig(self.outputFile(fname)) self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg="Original plot is not correct") style.apply(fig) fname = "MplStyle_testDefaults_002.png" fig.savefig(self.outputFile(fname)) self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg="Styled plot is not correct")
def testBasic(self): """A basic test of MplStyleManager.""" # Setup the plot fig, ax = matplotlib.pyplot.subplots() patch = mpl.patches.Patch() ax.minorticks_on() ax.set_title('Axes Title') ax.set_xlabel("X-Axis Label") ax.set_ylabel("Y-Axis Label") xdata = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 4.75, 5] ydata = [1, 1.75, 2, 2.75, 3, 2.75, 2, 2.25, 2.75, 3] line = ax.plot(xdata, ydata, color='blue') axText = ax.text(4.2, 1.1, "Axes Text") # Get the manager mgr = S.mgr style1 = mgr.create('Style #1') style1.axes.labels.font.size = 8 style2 = mgr.create('Style #2', { 'figure.bgColor': 'grey', 'axes.bgColor': 'white' }) style3 = mgr.create('Style #3', parent=style1) style3.axes.labels.font.size = 24 # Resolved 3 with 2 style4 = S.MplStyle('Style #4') style4.axes.labels.font.size = 24 style4.figure.bgColor = 'grey' style4.axes.bgColor = 'yellow' # Resolved 3 with 2 and updated 3 style5 = mgr.create('Style #5') mgr['Style #5'].axes.labels.font.size = 16 mgr['Style #5'].figure.bgColor = 'grey' mgr['Style #5'].axes.bgColor = 'yellow' # Copy test newStyle = mgr.copy(style3, 'NewStyle') self.checkStyleEq('Copy - style3', style3, newStyle) self.assertRaises( Exception, mgr.copy, 'badName', 'blah', msg="Failed to raise when copying a non-existant style.") self.assertEqual([], mgr.getElementStyles(fig), msg="Element styles should be [].") mgr.apply(fig, style4) self.checkPlot("MplStyleManager_testBasic_001", fig, msg="Apply by style") self.assertEqual(True, mgr.exists(style4), msg="Failed to auto add an applied style.") # This should be identical to *_001 mgr.apply(fig, 'Style #3') self.checkPlot("MplStyleManager_testBasic_002", fig, msg="Apply by name") style3.axes.labels.font.size = 16 mgr.reapply() self.checkPlot("MplStyleManager_testBasic_003", fig, msg="Re-Apply") mgr.set(fig, 'axes.labels.font.size', 24) self.checkPlot("MplStyleManager_testBasic_004", fig, msg="Set by name") mgr.set(fig, {'axes.labels.font.size': 16}) self.checkPlot("MplStyleManager_testBasic_005", fig, msg="Set by dict") mgr.setElementStyles(ax, [style2.name]) mgr.reapply() self.checkPlot("MplStyleManager_testBasic_006", fig, msg="Manually set element styles") tmpStyle = S.MplStyle("Temp Style") mgr.add(tmpStyle) self.assertRaises(Exception, mgr.add, tmpStyle, msg="Failed to throw on multiple adds.") mgr.erase(tmpStyle) result = mgr.find(tmpStyle.name) self.assertEqual(None, result, msg="Did not remove 'tmpStyle' from the manager.") msg = "Failed to throw on multiple removes." self.assertRaises(Exception, mgr.erase, tmpStyle, msg=msg) mgr.loadFile(self.inputFile("GoodStyle.mplstyle")) mgr.apply(fig, "Good Style") self.checkPlot("MplStyleManager_testBasic_007", fig, msg="Custom python script") # Check the get/set of the element name self.assertEqual([], mgr.getTags(fig), msg="Element name should be None") mgr.tag(fig, 'testName') self.assertEqual(['testName'], mgr.getTags(fig), msg="Failed to get and set the element name.")
def testBasic(self): """A basic test of MplStyle.""" # Setup the plot fig, ax = matplotlib.pyplot.subplots() patch = mpl.patches.Patch() ax.set_title('Axes Title') ax.set_xlabel("X-Axis Label") ax.set_ylabel("Y-Axis Label") xdata = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 4.75, 5] ydata = [1, 1.75, 2, 2.75, 3, 2.75, 2, 2.25, 2.75, 3] line = ax.plot(xdata, ydata) # There is only one line returned line = line[0] rect = mpl.patches.Rectangle((2.8, 1.0), 0.4, 1.2) ax.add_patch(rect) axText = ax.text(4.2, 1.1, "Axes Text") figText = fig.text(0.0, 0.0, "Figure Text") figLine = mpl.lines.Line2D([0.5, 0.5], [0.0, 1.0], color='red') fig._set_artist_props(figLine) fig.lines.append(figLine) figRect = mpl.patches.Rectangle((0.25, 0.25), 0.5, 0.5, facecolor='lightgreen', edgecolor='lightgreen', alpha=0.25) fig._set_artist_props(figRect) fig.patches.append(figRect) # Setup the style style = S.MplStyle("Test Style") style.bgColor = 'white' style.fgColor = 'black' # Figure style.figure.width = 10 style.figure.height = 10 # Axes style.axes.axisBelow = True style.axes.leftEdge.color = 'magenta' style.axes.leftEdge.width = 5 style.axes.leftEdge.style = '--' style.axes.bottomEdge.color = 'magenta' style.axes.bottomEdge.width = 5 style.axes.bottomEdge.style = 'dashed' style.axes.topEdge.visible = False style.axes.rightEdge.visible = False style.axes.title.font.scale = 2.0 style.axes.title.font.family = 'sans-serif' # X-Axis style.axes.xAxis.autoscale = True style.axes.xAxis.dataMargin = 0.2 style.axes.xAxis.label.font.scale = 1.2 style.axes.xAxis.majorTicks.labels.font.scale = 0.75 style.axes.xAxis.majorTicks.marks.visible = True style.axes.xAxis.majorTicks.grid.visible = True style.axes.xAxis.majorTicks.grid.color = '#B0B0B0' style.axes.xAxis.majorTicks.grid.width = 1.5 style.axes.xAxis.majorTicks.grid.style = ':' style.axes.xAxis.majorTicks.length = 15.0 style.axes.xAxis.majorTicks.width = 1.5 style.axes.xAxis.minorTicks.marks.visible = True style.axes.xAxis.minorTicks.grid.visible = True style.axes.xAxis.minorTicks.grid.color = '#B0B0B0' style.axes.xAxis.minorTicks.grid.width = 0.5 style.axes.xAxis.minorTicks.grid.style = ':' style.axes.xAxis.minorTicks.length = 5.0 style.axes.xAxis.minorTicks.width = 0.5 # Y-Axis style.axes.yAxis = style.axes.xAxis.copy() # Lines style.line.color = "blue" style.line.style = 'dash-dot' style.line.width = 1.5 style.line.marker.color = 'red' style.line.marker.edgeColor = 'green' style.line.marker.size = 12 style.line.marker.style = 'circle' style.line.marker.fill = 'bottom' # Patches style.patch.color = 'gold' style.patch.filled = True style.patch.edgeColor = 'purple' style.patch.edgeWidth = 5 # Text style.text.lineSpacing = 1.0 style.text.font.size = 12 style.text.font.family = 'monospace' # Check the dir function expected = [ '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_aliases', '_applyStyle', '_completed_init', '_getParentOfProperty', '_name', '_propertyNames', '_restricted_setattr', '_subStyle', 'apply', 'axes', 'bgColor', 'canApply', 'copy', 'custom', 'fgColor', 'figure', 'format', 'getPropertyType', 'getResolvedValue', 'getValue', 'hasAnySet', 'kwargs', 'line', 'name', 'parent', 'patch', 'propertyNames', 'resolve', 'resolveStyles', 'setValue', 'text', 'update' ] self.assertEqual(expected, dir(style), msg="Invalid 'dir' result.") # Check the str function style2 = S.MplStyle("Test Style #2") style2.update({ 'bgColor': 'white', 'fgColor': 'black', 'figure.width': 10, 'figure.height': 10, }) expected = """MplSubStyle: * bgColor = #FFFFFF * fgColor = #000000 * figure = MplFigureStyle: * height = 10.0 * width = 10.0""" self.assertEqual(expected, str(style2), msg="Invalid 'str' result.") # Check Property types self.assertEqual(S.types.property.MplColor, style.getPropertyType('fgColor'), msg="Invalid property type for 'fgColor'") self.assertEqual(S.types.property.SubStyle, style.getPropertyType('patch'), msg="Invalid property type for 'patch'") # Check 'canApply' self.assertEqual(True, style.canApply(fig), msg="Invalid 'canApply' for Figure") self.assertEqual(True, style.canApply(ax), msg="Invalid 'canApply' for Axes") self.assertEqual(True, style.canApply(line), msg="Invalid 'canApply' for Line2D") self.assertEqual(True, style.canApply(rect), msg="Invalid 'canApply' for Patch") self.assertEqual(True, style.canApply(axText), msg="Invalid 'canApply' for Text") self.assertEqual(False, style.canApply(1.23), msg="Invalid 'canApply' for Float") # Write to a file fname = "MplStyle_testBasic_001.png" fig.savefig(self.outputFile(fname)) msg = "Failed initial image plot check" self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg) # Apply the styles style.apply(fig, postProcess=doNothing) # Write to a file fname = "MplStyle_testBasic_002.png" fig.savefig(self.outputFile(fname)) msg = "Error formatting the plot properly." self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg) # Copy the style styleCopy = style.copy("Original Style") # Update the x-axis only style.axes.xAxis.dataMargin = 0.5 style.axes.xAxis.majorTicks.labels.rotation = -30.0 style.axes.yAxis.majorTicks.labels.rotation = -90.0 style.apply(ax.get_xaxis(), postProcess=doNothing) style.apply(ax.get_yaxis(), postProcess=doNothing) fname = "MplStyle_testBasic_003.png" fig.savefig(self.outputFile(fname)) self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg="axis is not correct") # Update the font properties style.text.font.size = 16 style.apply(ax.title.get_fontproperties(), postProcess=doNothing) fname = "MplStyle_testBasic_004.png" fig.savefig(self.outputFile(fname)) self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg="axes text is not correct") # Apply the new style to the entire figure style.apply(fig, postProcess=doNothing) fname = "MplStyle_testBasic_005.png" fig.savefig(self.outputFile(fname)) self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg="modified Style is not correct") # apply the copied style styleCopy.apply(fig, postProcess=doNothing) # This is almost identical to #002 -- the x-axis labels should be # rotated fname = "MplStyle_testBasic_006.png" fig.savefig(self.outputFile(fname)) self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg="Copied Style is not correct") # Make a new style invertedStyle = S.MplStyle("Inverted Style") invertedStyle.bgColor = 'black' invertedStyle.fgColor = 'white' styleCopy.update(invertedStyle) styleCopy.apply(ax, postProcess=doNothing) fname = "MplStyle_testBasic_007.png" fig.savefig(self.outputFile(fname)) self.checkImage(self.baselineFile(fname), self.outputFile(fname), 1.0e-3, msg="Updated Style is not correct")