8000 fix multiple whitespace issues in svg_*.py examples (E293, E291) · matplotlib/matplotlib@8cae397 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cae397

Browse files
committed
fix multiple whitespace issues in svg_*.py examples (E293, E291)
1 parent 5cba226 commit 8cae397

File tree

2 files changed

+69
-67
lines changed

2 files changed

+69
-67
lines changed

examples/user_interfaces/svg_histogram.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33

44
"""
55
Demonstrate how to create an interactive histogram, in which bars
6-
are hidden or shown by cliking on legend markers.
6+
are hidden or shown by cliking on legend markers.
77
8-
The interactivity is encoded in ecmascript (javascript) and inserted in
9-
the SVG code in a post-processing step. To render the image, open it in
10-
a web browser. SVG is supported in most web browsers used by Linux and
11-
OSX users. Windows IE9 supports SVG, but earlier versions do not.
8+
The interactivity is encoded in ecmascript (javascript) and inserted in
9+
the SVG code in a post-processing step. To render the image, open it in
10+
a web browser. SVG is supported in most web browsers used by Linux and
11+
OSX users. Windows IE9 supports SVG, but earlier versions do not.
1212
1313
Notes
1414
-----
1515
The matplotlib backend lets us assign ids to each object. This is the
16-
mechanism used here to relate matplotlib objects created in python and
16+
mechanism used here to relate matplotlib objects created in python and
1717
the corresponding SVG constructs that are parsed in the second step.
18-
While flexible, ids are cumbersome to use for large collection of
18+
While flexible, ids are cumbersome to use for large collection of
1919
objects. Two mechanisms could be used to simplify things:
2020
* systematic grouping of objects into SVG <g> tags,
2121
* assingning classes to each SVG object according to its origin.
22-
23-
For example, instead of modifying the properties of each individual bar,
22+
23+
For example, instead of modifying the properties of each individual bar,
2424
the bars from the `hist` function could either be grouped in
25-
a PatchCollection, or be assigned a class="hist_##" attribute.
25+
a PatchCollection, or be assigned a class="hist_##" attribute.
2626
2727
CSS could also be used more extensively to replace repetitive markup
28-
troughout the generated SVG.
28+
troughout the generated SVG.
2929
3030
__author__="david.huard@gmail.com"
3131
@@ -40,7 +40,7 @@
4040

4141
plt.rcParams['svg.embed_char_paths'] = 'none'
4242

43-
# Apparently, this `register_namespace` method works only with
43+
# Apparently, this `register_namespace` method works only with
4444
# python 2.7 and up and is necessary to avoid garbling the XML name
4545
# space with ns0.
4646
ET.register_namespace("", "http://www.w3.org/2000/svg")
@@ -54,11 +54,11 @@
5454
H = plt.hist([r, r1], label=labels)
5555
containers = H[-1]
5656
leg = plt.legend(frameon=False)
57-
plt.title("""From a web browser, click on the legend
57+
plt.title("""From a web browser, click on the legend
5858
marker to toggle the corresponding histogram.""")
5959

6060

61-
# --- Add ids to the svg objects we'll modify
61+
# --- Add ids to the svg objects we'll modify
6262

6363
hist_patches = {}
6464
for ic, c in enumerate(containers):
@@ -67,7 +67,7 @@
6767
element.set_gid('hist_%d_patch_%d' % (ic, il))
6868
hist_patches['hist_%d' %ic].append('hist_%d_patch_%d' % (ic, il))
6969

70-
# Set ids for the legend patches
70+
# Set ids for the legend patches
7171
for i, t in enumerate(leg.get_patches()):
7272
t.set_gid('leg_patch_%d' % i)
7373

@@ -85,32 +85,32 @@
8585

8686
# --- Add interactivity ---
8787

88-
# Add attributes to the patch objects.
88+
# Add attributes to the patch objects.
8989
for i, t in enumerate(leg.get_patches()):
9090
el = xmlid['leg_patch_%d' % i]
9191
el.set('cursor', 'pointer')
9292
el.set('onclick', "toggle_hist(this)")
9393

94-
# Add attributes to the text objects.
94+
# Add attributes to the text objects.
9595
for i, t in enumerate(leg.get_texts()):
9696
el = xmlid['leg_text_%d' % i]
9797
el.set('cursor', 'pointer')
9898
el.set('onclick', "toggle_hist(this)")
9999

100-
# Create script defining the function `toggle_hist`.
101-
# We create a global variable `container` that stores the patches id
102-
# belonging to each histogram. Then a function "toggle_element" sets the
100+
# Create script defining the function `toggle_hist`.
101+
# We create a global variable `container` that stores the patches id
102+
# belonging to each histogram. Then a function "toggle_element" sets the
103103
# visibility attribute of all patches of each histogram and the opacity
104-
# of the marker itself.
104+
# of the marker itself.
105105

