Skip to content

Commit c669811

Browse files
committed
add hist2d
1 parent bbe1053 commit c669811

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed

boilerplate.py

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def %(func)s(%(argspec)s):
7171
'fill_betweenx',
7272
'hexbin',
7373
'hist',
74+
'hist2d',
7475
'hlines',
7576
'imshow',
7677
'loglog',
@@ -120,6 +121,7 @@ def %(func)s(%(argspec)s):
120121
'scatter' : 'sci(%(ret)s)',
121122
'pcolor' : 'sci(%(ret)s)',
122123
'pcolormesh': 'sci(%(ret)s)',
124+
'hist2d' : 'sci(%(ret)s[-1])',
123125
'imshow' : 'sci(%(ret)s)',
124126
#'spy' : 'sci(%(ret)s)', ### may return image or Line2D
125127
'quiver' : 'sci(%(ret)s)',
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pylab import *
2+
x = randn(1000)
3+
y = randn(1000)+5
4+
5+
#normal distribution center at x=0 and y=5
6+
hist2d(x,y,bins=40)
7+
show()

lib/matplotlib/axes.py

+60
Original file line numberDiff line numberDiff line change
@@ -7931,6 +7931,66 @@ def hist(x, bins=10, range=None, normed=False, weights=None,
79317931
else:
79327932
return n, bins, cbook.silent_list('Lists of Patches', patches)
79337933

7934+
7935+
@docstring.dedent_interpd
7936+
def hist2d(self, x, y, bins = 10, range=None, weights=None, cmin=None, cmax=None, **kwargs):
7937+
"""
7938+
Call signature::
7939+
7940+
hist2d(x, y, bins = None, range=None, weights=None, cmin=None, cmax=None **kwargs)
7941+
Make a 2d histogram plot of *x* versus *y*, where *x*,
7942+
*y* are 1-D sequences of the same length
7943+
7944+
The return value is (counts,xedges,yedges,im)
7945+
7946+
Optional keyword arguments:
7947+
*bins*: [None | int | [int, int] | array_like | [array, array]]
7948+
The bin specification:
7949+
If int, the number of bins for the two dimensions (nx=ny=bins).
7950+
If [int, int], the number of bins in each dimension (nx, ny = bins).
7951+
If array_like, the bin edges for the two dimensions (x_edges=y_edges=bins).
7952+
If [array, array], the bin edges in each dimension (x_edges, y_edges = bins).
7953+
The default value is 10.
7954+
7955+
*range*: [*None* | array_like shape(2,2)]
7956+
The leftmost and rightmost edges of the bins along each dimension (if not specified
7957+
explicitly in the bins parameters): [[xmin, xmax], [ymin, ymax]]. All values outside of
7958+
this range will be considered outliers and not tallied in the histogram.
7959+
7960+
*weights*: [*None* | array]
7961+
An array of values w_i weighing each sample (x_i, y_i).
7962+
7963+
*cmin* : [None| scalar]
7964+
All bins that has count less than cmin will not be displayed
7965+
and these count values in the return value count histogram will also be set to nan upon return
7966+
7967+
*cmax* : [None| scalar]
7968+
All bins that has count more than cmax will not be displayed (set to none before passing to imshow)
7969+
and these count values in the return value count histogram will also be set to nan upon return
7970+
7971+
Remaining keyword arguments are passed directly to :meth:imshow
7972+
7973+
**Example:**
7974+
7975+
.. plot:: mpl_examples/pylab_examples/hist2d_demo.py
7976+
"""
7977+
7978+
# xrange becomes range after 2to3
7979+
bin_range = range
7980+
range = __builtins__["range"]
7981+
h,xedges,yedges = np.histogram2d(x, y, bins=bins, range=bin_range, normed=False, weights=weights)
7982+
7983+
if 'origin' not in kwargs: kwargs['origin']='lower'
7984+
if 'extent' not in kwargs: kwargs['extent']=[xedges[0], xedges[-1], yedges[0], yedges[-1]]
7985+
if 'interpolation' not in kwargs: kwargs['interpolation']='nearest'
7986+
if 'aspect' not in kwargs: kwargs['aspect']='auto'
7987+
if cmin is not None: h[h<cmin]=None
7988+
if cmax is not None: h[h>cmax]=None
7989+
7990+
im = self.imshow(h.T,**kwargs)
7991+
7992+
return h,xedges,yedges,im
7993+
79347994
@docstring.dedent_interpd
79357995
def psd(self, x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
79367996
window=mlab.window_hanning, noverlap=0, pad_to=None,

lib/matplotlib/pyplot.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,7 @@ def plotting():
15321532
gci get the current image, or None
15331533
getp get a graphics property
15341534
hist make a histogram
1535+
hist2d make a 2d histogram
15351536
hold set the hold state on current axes
15361537
legend add a legend to the axes
15371538
loglog a log log plot
@@ -1588,7 +1589,7 @@ def plotting():
15881589
def get_plot_commands(): return ( 'axes', 'axis', 'bar', 'boxplot', 'cla', 'clf',
15891590
'close', 'colorbar', 'cohere', 'csd', 'draw', 'errorbar',
15901591
'figlegend', 'figtext', 'figimage', 'figure', 'fill', 'gca',
1591-
'gcf', 'gci', 'get', 'gray', 'barh', 'jet', 'hist', 'hold', 'imread', 'imsave',
1592+
'gcf', 'gci', 'get', 'gray', 'barh', 'jet', 'hist', 'hist2d', 'hold', 'imread', 'imsave',
15921593
'imshow', 'legend', 'loglog', 'quiver', 'rc', 'pcolor', 'pcolormesh', 'plot', 'psd',
15931594
'savefig', 'scatter', 'set', 'semilogx', 'semilogy', 'show',
15941595
'specgram', 'stem', 'subplot', 'table', 'text', 'title', 'xlabel',
@@ -1937,17 +1938,16 @@ def getname_val(identifier):
19371938

19381939
draw_if_interactive()
19391940

1940-
19411941
def autogen_docstring(base):
19421942
"""Autogenerated wrappers will get their docstring from a base function
19431943
with an addendum."""
19441944
msg = "\n\nAdditional kwargs: hold = [True|False] overrides default hold state"
19451945
addendum = docstring.Appender(msg, '\n\n')
19461946
return lambda func: addendum(docstring.copy_dedent(base)(func))
19471947

1948-
19491948
# This function cannot be generated by boilerplate.py because it may
19501949
# return an image or a line.
1950+
19511951
@autogen_docstring(Axes.spy)
19521952
def spy(Z, precision=0, marker=None, markersize=None, aspect='equal', hold=None, **kwargs):
19531953
ax = gca()
@@ -1965,9 +1965,6 @@ def spy(Z, precision=0, marker=None, markersize=None, aspect='equal', hold=None,
19651965
sci(ret)
19661966
return ret
19671967

1968-
1969-
## Plotting part 2: autogenerated wrappers for axes methods ##
1970-
19711968
# This function was autogenerated by boilerplate.py. Do not edit as
19721969
# changes will be lost
19731970
@autogen_docstring(Axes.acorr)
@@ -2079,7 +2076,7 @@ def axvspan(xmin, xmax, ymin=0, ymax=1, hold=None, **kwargs):
20792076
# This function was autogenerated by boilerplate.py. Do not edit as
20802077
# changes will be lost
20812078
@autogen_docstring(Axes.bar)
2082-
def bar(left, height, width=0.80000000000000004, bottom=None, hold=None, **kwargs):
2079+
def bar(left, height, width=0.8, bottom=None, hold=None, **kwargs):
20832080
ax = gca()
20842081
# allow callers to override the hold state by passing hold=True|False
20852082
washold = ax.ishold()
@@ -2097,7 +2094,7 @@ def bar(left, height, width=0.80000000000000004, bottom=None, hold=None, **kwarg
20972094
# This function was autogenerated by boilerplate.py. Do not edit as
20982095
# changes will be lost
20992096
@autogen_docstring(Axes.barh)
2100-
def barh(bottom, width, height=0.80000000000000004, left=None, hold=None, **kwargs):
2097+
def barh(bottom, width, height=0.8, left=None, hold=None, **kwargs):
21012098
ax = gca()
21022099
# allow callers to override the hold state by passing hold=True|False
21032100
washold = ax.ishold()
@@ -2346,6 +2343,24 @@ def hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False, b
23462343

23472344
return ret
23482345

2346+
# This function was autogenerated by boilerplate.py. Do not edit as
2347+
# changes will be lost
2348+
@autogen_docstring(Axes.hist2d)
2349+
def hist2d(x, y, bins=10, range=None, weights=None, cmin=None, cmax=None, hold=None, **kwargs):
2350+
ax = gca()
2351+
# allow callers to override the hold state by passing hold=True|False
2352+
washold = ax.ishold()
2353+
2354+
if hold is not None:
2355+
ax.hold(hold)
2356+
try:
2357+
ret = ax.hist2d(x, y, bins, range, weights, cmin, cmax, **kwargs)
2358+
draw_if_interactive()
2359+
finally:
2360+
ax.hold(washold)
2361+
sci(ret[-1])
2362+
return ret
2363+
23492364
# This function was autogenerated by boilerplate.py. Do not edit as
23502365
# changes will be lost
23512366
@autogen_docstring(Axes.hlines)
@@ -2439,7 +2454,7 @@ def pcolormesh(*args, **kwargs):
24392454
# This function was autogenerated by boilerplate.py. Do not edit as
24402455
# changes will be lost
24412456
@autogen_docstring(Axes.pie)
2442-
def pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.59999999999999998, shadow=False, labeldistance=1.1000000000000001, hold=None):
2457+
def pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, hold=None):
24432458
ax = gca()
24442459
# allow callers to override the hold state by passing hold=True|False
24452460
washold = ax.ishold()
@@ -2655,7 +2670,7 @@ def step(x, y, *args, **kwargs):
26552670
# This function was autogenerated by boilerplate.py. Do not edit as
26562671
# changes will be lost
26572672
@autogen_docstring(Axes.streamplot)
2658-
def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None, arrowsize=1, arrowstyle='-|>', minlength=0.10000000000000001, hold=None):
2673+
def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None, arrowsize=1, arrowstyle='-|>', minlength=0.1, hold=None):
26592674
ax = gca()
26602675
# allow callers to override the hold state by passing hold=True|False
26612676
washold = ax.ishold()

0 commit comments

Comments
 (0)