Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38a3d1f

Browse files
committed
Handle log scale in bar plots; improve log error reporting
svn path=/trunk/matplotlib/; revision=2947
1 parent 7c5ac98 commit 38a3d1f

4 files changed

Lines changed: 106 additions & 10 deletions

File tree

API_CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Bar, barh, and hist have "log" binary kwarg: log=True
2+
sets the ordinate to a log scale.
3+
14
Boxplot can handle a list of vectors instead of just
25
an array, so vectors can have different lengths.
36

CHANGELOG

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2006-12-28 Improved error message for nonpositive input to log
2+
transform; added log kwarg to bar, barh, and hist,
3+
and modified bar method to behave sensibly by default
4+
when the ordinate has a log scale. (This only works
5+
if the log scale is set before or by the call to bar,
6+
hence the utility of the log kwarg.) - EF
7+
18
2006-12-27 backend_cairo.py: update draw_image() and _draw_mathtext() to work
29
with numpy - SC
310

lib/matplotlib/axes.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,16 +2548,16 @@ def get_handles():
25482548
#### Specialized plotting
25492549

25502550

2551-
def bar(self, left, height, width=0.8, bottom=0,
2551+
def bar(self, left, height, width=0.8, bottom=None,
25522552
color=None, edgecolor=None, linewidth=None,
25532553
yerr=None, xerr=None, ecolor=None, capsize=3,
2554-
align='edge', orientation='vertical'
2554+
align='edge', orientation='vertical', log=False
25552555
):
25562556
"""
25572557
BAR(left, height, width=0.8, bottom=0,
25582558
color=None, edgecolor=None, linewidth=None,
25592559
yerr=None, xerr=None, ecolor=None, capsize=3,
2560-
align='edge', orientation='vertical')
2560+
align='edge', orientation='vertical', log=False)
25612561
25622562
Make a bar plot with rectangles bounded by
25632563
@@ -2596,6 +2596,9 @@ def bar(self, left, height, width=0.8, bottom=0,
25962596
25972597
orientation = 'vertical' | 'horizontal'
25982598
2599+
log = False | True - False (default) leaves the orientation
2600+
axis as-is; True sets it to log scale
2601+
25992602
For vertical bars, 'edge' aligns bars by their left edges in left,
26002603
while 'center' interprets these values as the x coordinates
26012604
of the bar centers.
@@ -2619,21 +2622,41 @@ def make_iterable(x):
26192622
return x
26202623

26212624
# make them safe to take len() of
2625+
_left = left
26222626
left = make_iterable(left)
26232627
height = make_iterable(height)
26242628
width = make_iterable(width)
2629+
_bottom = bottom
26252630
bottom = make_iterable(bottom)
26262631
linewidth = make_iterable(linewidth)
26272632

2633+
adjust_ylim = False
2634+
adjust_xlim = False
26282635
if orientation == 'vertical':
2636+
if log:
2637+
self.set_yscale('log')
26292638
# size width and bottom according to length of left
2639+
if _bottom is None:
2640+
if self.get_yscale() == 'log':
2641+
bottom = [1e-100]
2642+
adjust_ylim = True
2643+
else:
2644+
bottom = [0]
26302645
nbars = len(left)
26312646
if len(width) == 1:
26322647
width *= nbars
26332648
if len(bottom) == 1:
26342649
bottom *= nbars
26352650
elif orientation == 'horizontal':
2651+
if log:
2652+
self.set_xscale('log')
26362653
# size left and height according to length of bottom
2654+
if _left is None:
2655+
if self.get_xscale() == 'log':
2656+
left = [1e-100]
2657+
adjust_xlim = True
2658+
else:
2659+
left = [0]
26372660
nbars = len(bottom)
26382661
if len(left) == 1:
26392662
left *= nbars
@@ -2725,11 +2748,25 @@ def make_iterable(x):
27252748

27262749
self.hold(holdstate) # restore previous hold state
27272750

2751+
if adjust_xlim:
2752+
xmin, xmax = self.dataLim.intervalx().get_bounds()
2753+
xmin = amin(w)
2754+
if xerr is not None:
2755+
xmin = xmin - amax(xerr)
2756+
xmin = min(xmin, 1e-100)
2757+
self.dataLim.intervalx().set_bounds(xmin, xmax)
2758+
if adjust_ylim:
2759+
ymin, ymax = self.dataLim.intervaly().get_bounds()
2760+
ymin = amin(h)
2761+
if yerr is not None:
2762+
ymin = ymin - amax(yerr)
2763+
ymin = max(ymin, 1e-100)
2764+
self.dataLim.intervaly().set_bounds(ymin, ymax)
27282765
self.autoscale_view()
27292766
return patches
27302767

27312768

2732-
def barh(self, bottom, width, height=0.8, left=0,
2769+
def barh(self, bottom, width, height=0.8, left=None,
27332770
color=None, edgecolor=None, linewidth=None,
27342771
xerr=None, yerr=None, ecolor=None, capsize=3,
27352772
align='edge'
@@ -4229,11 +4266,13 @@ def table(self, **kwargs):
42294266
#### Data analysis
42304267

42314268

4232-
def hist(self, x, bins=10, normed=0, bottom=0,
4233-
align='edge', orientation='vertical', width=None, **kwargs):
4269+
def hist(self, x, bins=10, normed=0, bottom=None,
4270+
align='edge', orientation='vertical', width=None,
4271+
log=False, **kwargs):
42344272
"""
4235-
HIST(x, bins=10, normed=0, bottom=0,
4236-
align='edge', orientation='vertical', width=None, **kwargs)
4273+
HIST(x, bins=10, normed=0, bottom=None,
4274+
align='edge', orientation='vertical', width=None,
4275+
log=False, **kwargs)
42374276
42384277
Compute the histogram of x. bins is either an integer number of
42394278
bins or a sequence giving the bins. x are the data to be binned.
@@ -4260,6 +4299,8 @@ def hist(self, x, bins=10, normed=0, bottom=0,
42604299
width: the width of the bars. If None, automatically compute
42614300
the width.
42624301
4302+
log: if True, the histogram axis will be set to a log scale
4303+
42634304
kwargs are used to update the properties of the
42644305
hist Rectangles:
42654306
%(Rectangle)s
@@ -4268,9 +4309,11 @@ def hist(self, x, bins=10, normed=0, bottom=0,
42684309
n, bins = matplotlib.mlab.hist(x, bins, normed)
42694310
if width is None: width = 0.9*(bins[1]-bins[0])
42704311
if orientation == 'horizontal':
4271-
patches = self.barh(bins, n, height=width, left=bottom, align=align)
4312+
patches = self.barh(bins, n, height=width, left=bottom,
4313+
align=align, log=log)
42724314
elif orientation == 'vertical':
4273-
patches = self.bar(bins, n, width=width, bottom=bottom, align=align)
4315+
patches = self.bar(bins, n, width=width, bottom=bottom,
4316+
align=align, log=log)
42744317
else:
42754318
raise ValueError, 'invalid orientation: %s' % orientation
42764319
for p in patches:

src/_transforms.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ Func::map(const Py::Tuple &args) {
687687
try {
688688
xout = this->operator()(xin);
689689
}
690+
catch (const std::exception &e) {
691+
throw Py::ValueError(e.what());
692+
}
690693
catch(...) {
691694
throw Py::ValueError("Domain error on Func::map");
692695
}
@@ -719,6 +722,9 @@ FuncXY::map(const Py::Tuple &args) {
719722
try {
720723
xy = this->operator()(xin, yin);
721724
}
725+
catch (const std::exception &e) {
726+
throw Py::ValueError(e.what());
727+
}
722728
catch(...) {
723729
throw Py::ValueError("Domain error on FuncXY nonlinear transform");
724730
}
@@ -881,6 +887,10 @@ Transformation::inverse_xy_tup(const Py::Tuple & args) {
881887
try {
882888
if (!_frozen) eval_scalars();
883889
}
890+
catch (const std::exception &e) {
891+
throw Py::ValueError(e.what());
892+
}
893+
884894
catch(...) {
885895
throw Py::ValueError("Domain error on inverse_xy_tup");
886896
}
@@ -919,6 +929,11 @@ Transformation::inverse_numerix_xy(const Py::Tuple & args) {
919929
try {
920930
if (!_frozen) eval_scalars();
921931
}
932+
catch (const std::exception &e) {
933+
Py_XDECREF(xyin);
934+
throw Py::ValueError(e.what());
935+
}
936+
922937
catch(...) {
923938
Py_XDECREF(xyin);
924939
throw Py::ValueError("Domain error on Transformation::inverse_numerix_xy");
@@ -968,6 +983,9 @@ Transformation::xy_tup(const Py::Tuple & args) {
968983
try {
969984
if (!_frozen) eval_scalars();
970985
}
986+
catch (const std::exception &e) {
987+
throw Py::ValueError(e.what());
988+
}
971989
catch(...) {
972990
throw Py::ValueError("Domain error on nonlinear transform");
973991
}
@@ -982,6 +1000,9 @@ Transformation::xy_tup(const Py::Tuple & args) {
9821000
try {
9831001
this->operator()(x, y);
9841002
}
1003+
catch (const std::exception &e) {
1004+
throw Py::ValueError(e.what());
1005+
}
9851006
catch(...) {
9861007
throw Py::ValueError("Domain error on nTransformation::xy_tup operator()(x,y)");
9871008
}
@@ -1010,6 +1031,9 @@ Transformation::seq_x_y(const Py::Tuple & args) {
10101031
try {
10111032
if (!_frozen) eval_scalars();
10121033
}
1034+
catch (const std::exception &e) {
1035+
throw Py::ValueError(e.what());
1036+
}
10131037
catch(...) {
10141038
throw Py::ValueError("Domain error on Transformation::seq_x_y");
10151039
}
@@ -1025,6 +1049,9 @@ Transformation::seq_x_y(const Py::Tuple & args) {
10251049
try {
10261050
this->operator()(thisx, thisy);
10271051
}
1052+
catch (const std::exception &e) {
1053+
throw Py::ValueError(e.what());
1054+
}
10281055
catch(...) {
10291056
throw Py::ValueError("Domain error on Transformation::seq_x_y operator()(thisx, thisy)");
10301057
}
@@ -1064,6 +1091,10 @@ Transformation::numerix_xy(const Py::Tuple & args) {
10641091
try {
10651092
if (!_frozen) eval_scalars();
10661093
}
1094+
catch (const std::exception &e) {
1095+
Py_XDECREF(xyin);
1096+
throw Py::ValueError(e.what());
1097+
}
10671098
catch(...) {
10681099
Py_XDECREF(xyin);
10691100
throw Py::ValueError("Domain error on Transformation::numerix_xy");
@@ -1134,6 +1165,9 @@ Transformation::numerix_x_y(const Py::Tuple & args) {
11341165
try {
11351166
if (!_frozen) eval_scalars();
11361167
}
1168+
catch (const std::exception &e) {
1169+
throw Py::ValueError(e.what());
1170+
}
11371171
catch(...) {
11381172
throw Py::ValueError("Domain error on Transformation::numerix_x_y");
11391173
}
@@ -1337,6 +1371,9 @@ Transformation::seq_xy_tups(const Py::Tuple & args) {
13371371
try {
13381372
if (!_frozen) eval_scalars();
13391373
}
1374+
catch (const std::exception &e) {
1375+
throw Py::ValueError(e.what());
1376+
}
13401377
catch(...) {
13411378
throw Py::ValueError("Domain error on Transformation::seq_xy_tups");
13421379
}
@@ -1355,6 +1392,9 @@ Transformation::seq_xy_tups(const Py::Tuple & args) {
13551392
try {
13561393
this->operator()(thisx, thisy);
13571394
}
1395+
catch (const std::exception &e) {
1396+
throw Py::ValueError(e.what());
1397+
}
13581398
catch(...) {
13591399
throw Py::ValueError("Domain error on nonlinear Transformation::seq_xy_tups operator()(thisx, thisy)");
13601400
}
@@ -1976,6 +2016,9 @@ Affine::deepcopy(const Py::Tuple &args) {
19762016
try {
19772017
eval_scalars();
19782018
}
2019+
catch (const std::exception &e) {
2020+
throw Py::ValueError(e.what());
2021+
}
19792022
catch(...) {
19802023
throw Py::ValueError("Domain error on Affine deepcopy");
19812024
}

0 commit comments

Comments
 (0)