Skip to content

Commit 2db763f

Browse files
committed
added hist functions to DataFrame and Series, added use_index option to {DataFrame, Series}.plot
1 parent 4906ee7 commit 2db763f

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

pandas/core/frame.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ def _join_index(self, other, how):
17971797
return self._constructor(result_series, index=join_index)
17981798

17991799
def plot(self, kind='line', subplots=False, sharex=True, sharey=False,
1800-
**kwds): # pragma: no cover
1800+
use_index=True, **kwds): # pragma: no cover
18011801
"""
18021802
Plot the DataFrame's series with the index on the x-axis using
18031803
matplotlib / pylab.
@@ -1823,15 +1823,40 @@ def plot(self, kind='line', subplots=False, sharex=True, sharey=False,
18231823
fig = plt.figure()
18241824
ax = fig.add_subplot(111)
18251825

1826+
if use_index:
1827+
x = self.index
1828+
else:
1829+
x = range(len(self))
1830+
18261831
for i, col in enumerate(_try_sort(self.columns)):
18271832
if subplots:
18281833
ax = axes[i]
1829-
ax.plot(self.index, self[col].values, 'k', label=col,
1830-
**kwds)
1834+
ax.plot(x, self[col].values, 'k', label=col, **kwds)
18311835
ax.legend(loc='best')
18321836
else:
1833-
ax.plot(self.index, self[col].values, label=col,
1834-
**kwds)
1837+
ax.plot(x, self[col].values, label=col, **kwds)
1838+
1839+
def hist(self): # pragma: no cover
1840+
"""
1841+
Draw Histogram the DataFrame's series using matplotlib / pylab.
1842+
1843+
Parameters
1844+
----------
1845+
kwds : other plotting keyword arguments
1846+
1847+
"""
1848+
import matplotlib.pyplot as plt
1849+
1850+
n = len(self.columns)
1851+
k = 1
1852+
while k**2 < n:
1853+
k += 1
1854+
_, axes = plt.subplots(nrows=k, ncols=k)
1855+
1856+
for i, col in enumerate(_try_sort(self.columns)):
1857+
ax = axes[i / k][i % k]
1858+
ax.hist(self[col].values)
1859+
ax.set_title(col)
18351860

18361861
def _get_agg_axis(self, axis_num):
18371862
if axis_num == 0:

pandas/core/series.py

+36-8
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,8 @@ def fillna(self, value=None, method='pad'):
900900
#-------------------------------------------------------------------------------
901901
# Miscellaneous
902902

903-
def plot(self, label=None, kind='line', rot=30, axes=None, style='-',
904-
**kwds): # pragma: no cover
903+
def plot(self, label=None, kind='line', use_index=True, rot=30, ax=None,
904+
style='-', **kwds): # pragma: no cover
905905
"""
906906
Plot the input series with the index on the x-axis using
907907
matplotlib / pylab.
@@ -911,6 +911,7 @@ def plot(self, label=None, kind='line', rot=30, axes=None, style='-',
911911
label : label argument to provide to plot
912912
kind : {'line', 'bar', 'hist'}
913913
Default: line for TimeSeries, hist for Series
914+
auto_x : if True, it will use range(len(self)) as x-axis
914915
kwds : other plotting keyword arguments
915916
916917
Notes
@@ -929,25 +930,52 @@ def plot(self, label=None, kind='line', rot=30, axes=None, style='-',
929930

930931
N = len(self)
931932

932-
if axes is None:
933-
axes = plt.gca()
933+
if ax is None:
934+
ax = plt.gca()
934935

935936
if kind == 'line':
936-
axes.plot(self.index, self.values, style, **kwds)
937+
if use_index:
938+
x = self.index
939+
else:
940+
x = range(len(self))
941+
942+
ax.plot(x, self.values, style, **kwds)
937943
elif kind == 'bar':
938944
xinds = np.arange(N) + 0.25
939-
axes.bar(xinds, self.values, 0.5, bottom=np.zeros(N), linewidth=1)
945+
ax.bar(xinds, self.values, 0.5, bottom=np.zeros(N), linewidth=1)
940946

941947
if N < 10:
942948
fontsize = 12
943949
else:
944950
fontsize = 10
945951

946-
axes.set_xticks(xinds + 0.25)
947-
axes.set_xticklabels(self.index, rotation=rot, fontsize=fontsize)
952+
ax.set_xticks(xinds + 0.25)
953+
ax.set_xticklabels(self.index, rotation=rot, fontsize=fontsize)
948954

949955
plt.draw_if_interactive()
950956

957+
def hist(self, ax=None): # pragma: no cover
958+
"""
959+
Draw histogram of the input series using matplotlib / pylab.
960+
961+
Parameters
962+
----------
963+
964+
Notes
965+
-----
966+
See matplotlib documentation online for more on this subject
967+
968+
Default plot-types: TimeSeries (line), Series (bar)
969+
970+
Intended to be used in ipython -pylab mode
971+
"""
972+
import matplotlib.pyplot as plt
973+
974+
if ax is None:
975+
ax = plt.gca()
976+
977+
ax.hist(self.values)
978+
951979
def toCSV(self, path):
952980
"""
953981
Write the Series to a CSV file

0 commit comments

Comments
 (0)