Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ee61ee4

Browse filesBrowse files
authored
Merge pull request #7691 from phobson/optional-3d-bar-shading
[MRG+1] ENH: Optional 3d bar shading
2 parents d278160 + 8e535e6 commit ee61ee4
Copy full SHA for ee61ee4

File tree

Expand file treeCollapse file tree

6 files changed

+150
-33
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+150
-33
lines changed
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Users can now toggle shading in 3D bar plots
2+
--------------------------------------------
3+
4+
A new ``shade`` parameter has been added the 3D bar plotting method.
5+
The default behavior remains to shade the bars, but now users
6+
have the option of setting ``shade`` to ``False``.
7+
8+
9+
Example
10+
```````
11+
::
12+
13+
import numpy as np
14+
import matplotlib.pyplot as plt
15+
from mpl_toolkits.mplot3d import Axes3D
16+
17+
fig = plt.figure(figsize=(7,3))
18+
ax1 = fig.add_subplot(121, projection='3d')
19+
x = np.arange(2)
20+
y = np.arange(3)
21+
x2d, y2d = np.meshgrid(x, y)
22+
x2d, y2d = x2d.ravel(), y2d.ravel()
23+
z = x2d + y2d
24+
ax1.bar3d(x2d, y2d, x2d * 0, 1, 1, z, shade=True)
25+
26+
ax2 = fig.add_subplot(122, projection='3d')
27+
ax2.bar3d(x2d, y2d, x2d * 0, 1, 1, z, shade=False)
28+
fig.canvas.draw()

‎examples/mplot3d/plot_3d_bars.py

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
=====================
3+
Demo of 3D bar charts
4+
=====================
5+
6+
A basic demo of how to plot 3D bars with and without
7+
shading.
8+
9+
"""
10+
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
from mpl_toolkits.mplot3d import Axes3D
14+
15+
16+
# setup the figure and axes
17+
fig = plt.figure(figsize=(8, 3))
18+
ax1 = fig.add_subplot(121, projection='3d')
19+
ax2 = fig.add_subplot(122, projection='3d')
20+
21+
# fake data
22+
_x = np.arange(4)
23+
_y = np.arange(5)
24+
_xx, _yy = np.meshgrid(_x, _y)
25+
x, y = _xx.ravel(), _yy.ravel()
26+
27+
top = x + y
28+
bottom = np.zeros_like(top)
29+
width = depth = 1
30+
31+
ax1.bar3d(x, y, bottom, width, depth, top, shade=True)
32+
ax1.set_title('Shaded')
33+
34+
ax2.bar3d(x, y, bottom, width, depth, top, shade=False)
35+
ax2.set_title('Not Shaded')
36+
37+
plt.show()

‎lib/mpl_toolkits/mplot3d/axes3d.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/mplot3d/axes3d.py
+61-31Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,39 +2409,65 @@ def bar(self, left, height, zs=0, zdir='z', *args, **kwargs):
24092409
return patches
24102410

24112411
def bar3d(self, x, y, z, dx, dy, dz, color=None,
2412-
zsort='average', *args, **kwargs):
2413-
'''
2414-
Generate a 3D bar, or multiple bars.
2415-
2416-
When generating multiple bars, x, y, z have to be arrays.
2417-
dx, dy, dz can be arrays or scalars.
2418-
2419-
*color* can be:
2420-
2421-
- A single color value, to color all bars the same color.
2422-
2423-
- An array of colors of length N bars, to color each bar
2424-
independently.
2425-
2426-
- An array of colors of length 6, to color the faces of the
2427-
bars similarly.
2412+
zsort='average', shade=True, *args, **kwargs):
2413+
"""Generate a 3D barplot.
2414+
2415+
This method creates three dimensional barplot where the width,
2416+
depth, height, and color of the bars can all be uniquely set.
2417+
2418+
Parameters
2419+
----------
2420+
x, y, z : array-like
2421+
The coordinates of the anchor point of the bars.
2422+
2423+
dx, dy, dz : scalar or array-like
2424+
The width, depth, and height of the bars, respectively.
2425+
2426+
color : sequence of valid color specifications, optional
2427+
The color of the bars can be specified globally or
2428+
individually. This parameter can be:
2429+
2430+
- A single color value, to color all bars the same color.
2431+
- An array of colors of length N bars, to color each bar
2432+
independently.
2433+
- An array of colors of length 6, to color the faces of the
2434+
bars similarly.
2435+
- An array of colors of length 6 * N bars, to color each face
2436+
independently.
2437+
2438+
When coloring the faces of the boxes specifically, this is
2439+
the order of the coloring:
2440+
2441+
1. -Z (bottom of box)
2442+
2. +Z (top of box)
2443+
3. -Y
2444+
4. +Y
2445+
5. -X
2446+
6. +X
2447+
2448+
zsort : str, optional
2449+
The z-axis sorting scheme passed onto
2450+
:func:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
2451+
2452+
shade : bool, optional (default = True)
2453+
When true, this shades the dark sides of the bars (relative
2454+
to the plot's source of light).
2455+
2456+
Any additional keyword arguments are passed onto
2457+
:func:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
24282458
2429-
- An array of colors of length 6 * N bars, to color each face
2430-
independently.
2459+
Returns
2460+
-------
2461+
collection : Poly3DCollection
2462+
A collection of three dimensional polygons representing
2463+
the bars.
24312464
2432-
When coloring the faces of the boxes specifically, this is
2433-
the order of the coloring:
2465+
Examples
2466+
--------
2467+
.. plot:: mpl_examples/mplot3d/plot_3d_bars.py
24342468
2435-
1. -Z (bottom of box)
2436-
2. +Z (top of box)
2437-
3. -Y
2438-
4. +Y
2439-
5. -X
2440-
6. +X
2469+
"""
24412470

2442-
Keyword arguments are passed onto
2443-
:func:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
2444-
'''
24452471
had_data = self.has_data()
24462472

24472473
if not cbook.iterable(x):
@@ -2512,8 +2538,12 @@ def bar3d(self, x, y, z, dx, dy, dz, color=None,
25122538
if len(facecolors) < len(x):
25132539
facecolors *= (6 * len(x))
25142540

2515-
normals = self._generate_normals(polys)
2516-
sfacecolors = self._shade_colors(facecolors, normals)
2541+
if shade:
2542+
normals = self._generate_normals(polys)
2543+
sfacecolors = self._shade_colors(facecolors, normals)
2544+
else:
2545+
sfacecolors = facecolors
2546+
25172547
col = art3d.Poly3DCollection(polys,
25182548
zsort=zsort,
25192549
facecolor=sfacecolors,
Loading
Loading

‎lib/mpl_toolkits/tests/test_mplot3d.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/tests/test_mplot3d.py
+24-2Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,37 @@ def test_bar3d():
2121
ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
2222

2323

24-
def test_bar3d_dflt_smoke():
24+
@image_comparison(
25+
baseline_images=['bar3d_shaded'],
26+
remove_text=True,
27+
extensions=['png']
28+
)
29+
def test_bar3d_shaded():
2530
fig = plt.figure()
2631
ax = fig.add_subplot(111, projection='3d')
2732
x = np.arange(4)
2833
y = np.arange(5)
2934
x2d, y2d = np.meshgrid(x, y)
3035
x2d, y2d = x2d.ravel(), y2d.ravel()
3136
z = x2d + y2d
32-
ax.bar3d(x2d, y2d, x2d * 0, 1, 1, z)
37+
ax.bar3d(x2d, y2d, x2d * 0, 1, 1, z, shade=True)
38+
fig.canvas.draw()
39+
40+
41+
@image_comparison(
42+
baseline_images=['bar3d_notshaded'],
43+
remove_text=True,
44+
extensions=['png']
45+
)
46+
def test_bar3d_notshaded():
47+
fig = plt.figure()
48+
ax = fig.add_subplot(111, projection='3d')
49+
x = np.arange(4)
50+
y = np.arange(5)
51+
x2d, y2d = np.meshgrid(x, y)
52+
x2d, y2d = x2d.ravel(), y2d.ravel()
53+
z = x2d + y2d
54+
ax.bar3d(x2d, y2d, x2d * 0, 1, 1, z, shade=False)
3355
fig.canvas.draw()
3456

3557

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.