def test_dodge_preserve_single_text(): df1 = pd.DataFrame({'x': ['a', 'b', 'b', 'b'], 'y': ['a', 'a', 'b', 'b']}) d = position_dodge(preserve='single', width=0.9) p = (ggplot(df1, aes('x', fill='y')) + geom_bar(position=d) + geom_text(aes(y=after_stat('count'), label=after_stat('count')), stat='count', position=d, va='bottom')) assert p + _theme == 'dodge_preserve_single_text'
def test_points(): p = ( p0 + geom_point(aes(fill=after_stat('density'), size=after_stat('density')), stat='density_2d', stroke=0, n=16, contour=False) + scale_size_radius(range=(0, 6))) assert p == 'points'
def test_calculated_aes(): # after_stat('ae') mapping1 = aes('x', y=after_stat('density')) mapping2 = aes('x', y=after_stat('density*2')) mapping3 = aes('x', y=after_stat('density + count')) mapping4 = aes('x', y=after_stat('func(density)')) def _test(): assert list(mapping1._calculated.keys()) == ['y'] assert list(mapping2._calculated.keys()) == ['y'] assert list(mapping3._calculated.keys()) == ['y'] assert list(mapping4._calculated.keys()) == ['y'] assert mapping1['y'].after_stat == 'density' assert mapping2['y'].after_stat == 'density*2' assert mapping3['y'].after_stat == 'density + count' assert mapping4['y'].after_stat == 'func(density)' assert mapping1._calculated['y'] == 'density' assert mapping2._calculated['y'] == 'density*2' assert mapping3._calculated['y'] == 'density + count' assert mapping4._calculated['y'] == 'func(density)' _test() # 'stat(ae)', DEPRECATED but still works mapping1 = aes('x', y='stat(density)') mapping2 = aes('x', y='stat(density*2)') mapping3 = aes('x', y='stat(density + count)') mapping4 = aes('x', y='stat(func(density))') _test() # '..ae..', DEPRECATED but still works mapping1 = aes('x', y='..density..') mapping2 = aes('x', y='..density..*2') mapping3 = aes('x', y='..density.. + ..count..') mapping4 = aes('x', y='func(..density..)') _test() df = pd.DataFrame({'x': [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]}) p = ggplot(df) + geom_bar(aes(x='x', fill=after_stat('count + 2'))) p.draw_test() p = ggplot(df) + geom_bar(aes(x='x', fill='stat(count + 2)')) p.draw_test() p = ggplot(df) + geom_bar(aes(x='x', fill='..count.. + 2')) p.draw_test()
def test_stat_count_float(): df = pd.DataFrame({'x': ['a', 'b'], 'weight': [1.5, 2.5]}) p = (ggplot(df) + aes(x='x', weight='weight', fill='x') + geom_bar() + geom_text(aes(label=after_stat('count')), stat='count')) assert p + _theme == 'stat-count-float'
def test_computed_y_column(): p = (ggplot(df, aes('x')) + stat_ecdf(size=2) # Should be able to used computed y column & create a # new mapped column also called y + stat_ecdf(aes(y=after_stat('y-0.2')), size=2, color='blue') ) assert p == 'computed_y_column'
from scipy.stats import norm import plotnine as plt #---------------------------------------- # Generate data #---------------------------------------- data = pd.DataFrame(data=np.random.normal(-1, 1, 4000)) data = data[data<0].dropna() data = -1*data[0:2000].reset_index(drop=True) data.columns = ['v1'] #---------------------------------------- # Figure 1 #---------------------------------------- data['pdf'] = norm.pdf(data, 1, 1)/norm.sf(0, 1, 1) hist_plot = plt.ggplot(data, plt.aes(x='v1', y=plt.after_stat('density')))+ plt.geom_histogram(alpha=0.6, fill='red') + plt.theme_bw() + plt.geom_line(plt.aes(x='v1', y='pdf')) model2 = lpdensity(data['v1'], bw=[0.5], grid = np.linspace(0, 4, num=int(4/0.5))) model2.plot() #------------------------------------------------------------------- # lpdensity(): Estimation with bandwidth 0.5 on provided grid points #------------------------------------------------------------------- model1 = lpdensity(data['v1'], bw=[0.5], grid = np.linspace(0, 4, num=int(4/0.5))) model1.plot() print(repr(model1)) #---------------------------------------- # lpdensity(): extracting estimation # results #----------------------------------------
def test_polygon(): p = p0 + stat_density_2d(aes(fill=after_stat('level')), geom='polygon') assert p == 'polygon'
def test_contours(): p = p0 + geom_density_2d(aes(color=after_stat('level'))) assert p == 'contours'