Пример #1
0
 def plot(i):
     if i == 2:
         _lims = lims(color=(3, 7))
     else:
         _lims = lims(color=(1, 7))
     return (qplot(x, y, color=colors[i], xlab='x', ylab='y')
             + _lims
             + labs(color='color')
             + theme_minimal()
             + _theme
             )
Пример #2
0
 def plot(i):
     c = colors[i]
     if i == 2:
         p = (qplot(x, y, color=c, xlab='x', ylab='y')
              + lims(color=(1, 7))
              + labs(color='color'))
     else:
         p = (qplot(x, y, stroke=c, xlab='x', ylab='y')
              + lims(stroke=(1, 7))
              + labs(stroke='stroke'))
     return p + theme_minimal()
Пример #3
0
 def plot(i):
     c = colors[i]
     if i == 2:
         p = (qplot(x, y, color=c, xlab='x', ylab='y')
              + lims(color=(1, 7))
              + labs(color='color'))
     else:
         p = (qplot(x, y, stroke=c, xlab='x', ylab='y')
              + lims(stroke=(1, 7))
              + labs(stroke='stroke'))
     return p + theme_minimal()
Пример #4
0
 def plot(i):
     if i == 2:
         _lims = lims(color=(3, 7))
     else:
         _lims = lims(color=(1, 7))
     return (qplot(x, y, color=colors[i], xlab='x', ylab='y')
             + _lims
             + labs(color='color')
             + theme_minimal()
             + _theme
             )
Пример #5
0
def test_after_scale_mapping():
    df = pd.DataFrame({'x': [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]})
    df2 = pd.DataFrame({
        # Same as above, but add 2 of each unique element
        'x': [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] + [1, 2, 3, 4] * 2
    })

    p = ggplot(df) + geom_bar(aes(x='x', ymax=after_scale('ymax + 2')))
    p2 = ggplot(df2) + geom_bar(aes(x='x'))

    assert p + lims(y=(0, 7)) == 'after_scale_mapping'
    assert p2 + lims(y=(0, 7)) == 'after_scale_mapping'
Пример #6
0
def test_few_datapoints():
    df = pd.DataFrame({'x': [1, 2, 2, 3, 3, 3], 'z': list('abbccc')})

    # Bandwidth not set
    p = (ggplot(df, aes('x', color='z')) + geom_density() + lims(x=(-3, 9)))
    with pytest.warns(PlotnineWarning) as record:
        p.draw_test()

    record = list(record)  # iterate more than 1 time
    assert any('e.g `bw=0.1`' in str(r.message) for r in record)
    assert any('Groups with fewer than 2' in str(r.message) for r in record)

    p = (ggplot(df, aes('x', color='z')) + geom_density(bw=.1) +
         lims(x=(0, 4)))
    assert p == 'few_datapoints'
Пример #7
0
 def plot(i):
     return (qplot(x, y, color=colors[i], xlab='x', ylab='y')
             + lims(color=(1, 7))
             + labs(color='color')
             + theme_minimal()
             + _theme
             )
Пример #8
0
 def plot(i):
     return (qplot(x, y, color=colors[i], xlab='x', ylab='y')
             + lims(color=(1, 7))
             + labs(color='color')
             + theme_minimal()
             + _theme
             )
Пример #9
0
    def plot(i):
        if i == 2:
            p = qplot(x, y, xlab='x', ylab='y')
        else:
            p = (qplot(x, y, color=colors[i], xlab='x', ylab='y')
                 + lims(color=(1, 7))
                 + labs(color='color'))

        return p + theme_minimal()
Пример #10
0
    def plot(i):
        if i == 2:
            p = qplot(x, y, xlab='x', ylab='y')
        else:
            p = (qplot(x, y, color=colors[i], xlab='x', ylab='y')
                 + lims(color=(1, 7))
                 + labs(color='color'))

        return p + theme_minimal()
Пример #11
0
def test_datetime_scale_limits():
    n = 6

    df = pd.DataFrame({
        'x': [datetime(x, 1, 1) for x in range(2000, 2000 + n)],
        'y': range(n)
    })

    p = (ggplot(df, aes('x', 'y')) + geom_point() + lims(
        x=[datetime(2000, 1, 1), datetime(2007, 1, 1)]) +
         theme(axis_text_x=element_text(angle=45)))

    assert p == 'datetime_scale_limits'
Пример #12
0
def build_plot(ws: Points, i: int, j: int, p: int, q: int):
    df = DataFrame({
        'x': [w.real for w in ws],
        'y': [w.imag for w in ws],
        'include': [
            3 if k == p or k == q else
            -3 if i <= k and k <= j else
            0
            for k in range(len(ws))]});
    return (ggplot(df, aes('x', 'y', color='include'))
            + geom_point()
            + geom_abline(
                slope=slope(ws[p], ws[q]),
                intercept=intercept(ws[p], ws[q]))
            + lims(x=(0,1), y=(0,3), color=(-3.0, 3.0)))
