8000 Merge pull request #7644 from dstansby/bar-error-message · matplotlib/matplotlib@9187e3f · GitHub
[go: up one dir, main page]

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 9187e3f

Browse files
authored
Merge pull request #7644 from dstansby/bar-error-message
ENH: Allow scalar height for plt.bar
2 parents a495675 + 70afe12 commit 9187e3f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,8 +1876,8 @@ def bar(self, left, height, width=0.8, bottom=None, **kwargs):
18761876
left : sequence of scalars
18771877
the x coordinates of the left sides of the bars
18781878
1879-
height : sequence of scalars
1880-
the heights of the bars
1879+
height : scalar or sequence of scalars
1880+
the height(s) of the bars
18811881
18821882
width : scalar or array-like, optional
18831883
the width(s) of the bars
@@ -2025,8 +2025,6 @@ def make_iterable(x):
20252025
bottom = [0]
20262026

20272027
nbars = len(left)
2028-
if len(width) == 1:
2029-
width *= nbars
20302028
if len(bottom) == 1:
20312029
bottom *= nbars
20322030

@@ -2045,14 +2043,16 @@ def make_iterable(x):
20452043
nbars = len(bottom)
20462044
if len(left) == 1:
20472045
left *= nbars
2048-
if len(height) == 1:
2049-
height *= nbars
20502046

20512047
tick_label_axis = self.yaxis
20522048
tick_label_position = bottom
20532049
else:
20542050
raise ValueError('invalid orientation: %s' % orientation)
20552051

2052+
if len(height) == 1:
2053+
height *= nbars
2054+
if len(width) == 1:
2055+
width *= nbars
20562056
if len(linewidth) < nbars:
20572057
linewidth *= nbars
20582058

@@ -2077,7 +2077,7 @@ def make_iterable(x):
20772077
"be length %d or scalar" % nbars)
20782078
if len(height) != nbars:
20792079
raise ValueError("incompatible sizes: argument 'height' "
2080-
"must be length %d or scalar" % nbars)
2080+
"must be length %d or scalar" % nbars)
20812081
if len(width) != nbars:
20822082
raise ValueError("incompatible sizes: argument 'width' "
20832083
"must be length %d or scalar" % nbars)

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,3 +4877,12 @@ def test_scatter_color_masking():
48774877
def test_eventplot_legend():
48784878
plt.eventplot([1.0], label='Label')
48794879
plt.legend()
4880+
4881+
4882+
@cleanup
4883+
def test_bar_single_height():
4884+
fig, ax = plt.subplots()
4885+
# Check that a bar chart with a single height for all bars works
4886+
ax.bar(range(4), 1)
4887+
# Check that a horizontal chart with one width works
4888+
ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal')

0 commit comments

Comments
 (0)
0