|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright © 2009 Pierre Raybaut |
| 4 | +# Licensed under the terms of the MIT License |
| 5 | +# see the mpl licenses directory for a copy of the license |
| 6 | + |
| 7 | +"""Module that provides a GUI-based editor for matplotlib's figure options""" |
| 8 | + |
| 9 | +import os.path as osp |
| 10 | + |
| 11 | +import matplotlib.backends.qt4_editor.formlayout as formlayout |
| 12 | +from PyQt4.QtGui import QIcon |
| 13 | + |
| 14 | +def get_icon(name): |
| 15 | + import matplotlib |
| 16 | + basedir = osp.join(matplotlib.rcParams['datapath'], 'images') |
| 17 | + return QIcon(osp.join(basedir, name)) |
| 18 | + |
| 19 | +LINESTYLES = { |
| 20 | + '-': 'Solid', |
| 21 | + '--': 'Dashed', |
| 22 | + '-.': 'DashDot', |
| 23 | + ':': 'Dotted', |
| 24 | + 'steps': 'Steps', |
| 25 | + 'none': 'None', |
| 26 | + } |
| 27 | + |
| 28 | +MARKERS = { |
| 29 | + 'none': 'None', |
| 30 | + 'o': 'circles', |
| 31 | + '^': 'triangle_up', |
| 32 | + 'v': 'triangle_down', |
| 33 | + '<': 'triangle_left', |
| 34 | + '>': 'triangle_right', |
| 35 | + 's': 'square', |
| 36 | + '+': 'plus', |
| 37 | + 'x': 'cross', |
| 38 | + '*': 'star', |
| 39 | + 'D': 'diamond', |
| 40 | + 'd': 'thin_diamond', |
| 41 | + '1': 'tripod_down', |
| 42 | + '2': 'tripod_up', |
| 43 | + '3': 'tripod_left', |
| 44 | + '4': 'tripod_right', |
| 45 | + 'h': 'hexagon', |
| 46 | + 'H': 'rotated_hexagon', |
| 47 | + 'p': 'pentagon', |
| 48 | + '|': 'vertical_line', |
| 49 | + '_': 'horizontal_line', |
| 50 | + '.': 'dots', |
| 51 | + } |
| 52 | + |
| 53 | +COLORS = {'b': '#0000ff', 'g': '#00ff00', 'r': '#ff0000', 'c': '#ff00ff', |
| 54 | + 'm': '#ff00ff', 'y': '#ffff00', 'k': '#000000', 'w': '#ffffff'} |
| 55 | + |
| 56 | +def col2hex(color): |
| 57 | + """Convert matplotlib color to hex""" |
| 58 | + return COLORS.get(color, color) |
| 59 | + |
| 60 | +def figure_edit(axes, parent=None): |
| 61 | + """Edit matplotlib figure options""" |
| 62 | + sep = (None, None) # separator |
| 63 | + |
| 64 | + has_curve = len(axes.get_lines()) > 0 |
| 65 | + |
| 66 | + # Get / General |
| 67 | + xmin, xmax = axes.get_xlim() |
| 68 | + ymin, ymax = axes.get_ylim() |
| 69 | + general = [('Title', axes.get_title()), |
| 70 | + sep, |
| 71 | + (None, "<b>X-Axis</b>"), |
| 72 | + ('Min', xmin), ('Max', xmax), |
| 73 | + ('Label', axes.get_xlabel()), |
| 74 | + ('Scale', [axes.get_xscale(), 'linear', 'log']), |
| 75 | + sep, |
| 76 | + (None, "<b>Y-Axis</b>"), |
| 77 | + ('Min', ymin), ('Max', ymax), |
| 78 | + ('Label', axes.get_ylabel()), |
| 79 | + ('Scale', [axes.get_yscale(), 'linear', 'log']) |
| 80 | + ] |
| 81 | + |
| 82 | + if has_curve: |
| 83 | + # Get / Curves |
| 84 | + linedict = {} |
| 85 | + for line in axes.get_lines(): |
| 86 | + label = line.get_label() |
| 87 | + if label == '_nolegend_': |
| 88 | + continue |
| 89 | + linedict[label] = line |
| 90 | + curves = [] |
| 91 | + linestyles = LINESTYLES.items() |
| 92 | + markers = MARKERS.items() |
| 93 | + curvelabels = sorted(linedict.keys()) |
| 94 | + for label in curvelabels: |
| 95 | + line = linedict[label] |
| 96 | + curvedata = [ |
| 97 | + ('Label', label), |
| 98 | + sep, |
| 99 | + (None, '<b>Line</b>'), |
| 100 | + ('Style', [line.get_linestyle()] + linestyles), |
| 101 | + ('Width', line.get_linewidth()), |
| 102 | + ('Color', col2hex(line.get_color())), |
| 103 | + sep, |
| 104 | + (None, '<b>Marker</b>'), |
| 105 | + ('Style', [line.get_marker()] + markers), |
| 106 | + ('Size', line.get_markersize()), |
| 107 | + ('Facecolor', col2hex(line.get_markerfacecolor())), |
| 108 | + ('Edgecolor', col2hex(line.get_markeredgecolor())), |
| 109 | + ] |
| 110 | + curves.append([curvedata, label, ""]) |
| 111 | + |
| 112 | + datalist = [(general, "Axes", "")] |
| 113 | + if has_curve: |
| 114 | + datalist.append((curves, "Curves", "")) |
| 115 | + result = formlayout.fedit(datalist, title="Figure options", parent=parent, |
| 116 | + icon=get_icon('qt4_editor_options.svg')) |
| 117 | + if result is None: |
| 118 | + return |
| 119 | + |
| 120 | + if has_curve: |
| 121 | + general, curves = result |
| 122 | + else: |
| 123 | + general, = result |
| 124 | + |
| 125 | + # Set / General |
| 126 | + title, xmin, xmax, xlabel, xscale, ymin, ymax, ylabel, yscale = general |
| 127 | + axes.set_xscale(xscale) |
| 128 | + axes.set_yscale(yscale) |
| 129 | + axes.set_title(title) |
| 130 | + axes.set_xlim(xmin, xmax) |
| 131 | + axes.set_xlabel(xlabel) |
| 132 | + axes.set_ylim(ymin, ymax) |
| 133 | + axes.set_ylabel(ylabel) |
| 134 | + |
| 135 | + if has_curve: |
| 136 | + # Set / Curves |
| 137 | + for index, curve in enumerate(curves): |
| 138 | + line = linedict[curvelabels[index]] |
| 139 | + label, linestyle, linewidth, color, \ |
| 140 | + marker, markersize, markerfacecolor, markeredgecolor = curve |
| 141 | + line.set_label(label) |
| 142 | + line.set_linestyle(linestyle) |
| 143 | + line.set_linewidth(linewidth) |
| 144 | + line.set_color(color) |
| 145 | + if marker is not 'none': |
| 146 | + line.set_marker(marker) |
| 147 | + line.set_markersize(markersize) |
| 148 | + line.set_markerfacecolor(markerfacecolor) |
| 149 | + line.set_markeredgecolor(markeredgecolor) |
| 150 | + |
| 151 | + # Redraw |
| 152 | + figure = axes.get_figure() |
| 153 | + figure.canvas.draw() |
| 154 | + |
0 commit comments