Пример #13
0
def plot_qq(df, color_var, facet_var=None, title=''):
    """
    Inspired by https://www.cureffi.org/2012/08/15/qq-plots-with-matplotlib/
    """
    # retrive pmin, the most significant (i.e. min) p value (for defining
    # the axes)
    axis_max = max(df['pval_neglog10'])

    if facet_var is None:
        pvals = df.groupby(
            by=color_var).apply(calculate_expected_pval).reset_index(
                level=color_var, drop=True)
    else:
        pvals = df.groupby(by=[color_var, facet_var]).apply(
            calculate_expected_pval).reset_index(level=[color_var, facet_var],
                                                 drop=True)

    # now plot these two arrays against each other
    n_colors = pvals[color_var].nunique()
    qqplot = plt9.ggplot(
        pvals,
        plt9.aes(x='expected_pval_neglog10',
                 y='pval_neglog10',
                 color=color_var))
    qqplot = qqplot + plt9.geom_point(size=0.1, alpha=0.25)
    qqplot = qqplot + plt9.geom_abline(
        slope=1, intercept=0, color='black', linetype='dashed')
    qqplot = qqplot + plt9.theme_bw()
    if n_colors < 9:
        qqplot = qqplot + plt9.scale_colour_brewer(palette='Dark2',
                                                   type='qual')
    qqplot = qqplot + plt9.labs(x='Expected (-log10 p-value)',
                                y='Observed (-log10 p-value)',
                                title=title,
                                color='')
    qqplot = qqplot + plt9.lims(x=(0, axis_max), y=(0, axis_max))
    if facet_var is not None:
        qqplot = qqplot + plt9.facet_wrap('~ {}'.format(facet_var), ncol=5)
    qqplot = qqplot + plt9.theme(strip_text=plt9.element_text(size=5),
                                 axis_text_x=plt9.element_text(angle=-45,
                                                               hjust=0))
    # set guide legend alpha to 1
    qqplot = qqplot + plt9.guides(color=plt9.guide_legend(override_aes={
        'size': 2.0,
        'alpha': 1.0
    }))
    return (qqplot)
Пример #14
0
def test_inplace_add():
    p = _p = ggplot(df)

    p += aes('x', 'y')
    assert p is _p

    p += geom_point()
    assert p is _p

    p += stat_identity()
    assert p is _p

    p += scale_x_continuous()
    assert p is _p

    with pytest.warns(PlotnineWarning):
        # Warning for; replacing existing scale added above
        p += xlim(0, 10)
        assert p is _p

    p += lims(y=(0, 10))
    assert p is _p

    p += labs(x='x')
    assert p is _p

    p += coord_trans()
    assert p is _p

    p += facet_null()
    assert p is _p

    p += annotate('point', 5, 5, color='red', size=5)
    assert p is _p

    p += guides()
    assert p is _p

    p += theme_gray()
    assert p is _p

    th = _th = theme_gray()
    th += theme(aspect_ratio=1)
    assert th is _th
import pandas as pd

from plotnine import (ggplot, aes, geom_density_2d, stat_density_2d,
                      geom_point, theme, lims, scale_size_radius)

n = 20
adj = n // 4

df = pd.DataFrame({'x': range(n), 'y': range(n)})
_theme = theme(subplots_adjust={'right': 0.85})

p0 = (ggplot(df, aes('x', 'y')) + lims(x=(-adj, n + adj), y=(-adj, n + adj)) +
      _theme)


def test_contours():
    p = p0 + geom_density_2d(aes(color='stat(level)'))
    assert p == 'contours'


def test_points():
    p = (p0 + geom_point(aes(fill='stat(density)', size='stat(density)'),
                         stat='density_2d',
                         stroke=0,
                         n=16,
                         contour=False) + scale_size_radius(range=(0, 6)))

    assert p == 'points'


def test_polygon():
Пример #16
0
from __future__ import absolute_import, division, print_function

import pandas as pd

from plotnine import (ggplot, aes, geom_density_2d, stat_density_2d,
                      geom_point, theme, lims, scale_size_radius)

n = 20
adj = n//4

df = pd.DataFrame({'x': range(n), 'y': range(n)})
_theme = theme(subplots_adjust={'right': 0.85})

p0 = (ggplot(df, aes('x', 'y'))
      + lims(x=(-adj, n+adj), y=(-adj, n+adj))
      + _theme)


def test_contours():
    p = p0 + geom_density_2d(aes(color='calc(level)'))
    assert p == 'contours'


def test_points():
    p = (p0
         + geom_point(
             aes(fill='calc(density)', size='calc(density)'),
             stat='density_2d',
             stroke=0, n=16, contour=False)
         + scale_size_radius(range=(0, 6)))
Пример #17
0
    items = {}
    data = {}
    all_models = set()
    all_tasks = set()
    data = {}
    with gzip.open(options.input) as ifd:
        for row in csv.DictReader(ifd, delimiter="\t"):
            for k, v in row.items():
                data[k] = data.get(k, [])
                data[k].append(v)
    
    for k in data.keys():
        floats = [maybe_float(x) for x in data[k]]
        if all([re.match(r"^\d+$", x) for x in data[k]]):
            data[k] = [int(x) for x in data[k]]
        elif all(floats):
            data[k] = floats

    df = pandas.DataFrame(data)
    #print df
    x = (ggplot(df, aes("factor(%s)" % (options.x), options.y, color="factor(%s)" % (options.color)))) + \
        ggtitle(options.title.strip("'")) + \
        ylab(options.ylabel.strip("'")) + \
        xlab(options.xlabel.strip("'")) + \
        labs(color=options.color_label.strip("'")) + \
        geom_col(show_legend=False) + \
        lims(y=(0.0, 1.0))
    x.save(options.output)

    #theme(legend_title=element_text("")) + \