Skip to content

Commit d3bcab7

Browse files
author
Chang She
committed
ENH: label sizes and rotations for histogram
TST: test cases for both Series and DataFrame histogram
1 parent 34e8a84 commit d3bcab7

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

pandas/tests/test_graphics.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ def test_hist(self):
142142
df = DataFrame(np.random.randn(100, 6))
143143
_check_plot_works(df.hist)
144144

145+
#make sure kwargs are handled
146+
ser = df[0]
147+
xf, yf = 20, 20
148+
xrot, yrot = 30, 30
149+
ax = ser.hist(xlabelsize=xf, xrot=30, ylabelsize=yf, yrot=30)
150+
ytick = ax.get_yticklabels()[0]
151+
xtick = ax.get_xticklabels()[0]
152+
self.assertAlmostEqual(ytick.get_fontsize(), yf)
153+
self.assertAlmostEqual(ytick.get_rotation(), yrot)
154+
self.assertAlmostEqual(xtick.get_fontsize(), xf)
155+
self.assertAlmostEqual(xtick.get_rotation(), xrot)
156+
157+
xf, yf = 20, 20
158+
xrot, yrot = 30, 30
159+
axes = df.hist(xlabelsize=xf, xrot=30, ylabelsize=yf, yrot=30)
160+
for i, ax in enumerate(axes.ravel()):
161+
if i < len(df.columns):
162+
ytick = ax.get_yticklabels()[0]
163+
xtick = ax.get_xticklabels()[0]
164+
self.assertAlmostEqual(ytick.get_fontsize(), yf)
165+
self.assertAlmostEqual(ytick.get_rotation(), yrot)
166+
self.assertAlmostEqual(xtick.get_fontsize(), xf)
167+
self.assertAlmostEqual(xtick.get_rotation(), xrot)
168+
145169
@slow
146170
def test_scatter(self):
147171
df = DataFrame(np.random.randn(100, 4))

pandas/tools/plotting.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,15 +702,27 @@ def plot_group(group, ax):
702702
return fig
703703

704704

705-
def hist_frame(data, grid=True, **kwds):
705+
def hist_frame(data, grid=True, xlabelsize=None, xrot=None,
706+
ylabelsize=None, yrot=None, **kwds):
706707
"""
707708
Draw Histogram the DataFrame's series using matplotlib / pylab.
708709
709710
Parameters
710711
----------
712+
grid : boolean, default True
713+
Whether to show axis grid lines
714+
xlabelsize : int, default None
715+
If specified changes the x-axis label size
716+
xrot : float, default None
717+
rotation of x axis labels
718+
ylabelsize : int, default None
719+
If specified changes the y-axis label size
720+
yrot : float, default None
721+
rotation of y axis labels
711722
kwds : other plotting keyword arguments
712723
To be passed to hist function
713724
"""
725+
import matplotlib.pyplot as plt
714726
n = len(data.columns)
715727
k = 1
716728
while k ** 2 < n:
@@ -723,17 +735,36 @@ def hist_frame(data, grid=True, **kwds):
723735
ax.set_title(col)
724736
ax.grid(grid)
725737

726-
return axes
738+
if xlabelsize is not None:
739+
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
740+
if xrot is not None:
741+
plt.setp(ax.get_xticklabels(), rotation=xrot)
742+
if ylabelsize is not None:
743+
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
744+
if yrot is not None:
745+
plt.setp(ax.get_yticklabels(), rotation=yrot)
727746

747+
return axes
728748

729-
def hist_series(self, ax=None, grid=True, **kwds):
749+
def hist_series(self, ax=None, grid=True, xlabelsize=None, xrot=None,
750+
ylabelsize=None, yrot=None, **kwds):
730751
"""
731752
Draw histogram of the input series using matplotlib
732753
733754
Parameters
734755
----------
735756
ax : matplotlib axis object
736757
If not passed, uses gca()
758+
grid : boolean, default True
759+
Whether to show axis grid lines
760+
xlabelsize : int, default None
761+
If specified changes the x-axis label size
762+
xrot : float, default None
763+
rotation of x axis labels
764+
ylabelsize : int, default None
765+
If specified changes the y-axis label size
766+
yrot : float, default None
767+
rotation of y axis labels
737768
kwds : keywords
738769
To be passed to the actual plotting function
739770
@@ -752,6 +783,15 @@ def hist_series(self, ax=None, grid=True, **kwds):
752783
ax.hist(values, **kwds)
753784
ax.grid(grid)
754785

786+
if xlabelsize is not None:
787+
plt.setp(ax.get_xticklabels(), fontsize=xlabelsize)
788+
if xrot is not None:
789+
plt.setp(ax.get_xticklabels(), rotation=xrot)
790+
if ylabelsize is not None:
791+
plt.setp(ax.get_yticklabels(), fontsize=ylabelsize)
792+
if yrot is not None:
793+
plt.setp(ax.get_yticklabels(), rotation=yrot)
794+
755795
return ax
756796

757797

0 commit comments

Comments
 (0)