Re: [matplotlib-devel] Proposal for Broken Axes
ayuffa, on 2011-07-07 13:54, wrote: Have your changes to axes.py, namely breakx and breaky, been accepted? If not, could you post your axes.py file. Here's an example, I'm looking into why it's not making it to the official docs right now, but you should be able to run it locally: https://github.com/matplotlib/matplotlib/blob/master/examples/pylab_examples/broken_axis.py best, -- Paul Ivanov 314 address only used for lists, off-list direct email at: http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7 signature.asc Description: Digital signature -- All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2d-c2___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Re: [matplotlib-devel] Proposal for Broken Axes
First of all, thanks, klukas for the useful piece of code. Jae-Joon Lee wrote: On Mon, Mar 29, 2010 at 12:30 PM, Jeff Klukas klu...@wisc.edu wrote: # Create BrokenAxes with bottom from 0 to 5 and top from 30 to 35 ax = plt.broken_axes(ybounds=[0.,5.,30.,35.]) # Plot a line onto BOTH subaxes ax.plot(range(35),range(35)) The call to plot would get routed through __getattribute__, which would then call plot for each of the subaxes. This would be much more intuitive than my existing breaky solution, where you have to loop over all subaxes and plot on each individually. How do you want to handle l1, = ax.plot(range(35), range(35)) l1.set_color(r) then? Well, I guess BrokenAxes.plot should return a list of lines instead of a line in ll. i.e. something like [[x.lines[-1] for x in ax.subaxes]] would replace [ax.lines[-1]] as the return value. Better yet, instead of a list we should have a vector-type proxy container that should transfer method calls to the contained items. I think keeping two (or more) separate artists for each axes while an user think there is only one artist (because only one axes is exposed to the user) is not a good idea. Ideally this should really be one artist. However from JDH's response I understand this would be harder to implement (using custom transforms or something). Maybe emulating one using the current implementation (as I suggested above) is good enough. Meanwhile, this redundant looping for each plot call is annoying, so I can offer the following compromise: store the subaxes in the parent (broken)Axes (add self._subaxes = subaxes before returning from breakx and breaky), then add a new plot_subs method: def plot_subs(self,*args,**keys): for sub in self._subaxes: res = sub.plot(*args,**keys) return res This is a simplified version, returning just the lines of the last subaxes, but at least this way you can avoid the looping. Regards, Amit A. -- Download Intel#174; Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev ___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Re: [matplotlib-devel] Proposal for Broken Axes
I haven't heard a response back about the proposal I posted for broken axes. Hopefully that just means people are busy :). If there are concerns about the method or interface, I'm certainly open to hearing them. In the meantime, I've been thinking about the interface, and I think the more correct and more ambitious thing to do would be to create a new BrokenAxes class that inherits from Axes. The class could redefine __getattribute__ to pass most function calls straight to the subaxes. So in the end a session could look like the following: # Create BrokenAxes with bottom from 0 to 5 and top from 30 to 35 ax = plt.broken_axes(ybounds=[0.,5.,30.,35.]) # Plot a line onto BOTH subaxes ax.plot(range(35),range(35)) The call to plot would get routed through __getattribute__, which would then call plot for each of the subaxes. This would be much more intuitive than my existing breaky solution, where you have to loop over all subaxes and plot on each individually. The more ambitious thing to do would be to also define a BrokenAxis class that inherits from Axis and would redefine get_ticklabel_extents to look as each subaxis and push the axis label far enough out to clear the ticklabels on all subaxes. Does that new interface sound like a good idea? Are there any show-stopping problems that seem apparent. If it sounds like something worth trying, I could take a stab at writing an implementation. Cheers, Jeff || Jeff Klukas, Research Assistant, Physics || University of Wisconsin -- Madison || jeff.klu...@gmail | jeffyklu...@aim | jeffklu...@skype || http://www.hep.wisc.edu/~jklukas/ On Thu, Mar 18, 2010 at 1:38 PM, Jeff Klukas klu...@wisc.edu wrote: I have implemented breakx and breaky methods for the Axes class and attached the diff for axes.py to this message. You can test out the function with the following examples: -- import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt # Broken y fig = plt.figure() main_axes = plt.axes() plt.title('Broken x-axis example') plt.xlabel('x-axis label') subaxes = main_axes.breaky([0., 1.9, 5.1, 6.9, 9.1, 12]) for axes in subaxes: axes.plot(np.linspace(0,12,13),np.linspace(0,12,13)) plt.ylabel('y-axis label') plt.show() -- import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt # Broken x fig = plt.figure() main_axes = plt.axes() plt.title('Broken x-axis example') plt.ylabel('y-axis label') subaxes = main_axes.breakx([0., 1.9, 5.1, 6.9, 9.1, 12]) for axes in subaxes: axes.plot(np.linspace(0,12,13),np.linspace(0,12,13)) plt.xlabel('x-axis label') plt.show() - I've included in the docstrings some of the TODO items, but this is pretty stable in its current form. Cheers, Jeff || Jeff Klukas, Research Assistant, Physics || University of Wisconsin -- Madison || jeff.klu...@gmail | jeffyklu...@aim | jeffklu...@skype || http://www.hep.wisc.edu/~jklukas/ On Tue, Mar 16, 2010 at 1:08 PM, Jeff Klukas klu...@wisc.edu wrote: What would be great is if you could refactor the basic functionality into a matplotlib.Axes.breaky method (and possibly breakx but most people request a broken y axis), which would resize the self axes and return the broken compliment which could be plotted onto. Then you could provide a thin pyplot wrapper much like pyplot.twinx, so that pyplot as well as API users could benefit. I can try to do this. I think I would prefer, however, not to resize the self axes and continue with my current approach of creating two new axes within the original axes. On the user end, I think it makes more sense to set the title and ylabel of the main axes, rather than setting them for the individual upper and lower axes. More on that below. The only real problems here is that you need to explicitly plot things on both the upper and lower axes, and then I haven't figured out how to push out the y-axis label of the main axes object so it doesn't overlap with the tick labels of the upper and lower axes. So, I instead moved the y-labels of the upper and lower axes so that they appear at the center of the axis, but this is problematic. Any thoughts on how to do that part better? klukas, I'm afraid I don't understand your issue... Can you explain using it differently? In my approach, you end up with a main axes object that is invisible, and then two visible axes objects (upper and lower) within the main axes. I would ideally like to have the y label display in the middle of the main y-axis, independent of where the break lies. If I place a y label on the main axes (which has ticks or tick labels), though, it appears right up against the axis line. I'd like it to be placed further to the left, clear of the tick labels that appear on the upper and lower axes. So, I'd like to be able to access whatever algorithm is used to choose the offset of the axis label, and explicitly set the offset of
Re: [matplotlib-devel] Proposal for Broken Axes
On Mon, Mar 29, 2010 at 12:30 PM, Jeff Klukas klu...@wisc.edu wrote: # Create BrokenAxes with bottom from 0 to 5 and top from 30 to 35 ax = plt.broken_axes(ybounds=[0.,5.,30.,35.]) # Plot a line onto BOTH subaxes ax.plot(range(35),range(35)) The call to plot would get routed through __getattribute__, which would then call plot for each of the subaxes. This would be much more intuitive than my existing breaky solution, where you have to loop over all subaxes and plot on each individually. How do you want to handle l1, = ax.plot(range(35), range(35)) l1.set_color(r) then? I think keeping two (or more) separate artists for each axes while an user think there is only one artist (because only one axes is exposed to the user) is not a good idea. Regards, -JJ -- Download Intel#174; Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev ___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Re: [matplotlib-devel] Proposal for Broken Axes
What would be great is if you could refactor the basic functionality into a matplotlib.Axes.breaky method (and possibly breakx but most people request a broken y axis), which would resize the self axes and return the broken compliment which could be plotted onto. Then you could provide a thin pyplot wrapper much like pyplot.twinx, so that pyplot as well as API users could benefit. I can try to do this. I think I would prefer, however, not to resize the self axes and continue with my current approach of creating two new axes within the original axes. On the user end, I think it makes more sense to set the title and ylabel of the main axes, rather than setting them for the individual upper and lower axes. More on that below. The only real problems here is that you need to explicitly plot things on both the upper and lower axes, and then I haven't figured out how to push out the y-axis label of the main axes object so it doesn't overlap with the tick labels of the upper and lower axes. So, I instead moved the y-labels of the upper and lower axes so that they appear at the center of the axis, but this is problematic. Any thoughts on how to do that part better? klukas, I'm afraid I don't understand your issue... Can you explain using it differently? In my approach, you end up with a main axes object that is invisible, and then two visible axes objects (upper and lower) within the main axes. I would ideally like to have the y label display in the middle of the main y-axis, independent of where the break lies. If I place a y label on the main axes (which has ticks or tick labels), though, it appears right up against the axis line. I'd like it to be placed further to the left, clear of the tick labels that appear on the upper and lower axes. So, I'd like to be able to access whatever algorithm is used to choose the offset of the axis label, and explicitly set the offset of the ylabel for the main axes so that it clears the tick labels. // Jeff -- Download Intel#174; Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev ___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
[matplotlib-devel] Proposal for Broken Axes
It's my understanding that there is no built-in method for generating a broken axis (where you skip over some range of values, indicating this with some graphical mark). I wanted to do this, so I've put together a function which seems to be fairly robust, and I thought I might propose it as a starting point if there's interest in having a built-in facility for broken axes. Please let me know if this is not the appropriate place to be submitting this suggestion. The basic idea of the below function is that you feed is an axes object along with an iterable giving the bounds of each section of the y-axis. It then returns new upper and lower axes objects, after masking spines, calculating distances, etc. The only real problems here is that you need to explicitly plot things on both the upper and lower axes, and then I haven't figured out how to push out the y-axis label of the main axes object so it doesn't overlap with the tick labels of the upper and lower axes. So, I instead moved the y-labels of the upper and lower axes so that they appear at the center of the axis, but this is problematic. Any thoughts on how to do that part better? http://old.nabble.com/file/p27909750/broken.png -- from matplotlib import pyplot as plt def axes_broken_y(axes, upper_frac=0.5, break_frac=0.02, ybounds=None, xlabel=None, ylabel=None): Replace the current axes with a set of upper and lower axes. The new axes will be transparent, with a breakmark drawn between them. They share the x-axis. Returns (upper_axes, lower_axes). If ybounds=[ymin_lower, ymax_lower, ymin_upper, ymax_upper] is defined, upper_frac will be ignored, and the y-axis bounds will be fixed with the specified values. def breakmarks(axes, y_min, y_max, xwidth=0.008): x1, y1, x2, y2 = axes.get_position().get_points().flatten().tolist() segment_height = (y_max - y_min) / 3. xoffsets = [0, +xwidth, -xwidth, 0] yvalues = [y_min + (i * segment_height) for i in range(4)] # Get color of y-axis for loc, spine in axes.spines.iteritems(): if loc == 'left': color = spine.get_edgecolor() for x_position in [x1, x2]: line = matplotlib.lines.Line2D( [x_position + offset for offset in xoffsets], yvalues, transform=plt.gcf().transFigure, clip_on=False, color=color) axes.add_line(line) # Readjust upper_frac if ybounds are defined if ybounds: if len(ybounds) != 4: print len(ybounds) != 4; aborting... return ymin1, ymax1, ymin2, ymax2 = [float(value) for value in ybounds] data_height1, data_height2 = (ymax1 - ymin1), (ymax2 - ymin2) upper_frac = data_height2 / (data_height1 + data_height2) x1, y1, x2, y2 = axes.get_position().get_points().flatten().tolist() width = x2 - x1 lower_height = (y2 - y1) * ((1 - upper_frac) - 0.5 * break_frac) upper_height = (y2 - y1) * (upper_frac - 0.5 * break_frac) upper_bottom = (y2 - y1) - upper_height + y1 lower_axes = plt.axes([x1, y1, width, lower_height], axisbg='None') upper_axes = plt.axes([x1, upper_bottom, width, upper_height], axisbg='None', sharex=lower_axes) # Erase the edges between the axes for loc, spine in upper_axes.spines.iteritems(): if loc == 'bottom': spine.set_color('none') for loc, spine in lower_axes.spines.iteritems(): if loc == 'top': spine.set_color('none') upper_axes.get_xaxis().set_ticks_position('top') lower_axes.get_xaxis().set_ticks_position('bottom') plt.setp(upper_axes.get_xticklabels(), visible=False) breakmarks(upper_axes, y1 + lower_height, upper_bottom) # Set ylims if ybounds are defined if ybounds: lower_axes.set_ylim(ymin1, ymax1) upper_axes.set_ylim(ymin2, ymax2) lower_axes.set_autoscaley_on(False) upper_axes.set_autoscaley_on(False) upper_axes.yaxis.get_label().set_position((0, 1 - (0.5 / (upper_frac/(1+break_frac) lower_axes.yaxis.get_label().set_position((0, 0.5 / ((1 - upper_frac)/(1+break_frac # Make original axes invisible axes.set_xticks([]) axes.set_yticks([]) print upper_axes.yaxis.get_label().get_position() print lower_axes.yaxis.get_label().get_position() print axes.yaxis.get_label().get_position() print axes.yaxis.labelpad for loc, spine in axes.spines.iteritems(): spine.set_color('none') return upper_axes, lower_axes def prepare_efficiency(axes, lower_bound=0.69): Set up an efficiency figure with breakmarks to indicate a suppressed zero. The y-axis limits are set to (lower_bound, 1.0), as appropriate for an efficiency plot, and autoscaling is turned off. upper_axes, lower_axes = axes_broken_y(axes, upper_frac=0.97) lower_axes.set_yticks([])
Re: [matplotlib-devel] Proposal for Broken Axes
On Mon, Mar 15, 2010 at 3:16 PM, klukas klu...@wisc.edu wrote: It's my understanding that there is no built-in method for generating a broken axis (where you skip over some range of values, indicating this with some graphical mark). I wanted to do this, so I've put together a function which seems to be fairly robust, and I thought I might propose it as a starting point if there's interest in having a built-in facility for broken axes. Please let me know if this is not the appropriate place to be submitting this suggestion. This is a nice start of an oft requested feature, and we are definitely interested. It is enabled by the spine contribution of Andrew, so you can turn off the upper and lower spines between the break, so it is nice to see some unintended benefits of his refactoring. From a usability standpoint, one thing we try to do is make pyplot a thin wrapper around functionality that exists in the API proper in matplotlib.figure, matplotlib.axes, etc. Functionally and in terms of implementation, this broken axes implementation is in the style of twinx which makes two axes for plotting on different scales http://matplotlib.sourceforge.net/examples/api/two_scales.html http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.twinx http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.twinx What would be great is if you could refactor the basic functionality into a matplotlib.Axes.breaky method (and possibly breakx but most people request a broken y axis), which would resize the self axes and return the broken compliment which could be plotted onto. Then you could provide a thin pyplot wrapper much like pyplot.twinx, so that pyplot as well as API users could benefit. Finally, an svn patch which provided an example and patches to axes.py and pyplot.py would be most helpful. http://matplotlib.sourceforge.net/faq/howto_faq.html#submit-a-patch An alternative implementation could craft a custom transform using some custom artists for spines, but this might be a good bit harder. Do you have an opinion Andrew on this approach? JDH -- Download Intel#174; Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev ___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Re: [matplotlib-devel] Proposal for Broken Axes
John Hunter wrote: On Mon, Mar 15, 2010 at 3:16 PM, klukas klu...@wisc.edu wrote: It's my understanding that there is no built-in method for generating a broken axis (where you skip over some range of values, indicating this with some graphical mark). I wanted to do this, so I've put together a function which seems to be fairly robust, and I thought I might propose it as a starting point if there's interest in having a built-in facility for broken axes. This is a nice start of an oft requested feature, and we are definitely interested. It is enabled by the spine contribution of Andrew, so you can turn off the upper and lower spines between the break, so it is nice to see some unintended benefits of his refactoring. An alternative implementation could craft a custom transform using some custom artists for spines, but this might be a good bit harder. Do you have an opinion Andrew on this approach? John, I'm attaching a helper function I wrote to do just this. Unfortunately, I don't have time to attempt to merge this into MPL right now... On Mon, Mar 15, 2010 at 3:16 PM, klukas klu...@wisc.edu wrote: The only real problems here is that you need to explicitly plot things on both the upper and lower axes, and then I haven't figured out how to push out the y-axis label of the main axes object so it doesn't overlap with the tick labels of the upper and lower axes. So, I instead moved the y-labels of the upper and lower axes so that they appear at the center of the axis, but this is problematic. Any thoughts on how to do that part better? klukas, I'm afraid I don't understand your issue... Can you explain using it differently? -Andrew def add_spine_break(ax,spine_name, data_loc, data_shift_amount=5, data_width=10, axes_shift_amount = 0.05, axis='y'): draw a white parallelogram patch over the axis line at data_loc import matplotlib.path as mpath import matplotlib.patches as mpatches t = ax.spines[spine_name].get_transform() axes_coords = [-axes_shift_amount, axes_shift_amount, axes_shift_amount, -axes_shift_amount] data_coords = [ data_loc-data_shift_amount, data_loc+data_shift_amount, data_loc+data_shift_amount+data_width, data_loc-data_shift_amount+data_width ] if axis=='y': xs = axes_coords ys = data_coords elif axis=='x': xs = data_coords ys = axes_coords else: raise ValueError('unknown axis: %s'%axis) result = {} if 1: # white patch verts = [] for xi,yi in zip(xs,ys): verts.append( (xi,yi) ) verts.append( (0,0) ) codes = [mpath.Path.MOVETO] + \ [mpath.Path.LINETO]*(len(verts)-2) + \ [mpath.Path.CLOSEPOLY] path = mpath.Path( verts, codes ) patch = mpatches.PathPatch(path,edgecolor='none',facecolor='w') ax.add_artist(patch) patch.set_clip_on(False) patch.set_transform(t) patch.set_zorder(100) result['white_patch']=patch if 1: # black lines verts = [] for xi,yi in zip(xs,ys): verts.append( (xi,yi) ) codes = [mpath.Path.MOVETO] + \ [mpath.Path.LINETO] + \ [mpath.Path.MOVETO] + \ [mpath.Path.LINETO] path = mpath.Path( verts, codes ) patch = mpatches.PathPatch(path,edgecolor='k',facecolor='none') ax.add_artist(patch) patch.set_clip_on(False) patch.set_transform(t) patch.set_zorder(101) result['black_lines']=patch -- Download Intel#174; Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev___ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel