Incenp.org´s plotting helper library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

82 lines
3.1 KiB

# -*- coding: utf-8 -*-
# Incenp.Plotting - Incenp.org's plotting helper library
# Copyright © 2020 Damien Goutte-Gattat
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Module description."""
from .util import xdistr
def scatterplot_subtrack(ax, data, n_track, n_subtrack, max_subtrack, color, width=.7, min_sep=.1):
offset = n_track * max_subtrack + n_subtrack
xs = xdistr(data.values, width, center=True, min_sep=min_sep, offset=offset)
ax.plot(xs, data.values, color + '.')
ax.hlines(data.mean(), offset - .5, offset + .5, color)
def scatterplot(ax, data, columns, subtrackcolumns=False, tracks=[None], trackname=0, subtracks=[None], subtrackname=1,
colors='rgb', width=.7, min_sep=.1):
if isinstance(columns, list):
if subtrackcolumns:
n = len(columns) + 1
labels = tracks
else:
n = len(subtracks) + 1
labels = columns
else:
n = len(subtracks) + 1
labels = tracks
i = 0
if isinstance(columns, list) and not subtrackcolumns:
for column in columns:
j = 0
for subtrack in subtracks:
if subtrack is None:
subset = data.loc[:, column].dropna()
else:
level = subtrackname if data.index.nlevels > 1 else None
subset = data.xs(subtrack, level=level).loc[:, column].dropna()
scatterplot_subtrack(ax, subset, i, j, n, colors[j % n], width, min_sep)
j += 1
i += 1
else:
for track in tracks:
j = 0
if isinstance(columns, list) and subtrackcolumns:
for column in columns:
subset = data.xs(track, level=trackname).loc[:, column].dropna()
scatterplot_subtrack(ax, subset, i, j, n, colors[j % n], width, min_sep)
j += 1
i += 1
else:
for subtrack in subtracks:
indexer = [track, subtrack] if subtrack else [track]
level = [trackname, subtrackname] if subtrack else [trackname]
subset = data.xs(indexer, level=level).loc[:, columns].dropna()
scatterplot_subtrack(ax, subset, i, j, n, colors[j % n], width, min_sep)
j += 1
i += 1
ax.set_xticks([(.5 * (n - 2)) + n * i for i in range(len(labels))])
ax.set_xticklabels(labels)