106106
script = """
107107
<script type="text/ecmascript">
108108
<![CDATA[
109-
var container = %s
109+
var container = %s
110110
111111
function toggle(oid, attribute, values) {
112-
/* Toggle the style attribute of an object between two values.
113-
112+
/* Toggle the style attribute of an object between two values.
113+
114114
Parameters
115115
----------
116116
oid : str
@@ -122,20 +122,20 @@
122122
*/
123123
var obj = document.getElementById(oid);
124124
var a = obj.style[attribute];
125-
125+
126126
a = (a == values[0] || a == "") ? values[1] : values[0];
127127
obj.style[attribute] = a;
128128
}
129129
130130
function toggle_hist(obj) {
131-
131+
132132
var num = obj.id.slice(-1);
133-
133+
134134
toggle('leg_patch_' + num, 'opacity', [1, 0.3]);
135135
toggle('leg_text_' + num, 'opacity', [1, 0.5]);
136-
136+
137137
var names = container['hist_'+num]
138-
138+
139139
for (var i=0; i < names.length; i++) {
140140
toggle(names[i], 'opacity', [1,0])
141141
};
@@ -146,7 +146,8 @@
146146

147147
# Add a transition effect
148148
css = tree.getchildren()[0][0]
149-
css.text = css.text + "g {-webkit-transition:opacity 0.4s ease-out;-moz-transition:opacity 0.4s ease-out;}"
149+
css.text = css.text + "g {-webkit-transition:opacity 0.4s ease-out;" + \
150+
"-moz-transition:opacity 0.4s ease-out;}"
150151

151152
# Insert the script and save to file.
152153
tree.insert(0, ET.XML(script))

examples/user_interfaces/svg_tooltip.py

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
SVG tooltip example
33
===================
44
5-
This example shows how to create a tooltip that will show up when
6-
hovering over a matplotlib patch.
5+
This example shows how to create a tooltip that will show up when
6+
hovering over a matplotlib patch.
77
8-
Although it is possible to create the tooltip from CSS or javascript,
9-
here we create it in matplotlib and simply toggle its visibility on
10-
when hovering over the patch. This approach provides total control over
8+
Although it is possible to create the tooltip from CSS or javascript,
9+
here we create it in matplotlib and simply toggle its visibility on
10+
when hovering over the patch. This approach provides total control over
1111
the tooltip placement and appearance, at the expense of more code up
12-
front.
12+
front.
1313
14-
The alternative approach would be to put the tooltip content in `title`
15-
atttributes of SVG objects. Then, using an existing js/CSS library, it
16-
would be relatively straightforward to create the tooltip in the
17-
browser. The content would be dictated by the `title` attribute, and
18-
the appearance by the CSS.
14+
The alternative approach would be to put the tooltip content in `title`
15+
atttributes of SVG objects. Then, using an existing js/CSS library, it
16+
would be relatively straightforward to create the tooltip in the
17+
browser. The content would be dictated by the `title` attribute, and
18+
the appearance by the CSS.
1919
2020
2121
:author: David Huard
@@ -38,26 +38,27 @@
3838
ax.add_patch(rect)
3939

4040
# Create the tooltips
41-
circle_tip = ax.annotate('This is a blue circle.',
42-
xy=(0, 0),
43-
xytext=(30, -30),
44-
textcoords='offset points',
45-
color='w',
46-
ha='left',
47-
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92), ec=(1., 1., 1.), lw=1, zorder=1),
48-
)
49-
50-
rect_tip = ax.annotate('This is a green rectangle.',
51-
xy=(-5, 10),
52-
xytext=(30, 40),
53-
textcoords='offset points',
54-
color='w',
55-
ha='left',
56-
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92), ec=(1., 1., 1.), lw=1, zorder=1),
57-
)
58-
59-
60-
# Set id for the patches
41+
circle_tip = ax.annotate(
42+
'This is a blue circle.',
43+
xy=(0, 0),
44+
xytext=(30, -30),
45+
textcoords='offset points',
46+
color='w',
47+
ha='left',
48+
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92),
49+
ec=(1., 1., 1.), lw=1, zorder=1))
50+
51+
rect_tip = ax.annotate(
52+
'This is a green rectangle.',
53+
xy=(-5, 10),
54+
xytext=(30, 40),
55+
textcoords='offset points',
56+
color='w',
57+
ha='left',
58+
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92),
59+
ec=(1., 1., 1.), lw=1, zorder=1))
60+
61+
# Set id for the patches
6162
for i, t in enumerate(ax.patches):
6263
t.set_gid('patch_%d' % i)
6364

@@ -85,7 +86,7 @@
8586
el = xmlid['tooltip_%d' % i]
8687
el.set('visibility', 'hidden')
8788

88-
# Assign onmouseover and onmouseout callbacks to patches.
89+
# Assign onmouseover and onmouseout callbacks to patches.
8990
for i, t in enumerate(ax.patches):
9091
el = xmlid['patch_%d' % i]
9192
el.set('onmouseover', "ShowTooltip(this)")
@@ -95,26 +96,26 @@
9596
script = """
9697
<script type="text/ecmascript">
9798
<![CDATA[
98-
99+
99100
function init(evt) {
100101
if ( window.svgDocument == null ) {
101102
svgDocument = evt.target.ownerDocument;
102103
}
103104
}
104-
105+
105106
function ShowTooltip(obj) {
106107
var cur = obj.id.slice(-1);
107-
108+
108109
var tip = svgDocument.getElementById('tooltip_' + cur);
109110
tip.setAttribute('visibility',"visible")
110111
}
111-
112+
112113
function HideTooltip(obj) {
113114
var cur = obj.id.slice(-1);
114115
var tip = svgDocument.getElementById('tooltip_' + cur);
115116
tip.setAttribute('visibility',"hidden")
116117
}
117-
118+
118119
]]>
119120
</script>
120121
"""

0 commit comments

Comments
 (0)
0