def box_plot(df, **kwargs): """ Box plot based on seaborns boxplot Parameters ---------- df : pandas.DataFrame Seaborn compliant (long style) DataFrame kwargs : dict kwargs for seaborn.boxplot and plotting_base See Also -------- seaborn.boxplot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, **kwargs): meanprops = dict(marker='o', markeredgecolor='black', markerfacecolor="None", markersize=5) sns.boxplot(data=df, ax=axis, meanprops=meanprops, **kwargs) plotting_base(plot_func, df, **kwargs)
def scatter_plot(x, y, colorbar=False, **kwargs): """ scatter plot based on matplotlib.pyplot.scatter Parameters ---------- x : ndarray vector of x values y : ndarray vector of y values colorbar : bool, optional Flag to add colorbar, by default False kwargs : dict kwargs for matplotlib.pyplot.scatter and plotting_base See Also -------- matplotlib.pyplot.scatter : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, x, y, colorbar=False, **kwargs): cbar = axis.scatter(x, y, **kwargs) if colorbar: plt.colorbar(cbar) plotting_base(plot_func, x, y, colorbar=colorbar, **kwargs)
def sns_hist_plot(*arrays, colors=None, **kwargs): """ Histogram plot using seaborn's distplot Parameters ---------- arrays : ndarray nx1 array (or arrays) of data to create histogram from colors : list | str Color palette or list of colors to use kwargs : dict kwargs for seaborn.distplot or plotting_base See Also -------- seaborn.distplot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, *arrays, colors=None, **kwargs): colors = get_colors(color_palette=colors) for arr in arrays: sns.distplot(arr, color=next(colors), ax=axis, **kwargs) plotting_base(plot_func, *arrays, colors=colors, legend=None, **kwargs)
def df_error_plot(df, error, **kwargs): """ point / line plot with error bars based on pandas plot Parameters ---------- df : pandas.DataFrame DataFrame of data to plot error : pandas.DataFrame Error values for data values kwargs : dict kwargs for pandas.DataFrame.plot and plotting_base See Also -------- pandas.DataFrame.plot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, error, **kwargs): error.columns = df.columns error.index = df.index df.plot(yerr=error, ax=axis, **kwargs) plotting_base(plot_func, df, error, **kwargs)
def hist_plot(*arrays, colors=None, **kwargs): """ Histogram plot using matplotlib.pyplot.hist Parameters ---------- arrays : ndarray nx1 array (or arrays) of data to create histogram from colors : list | str Color palette or list of colors to use kwargs : dict kwargs for matplotlib.pyplot.hist or plotting_base See Also -------- matplotlib.pyplot.hist : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, *arrays, colors=None, **kwargs): colors = get_colors(color_palette=colors) for arr in arrays: axis.hist(arr, color=next(colors), **kwargs) plotting_base(plot_func, *arrays, colors=colors, legend=None, **kwargs)
def bar_plot(df, kind='bar', **kwargs): """ Bar plot based on seaborn's catplot Parameters ---------- df : pandas.DataFrame Seaborn compliant (long style) DataFrame kind : str kind of plot to use "count" or "bar" kwargs : dict kwargs for seaborn.barplot and plotting_base See Also -------- seaborn.catplot : plotting function seaborn.barplot : plotting function seaborn.countplot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, kind='bar', **kwargs): if kind == 'bar': sns.barplot(data=df, ax=axis, **kwargs) elif kind == 'count': sns.countplot(data=df, ax=axis, **kwargs) else: raise ValueError('kind must be "count" or "bar"') plotting_base(plot_func, df, kind=kind, **kwargs)
def stackedbar_plot(df, x, y, stack, **kwargs): """ Bar plot based on seaborn's catplot Parameters ---------- df : pandas.DataFrame Seaborn compliant (long style) DataFrame x : str Column to use for x-axis y : str Column to use for y-axis stack : str Column to stack order : list Stacking order kwargs : dict kwargs for pandas.DataFrame.plot and plotting_base See Also -------- pandas.DataFrame.plot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, x, y, stack, order=None, **kwargs): df = df.pivot(index=x, values=y, columns=stack) if order is not None: df = df[order] df.plot(kind='bar', stacked=True, ax=axis, **kwargs) plotting_base(plot_func, df, x, y, stack, **kwargs)
def dist_plot(df, fit=False, **kwargs): """ Distribution plot based on seaborn distplot Parameters ---------- df : pandas.DataFrame | pandas.Series Seaborn compliant (long style) DataFrame fit : bool Fit the distribution kwargs : dict kwargs for seaborn.distplot and plotting_base See Also -------- seaborn.distplot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, fit=None, **kwargs): palette = itertools.cycle(sns.color_palette()) if isinstance(df, pd.DataFrame): for label, series in df.iteritems(): if fit: sns.distplot(series, kde=False, fit_kws={"color": next(palette)}, label=label, ax=axis, **kwargs) else: sns.distplot(series, label=label, ax=axis, **kwargs) else: if fit: sns.distplot(df, kde=False, fit_kws={"color": next(palette)}, ax=axis, **kwargs) else: sns.distplot(df, ax=axis, **kwargs) plotting_base(plot_func, df, fit=fit, **kwargs)
def point_plot(df, **kwargs): """ Point / line plot based on seaborn pointplot Parameters ---------- df : pandas.DataFrame Seaborn compliant (long style) DataFrame kwargs : dict kwargs for seaborn.pointplot and plotting_base See Also -------- seaborn.pointplot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, **kwargs): sns.pointplot(data=df, ax=axis, **kwargs) plotting_base(plot_func, df, **kwargs)
def df_pie_plot(df, **kwargs): """ Pie chart using pandas plot Parameters ---------- df : pandas.DataFrame DataFrame of data to plot kwargs : dict kwargs for pandas.DataFrame.plot and plotting_base See Also -------- pandas.DataFrame.plot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, **kwargs): df.plot.pie(ax=axis, **kwargs) plotting_base(plot_func, df, **kwargs)
def df_line_plot(df, **kwargs): """ point / line plot based on pandas plot Parameters ---------- df : pandas.DataFrame DataFrame of data to plot kwargs : dict kwargs for pandas.DataFrame.plot and plotting_base See Also -------- pandas.DataFrame.plot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, **kwargs): df.plot(ax=axis, **kwargs) plotting_base(plot_func, df, **kwargs)
def df_scatter(df, **kwargs): """ scatter plot based on pandas.plot.scatter Parameters ---------- df : pandas.DataFrame Seaborn compliant (long style) DataFrame kwargs : dict kwargs for pandas.DataFrame.plot.scatter and plotting_base See Also -------- pandas.DataFrame.plot.scatter : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, df, **kwargs): df.plot.scatter(ax=axis, **kwargs) plotting_base(plot_func, df, **kwargs)
def df_bar_plot(df, **kwargs): """ Bar plot based on pandas bar plot Parameters ---------- df : pandas.DataFrame DataFrame to plot kwargs : dict kwargs for pandas.DataFrame.plot See Also -------- pandas.DataFrame.plot plotting.base.plotting_base : plotting base """ def plot_func(axis, df, **kwargs): df.plot(kind='bar', ax=axis, **kwargs) plotting_base(plot_func, df, **kwargs)
def heatmap_plot(data, **kwargs): """ Heat map plot using seaborn heatmap Parameters ---------- data : ndarray | pandas.DataFrame ndarray of heatmap values or pandas DataFrame of heat map values with tick labels as index and column labels kwargs : dict kwargs for seaborn.heatmap and plotting_base See Also -------- seaborn.heatmap : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, data, **kwargs): sns.heatmap(data, ax=axis, **kwargs) plotting_base(plot_func, data, legend=None, **kwargs)
def error_plot(data_error, legend=None, **kwargs): """ Line plot with error bars Parameters ---------- data_error : ndarray, shape(data[i]) = (n,2) Either a tuple or list of nx2 arrays or a single nx2 array. legend : list Legend values to plot colors : str | list, optional color or list of colors to use for lines, by default None linestyles : str | list, optional Linestyle or list of linestyles to use, by default 'Automatic' linewidth : int, optional width of lines, by default 2 capsize : int, optional Error bar cap size, by default 6 markers : str | list, optional Marker or list of markers to use, by default None markersize : int, optional Marker size, by default 5 markeredge : list, optional Marker edge style, by default ['k', 0.5] kwargs : dict kwargs for plotting_base See Also -------- matplotlib.pyplot.errorbar : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, data_error, colors=None, linestyles='Automatic', linewidth=2, capsize=6, markers=None, markersize=5, markeredge=['k', 0.5]): msg = 'Input data needs to be in (data, error) pairs' assert isinstance(data_error, (list, tuple)), msg colors, linestyles, markers = get_line_styles(colors=colors, linestyles=linestyles, markers=markers) if markeredge is not None: mec, mew = markeredge else: mec = None mew = None for data, error in data_error: x = data[:, 0] x_error = error[:, 0] if np.isnan(x_error).all(): x_error = None y = data[:, 1] y_error = error[:, 1] if np.isnan(y_error).all(): y_error = None axis.errorbar(x, y, xerr=x_error, yerr=y_error, linewidth=linewidth, markersize=markersize, marker=next(markers), color=next(colors), linestyle=next(linestyles), mec=mec, mew=mew, capsize=capsize, capthick=linewidth) plotting_base(plot_func, data_error, legend=legend, **kwargs)
def line_plot(*lines, legend=None, **kwargs): """ Point / line plot Parameters ---------- lines : ndarray, shape(line) = (n,2) each line in lines must be a nx2 array or nx2 list legend : list Legend values to plot colors : str | list, optional color or list of colors to use for lines, by default None linestyles : str | list, optional Linestyle or list of linestyles to use, by default 'Automatic' linewidth : int, optional width of lines, by default 2 markers : str | list, optional Marker or list of markers to use, by default None markersize : int, optional Marker size, by default 5 markeredge : list, optional Marker edge style, by default ['k', 0.5] alpha : float | list, optional Opacity of list of opacities for lines, by default 1.0 kwargs : dict kwargs for plotting_base See Also -------- matplotlib.pyplot.plot : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, *lines, colors=None, linestyles='Automatic', linewidth=2, markers=None, markersize=5, markeredge=['k', 0.5], alpha=1.0): colors, linestyles, markers = get_line_styles(colors=colors, linestyles=linestyles, markers=markers) if markeredge is not None: mec, mew = markeredge else: mec = None mew = None if not isinstance(markersize, (list, tuple)): markersize = (markersize, ) markersize = itertools.cycle(markersize) if not isinstance(alpha, (list, tuple)): alpha = (alpha, ) alpha = itertools.cycle(alpha) for line in lines: if not isinstance(line, np.ndarray): line = np.array(line) axis.plot(line[:, 0], line[:, 1], markersize=next(markersize), marker=next(markers), mec=mec, mew=mew, color=next(colors), alpha=next(alpha), linestyle=next(linestyles), linewidth=linewidth) plotting_base(plot_func, *lines, legend=legend, **kwargs)
def contour_plot(data, **kwargs): """ Create a contoured colormap from data shape = (n, 3) Parameters ---------- data : ndarray n X 3 array of data to plot of form (x, y, c) figsize : tuple, optional Figure size, by default (8, 6) fontsize : int, optional Labels font size, by default 14 zlim : float, optional z / c limit, by default None major_spacing : float, optional space between major contours, by default None minor_spacing : float, optional space between minor contours, by default None contour_width : int, optional contour line width, by default 1 contour_color : str, optional contour line color, by default 'k' opacity : float, optional opacity of colormap, by default 1. colorbar : bool, optional Display color bar, by default True colorbar_location : str, optional Location of colorbar, by default 'right' colorbar_label : str, optional Colorbar label, by default None colorbar_lines : bool, optional Plot lines on colorbar, by default True colorbar_ticks : int, optional Number of colorbar ticks, by default None colormap : str, optional colormap style, by default 'jet' kwargs : dict kwargs for plotting_base See Also -------- matplotlib.pyplot.contour : plotting function matplotlib.pyplot.countourf : plotting function plotting.base.plotting_base : plotting base """ def plot_func(axis, data, figsize=(8, 6), fontsize=14, zlim=None, major_spacing=None, minor_spacing=None, contour_width=1, contour_color='k', opacity=1., colorbar=True, colorbar_location='right', colorbar_label=None, colorbar_lines=True, colorbar_ticks=None, colormap='jet'): assert len(data) == 3, 'Data must be of shape (x, y, c)' x, y, z = data z_m = ma.masked_invalid(z) a_ratio = z.shape a_ratio = a_ratio[1] / a_ratio[0] if isinstance(figsize, (int, float)): figsize = [figsize * a_ratio, figsize] else: figsize = max(figsize) figsize = [figsize * a_ratio, figsize] if zlim is None: zmin, zmax = np.nanmin(z), np.nanmax(z) else: zmin, zmax = zlim if major_spacing is None: major_spacing = (zmax - zmin) / 10 if minor_spacing is None: minor_spacing = major_spacing / 10 cl_levels = np.arange(zmin, zmax + major_spacing, major_spacing) cf_levels = np.arange(zmin, zmax + minor_spacing, minor_spacing) if colorbar_ticks is None: l_levels = cl_levels[::2] else: l_levels = (zmax - zmin) / colorbar_ticks l_levels = np.arange(zmin, zmax + l_levels, l_levels) orientation = 'vertical' if colorbar_location in ['top', 'bottom']: orientation = 'horizontal' cf = plt.contourf(x, y, z_m, alpha=opacity, levels=cf_levels, extend='both', antialiased=True) if contour_color is not None: cl = plt.contour(cf, levels=cl_levels, colors=(contour_color,), linewidths=(contour_width,)) if colormap is not None: cf.set_cmap(colormap) if colorbar: cbar_padding = 0.1 if colorbar_location in ['top', 'bottom']: figsize[1] += figsize[1] / 10 cbar_size = figsize[0] / 20 else: figsize[0] += figsize[0] / 10 cbar_size = figsize[1] / 20 divider = make_axes_locatable(axis) caxis = divider.append_axes(colorbar_location, size=cbar_size, pad=cbar_padding) cbar = plt.colorbar(cf, ticks=l_levels, cax=caxis, orientation=orientation, ticklocation=colorbar_location) cbar.ax.tick_params(labelsize=fontsize - 2) if colorbar_label is not None: cbar.set_label(colorbar_label, size=fontsize) if colorbar_lines is not None: if contour_color is not None: cbar.add_lines(cl) plotting_base(plot_func, data, **kwargs)