/usr/share/pyshared/pandas/tools/plotting.py is in python-pandas 0.7.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | import numpy as np
def scatter_matrix(data):
pass
def _gca():
import matplotlib.pyplot as plt
return plt.gca()
def _gcf():
import matplotlib.pyplot as plt
return plt.gcf()
def hist(data, column, by=None, ax=None, fontsize=None):
keys, values = zip(*data.groupby(by)[column])
if ax is None:
ax = _gca()
ax.boxplot(values)
ax.set_xticklabels(keys, rotation=0, fontsize=fontsize)
return ax
def grouped_hist(data, column, by=None, ax=None, bins=50, log=False,
figsize=None):
"""
Returns
-------
fig : matplotlib.Figure
"""
def plot_group(group, ax):
ax.hist(group[column].dropna(), bins=bins)
fig = _grouped_plot(plot_group, data, by=by, sharex=False,
sharey=False, figsize=figsize)
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9,
hspace=0.3, wspace=0.2)
return fig
def boxplot(data, column=None, by=None, ax=None, fontsize=None,
rot=0, grid=True, figsize=None):
"""
Make a box plot from DataFrame column optionally grouped by some columns or
other inputs
Parameters
----------
data : DataFrame
column : column name or list of names, or vector
Can be any valid input to groupby
by : string or sequence
Column in the DataFrame to group by
fontsize : int or string
Returns
-------
ax : matplotlib.axes.AxesSubplot
"""
def plot_group(grouped, ax):
keys, values = zip(*grouped)
keys = [_stringify(x) for x in keys]
ax.boxplot(values)
ax.set_xticklabels(keys, rotation=rot, fontsize=fontsize)
if column == None:
columns = None
else:
if isinstance(column, (list, tuple)):
columns = column
else:
columns = [column]
if by is not None:
if not isinstance(by, (list, tuple)):
by = [by]
fig, axes = _grouped_plot_by_column(plot_group, data, columns=columns,
by=by, grid=grid, figsize=figsize)
ax = axes
else:
if ax is None:
ax = _gca()
fig = ax.get_figure()
data = data._get_numeric_data()
if columns:
cols = columns
else:
cols = data.columns
keys = [_stringify(x) for x in cols]
ax.boxplot(list(data[cols].values.T))
ax.set_xticklabels(keys, rotation=rot, fontsize=fontsize)
ax.grid(grid)
fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
return ax
def _stringify(x):
if isinstance(x, tuple):
return '|'.join(str(y) for y in x)
else:
return str(x)
def format_date_labels(ax):
# mini version of autofmt_xdate
try:
for label in ax.get_xticklabels():
label.set_ha('right')
label.set_rotation(30)
fig = ax.get_figure()
fig.subplots_adjust(bottom=0.2)
except Exception: # pragma: no cover
pass
def scatter_plot(data, x, y, by=None, ax=None, figsize=None):
"""
Returns
-------
fig : matplotlib.Figure
"""
import matplotlib.pyplot as plt
def plot_group(group, ax):
xvals = group[x].values
yvals = group[y].values
ax.scatter(xvals, yvals)
if by is not None:
fig = _grouped_plot(plot_group, data, by=by, figsize=figsize)
else:
fig = plt.figure()
ax = fig.add_subplot(111)
plot_group(data, ax)
ax.set_ylabel(str(y))
ax.set_xlabel(str(x))
return fig
def _grouped_plot(plotf, data, by=None, numeric_only=True, figsize=None,
sharex=True, sharey=True):
import matplotlib.pyplot as plt
# allow to specify mpl default with 'default'
if not (isinstance(figsize, str) and figsize == 'default'):
figsize = (10, 5) # our default
grouped = data.groupby(by)
ngroups = len(grouped)
nrows, ncols = _get_layout(ngroups)
if figsize is None:
# our favorite default beating matplotlib's idea of the
# default size
figsize = (10, 5)
fig, axes = subplots(nrows=nrows, ncols=ncols, figsize=figsize,
sharex=sharex, sharey=sharey)
ravel_axes = []
for row in axes:
ravel_axes.extend(row)
for i, (key, group) in enumerate(grouped):
ax = ravel_axes[i]
if numeric_only:
group = group._get_numeric_data()
plotf(group, ax)
ax.set_title(str(key))
return fig, axes
def _grouped_plot_by_column(plotf, data, columns=None, by=None,
numeric_only=True, grid=False,
figsize=None):
import matplotlib.pyplot as plt
grouped = data.groupby(by)
if columns is None:
columns = data._get_numeric_data().columns - by
ngroups = len(columns)
nrows, ncols = _get_layout(ngroups)
fig, axes = subplots(nrows=nrows, ncols=ncols,
sharex=True, sharey=True,
figsize=figsize)
if isinstance(axes, plt.Axes):
ravel_axes = [axes]
else:
ravel_axes = []
for row in axes:
if isinstance(row, plt.Axes):
ravel_axes.append(row)
else:
ravel_axes.extend(row)
for i, col in enumerate(columns):
ax = ravel_axes[i]
gp_col = grouped[col]
plotf(gp_col, ax)
ax.set_title(col)
ax.set_xlabel(str(by))
ax.grid(grid)
byline = by[0] if len(by) == 1 else by
fig.suptitle('Boxplot grouped by %s' % byline)
return fig, axes
def _get_layout(nplots):
if nplots == 1:
return (1, 1)
elif nplots == 2:
return (1, 2)
elif nplots < 4:
return (2, 2)
k = 1
while k ** 2 < nplots:
k += 1
if (k - 1) * k >= nplots:
return k, (k - 1)
else:
return k, k
# copied from matplotlib/pyplot.py for compatibility with matplotlib < 1.0
def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw=None, **fig_kw):
"""Create a figure with a set of subplots already made.
This utility wrapper makes it convenient to create common layouts of
subplots, including the enclosing figure object, in a single call.
Keyword arguments:
nrows : int
Number of rows of the subplot grid. Defaults to 1.
ncols : int
Number of columns of the subplot grid. Defaults to 1.
sharex : bool
If True, the X axis will be shared amongst all subplots.
sharex : bool
If True, the Y axis will be shared amongst all subplots.
squeeze : bool
If True, extra dimensions are squeezed out from the returned axis object:
- if only one subplot is constructed (nrows=ncols=1), the resulting
single Axis object is returned as a scalar.
- for Nx1 or 1xN subplots, the returned object is a 1-d numpy object
array of Axis objects are returned as numpy 1-d arrays.
- for NxM subplots with N>1 and M>1 are returned as a 2d array.
If False, no squeezing at all is done: the returned axis object is always
a 2-d array contaning Axis instances, even if it ends up being 1x1.
subplot_kw : dict
Dict with keywords passed to the add_subplot() call used to create each
subplots.
fig_kw : dict
Dict with keywords passed to the figure() call. Note that all keywords
not recognized above will be automatically included here.
Returns:
fig, ax : tuple
- fig is the Matplotlib Figure object
- ax can be either a single axis object or an array of axis objects if
more than one supblot was created. The dimensions of the resulting array
can be controlled with the squeeze keyword, see above.
**Examples:**
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
# Just a figure and one subplot
f, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
# Two subplots, unpack the output array immediately
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
# Four polar axes
plt.subplots(2, 2, subplot_kw=dict(polar=True))
"""
import matplotlib.pyplot as plt
if subplot_kw is None:
subplot_kw = {}
fig = plt.figure(**fig_kw)
# Create empty object array to hold all axes. It's easiest to make it 1-d
# so we can just append subplots upon creation, and then
nplots = nrows*ncols
axarr = np.empty(nplots, dtype=object)
# Create first subplot separately, so we can share it if requested
ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)
if sharex:
subplot_kw['sharex'] = ax0
if sharey:
subplot_kw['sharey'] = ax0
axarr[0] = ax0
# Note off-by-one counting because add_subplot uses the MATLAB 1-based
# convention.
for i in range(1, nplots):
axarr[i] = fig.add_subplot(nrows, ncols, i+1, **subplot_kw)
if squeeze:
# Reshape the array to have the final desired dimension (nrow,ncol),
# though discarding unneeded dimensions that equal 1. If we only have
# one subplot, just return it instead of a 1-element array.
if nplots==1:
return fig, axarr[0]
else:
return fig, axarr.reshape(nrows, ncols).squeeze()
else:
# returned axis array will be always 2-d, even if nrows=ncols=1
return fig, axarr.reshape(nrows, ncols)
if __name__ == '__main__':
import pandas.rpy.common as com
sales = com.load_data('sanfrancisco.home.sales', package='nutshell')
top10 = sales['zip'].value_counts()[:10].index
sales2 = sales[sales.zip.isin(top10)]
fig = scatter_plot(sales2, 'squarefeet', 'price', by='zip')
# plt.show()
|