8000 Merge pull request #24705 from anntzer/rgba · matplotlib/matplotlib@2b1da3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b1da3f

Browse files
authored
Merge pull request #24705 from anntzer/rgba
Cleanup a few examples.
2 parents 00ec4ec + 72b3f16 commit 2b1da3f

File tree

3 files changed

+17
-35
lines changed

3 files changed

+17
-35
lines changed

examples/misc/demo_ribbon_box.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RibbonBox:
2424
nx = original_image.shape[1]
2525

2626
def __init__(self, color):
27-
rgb = mcolors.to_rgba(color)[:3]
27+
rgb = mcolors.to_rgb(color)
2828
self.im = np.dstack(
2929
[self.b_and_h - self.color * (1 - np.array(rgb)), self.alpha])
3030

examples/shapes_and_collections/collections.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
The third subplot will make regular polygons, with the same
1212
type of scaling and positioning as in the first two.
1313
14-
The last subplot illustrates the use of "offsets=(xo, yo)",
14+
The last subplot illustrates the use of ``offsets=(xo, yo)``,
1515
that is, a single tuple instead of a list of tuples, to generate
1616
successively offset curves, with the offset given in data
1717
units. This behavior is available only for the LineCollection.
1818
"""
1919

2020
import matplotlib.pyplot as plt
21-
from matplotlib import collections, colors, transforms
21+
from matplotlib import collections, transforms
2222
import numpy as np
2323

2424
nverts = 50
@@ -38,8 +38,7 @@
3838
xyo = rs.randn(npts, 2)
3939< 10000 code class="diff-text syntax-highlighted-line">

4040
# Make a list of colors cycling through the default series.
41-
colors = [colors.to_rgba(c)
42-
for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
41+
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
4342

4443
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
4544
fig.subplots_adjust(top=0.92, left=0.07, right=0.97,

examples/shapes_and_collections/line_collection.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
"""
2-
===============
3-
Line Collection
4-
===============
2+
=============================================
3+
Plotting multiple lines with a LineCollection
4+
=============================================
55
6-
Plotting lines with Matplotlib.
7-
8-
`~matplotlib.collections.LineCollection` allows one to plot multiple
9-
lines on a figure. Below we show off some of its properties.
6+
Matplotlib can efficiently draw multiple lines at once using a
7+
`~.LineCollection`, as showcased below.
108
"""
119

1210
import matplotlib.pyplot as plt
1311
from matplotlib.collections import LineCollection
14-
from matplotlib import colors as mcolors
1512

1613
import numpy as np
1714

18-
# In order to efficiently plot many lines in a single set of axes,
19-
# Matplotlib has the ability to add the lines all at once. Here is a
20-
# simple example showing how it is done.
2115

2216
x = np.arange(100)
2317
# Here are many sets of y to plot vs. x
@@ -30,7 +24,7 @@
3024
# Mask some values to test masked array support:
3125
segs = np.ma.masked_where((segs > 50) & (segs < 60), segs)
3226

33-
# We need to set the plot limits.
27+
# We need to set the plot limits, they will not autoscale
3428
fig, ax = plt.subplots()
3529
ax.set_xlim(x.min(), x.max())
3630
ax.set_ylim(ys.min(), ys.max())
@@ -41,8 +35,7 @@
4135
# onoffseq is an even length tuple of on and off ink in points. If linestyle
4236
# is omitted, 'solid' is used.
4337
# See `matplotlib.collections.LineCollection` for more information.
44-
colors = [mcolors.to_rgba(c)
45-
for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
38+
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
4639

4740
line_segments = LineCollection(segs, linewidths=(0.5, 1, 1.5, 2),
4841
colors=colors, linestyle='solid')
@@ -51,32 +44,22 @@
5144
plt.show()
5245

5346
###############################################################################
54-
# In order to efficiently plot many lines in a single set of axes,
55-
# Matplotlib has the ability to add the lines all at once. Here is a
56-
# simple example showing how it is done.
47+
# In the following example, instead of passing a list of colors
48+
# (``colors=colors``), we pass an array of values (``array=x``) that get
49+
# colormapped.
5750

5851
N = 50
5952
x = np.arange(N)
60-
# Here are many sets of y to plot vs. x
61-
ys = [x + i for i in x]
53+
ys = [x + i for i in x] # Many sets of y to plot vs. x
54+
segs = [np.column_stack([x, y]) for y in ys]
6255

63-
# We need to set the plot limits, they will not autoscale
6456
fig, ax = plt.subplots()
6557
ax.set_xlim(np.min(x), np.max(x))
6658
ax.set_ylim(np.min(ys), np.max(ys))
6759

68-
# colors is sequence of rgba tuples
69-
# linestyle is a string or dash tuple. Legal string values are
70-
# solid|dashed|dashdot|dotted. The dash tuple is (offset, onoffseq)
71-
# where onoffseq is an even length tuple of on and off ink in points.
72-
# If linestyle is omitted, 'solid' is used
73-
# See `matplotlib.collections.LineCollection` for more information
74-
75-
# Make a sequence of (x, y) pairs.
76-
line_segments = LineCollection([np.column_stack([x, y]) for y in ys],
60+
line_segments = LineCollection(segs, array=x,
7761
linewidths=(0.5, 1, 1.5, 2),
7862
linestyles='solid')
79-
line_segments.set_array(x)
8063
ax.add_collection(line_segments)
8164
axcb = fig.colorbar(line_segments)
8265
axcb.set_label('Line Number')

0 commit comments

Comments
 (0)
0