def test_multiple_annotation_geoms(): p = (ggplot(df, aes('x', 'y')) + geom_point() + annotate('point', 0, 1, color='red', size=5) + annotate('text', 1, 2, label='Text', color='red', size=15, angle=45) + annotate(geom_rect, xmin=1.8, xmax=2.2, ymin=2.8, ymax=3.2, size=1, color='red', alpha=0.3) + annotate(geom_segment, x=2.8, y=3.8, xend=3.2, yend=4.2, color='red', size=1)) assert p == 'multiple_annotation_geoms'
def plot(solu, k): # Generates a plot of the four bar mechanism, which represents a frame in the animation print("Frame: ", k) sol = solu[k:k + 1] p = ( ggplot(sol) + # MAIN LINKAGE geom_segment(aes(x = 0, y = 0, xend = sol.Ro4[k].real, yend = sol.Ro4[k].imag)) + geom_point(aes(x=0, y=0), shape = 'o', size = 3) + geom_point(aes(x = sol.Ro4[k].real, y = sol.Ro4[k].imag), shape = 'o', size = 3) + # 2ND LINKAGE geom_segment(aes(x = 0, y = 0, xend = sol.Ra[k].real, yend = sol.Ra[k].imag)) + geom_point(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag), shape = 'o', size = 3) + # AP LINKAGE geom_segment(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag, xend = sol.Rpa[k].real, yend = sol.Rpa[k].imag)) + geom_point(aes(x = sol.Rpa[k].real, y = sol.Rpa[k].imag), shape = 'o', size = 3) + # 3RD LINKAGE geom_segment(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag, xend = sol.Rba[k].real, yend = sol.Rba[k].imag)) + geom_point(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag), shape = 'o', size = 3) + # 4TH LINKAGE geom_segment(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag, xend = sol.Ro4[k].real, yend = sol.Ro4[k].imag)) + geom_point(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag), shape = 'o', size = 3) + # NODES IDENTIFICATION annotate("text", x = 0, y = -20, label = "$O_1$") + annotate("text", x = sol.Ro4[k].real, y = sol.Ro4[k].imag -20, label = "$O_4$") + annotate("text", x = sol.Ra[k].real+10, y = sol.Ra[k].imag, label = "$A$") + annotate("text", x = sol.Rba[k].real +20, y = sol.Rba[k].imag -10, label = "$B$") + annotate("text", x = sol.Rpa[k].real, y = sol.Rpa[k].imag -40, label = "$P$") + # ACCELERATIONS ARROWS (you may remove if you wish to remove acceleration informations) geom_segment(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag, \ xend = sol.Rba[k].real + sol.Aba[k].real * ACC_SCALE, \ yend = sol.Rba[k].imag + sol.Aba[k].imag * ACC_SCALE),\ colour='red', arrow=arrow()) + # Point B geom_segment(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag, \ xend = sol.Ra[k].real + sol.Aa[k].real * ACC_SCALE, \ yend = sol.Ra[k].imag + sol.Aa[k].imag * ACC_SCALE),\ colour='red', arrow=arrow()) + # Point A geom_segment(aes(x = sol.Rpa[k].real, y = sol.Rpa[k].imag, \ xend = sol.Rpa[k].real + sol.Apaa[k].real * ACC_SCALE, \ yend = sol.Rpa[k].imag + sol.Apaa[k].imag * ACC_SCALE),\ colour='red', arrow=arrow()) + # Point C # ACCELERATIONS TEXTS (you may comment if you wish to remove acceleration informations) # inputting text between '$ $' makes plotnine produce beautiful LaTeX text annotate("text", x = sol.Rba[k].real-30, y = sol.Rba[k].imag+10, label = f'${np.absolute(sol.Aba[k])/1000:.2f}~m/s^2$', colour='red') + annotate("text", x = sol.Ra[k].real+20, y = sol.Ra[k].imag-20, label = f'${np.absolute(sol.Aa[k])/1000:.2f}~m/s^2$', colour='red') + annotate("text", x = sol.Rpa[k].real+10, y = sol.Rpa[k].imag+20, label = f'${np.absolute(sol.Apaa[k])/1000:.2f}~m/s^2$', colour='red') + # TIME IDENTIFICATION annotate("label", x = 120, y = -80, label = f'Time: ${sol.time[k]:.2f}~s$', alpha = 1) + # labs(x='$x~[mm]$', y='$y~[mm]$') + coord_cartesian(xlim=SCALE_X, ylim=SCALE_Y) + # Scales plot limits, avoiding it to be bigger than necessary. You may comment this out if you wish to do so. theme_bw() # Plot is prettier with this theme compared to the default. ) return p
def test_multiple_annotation_geoms(): p = (ggplot(df, aes('x', 'y')) + geom_point() + annotate('point', 0, 1, color='red', size=5) + annotate('text', 1, 2, label='Text', color='red', size=15, angle=45) + annotate('rect', xmin=1.8, xmax=2.2, ymin=2.8, ymax=3.2, size=1, color='red', alpha=0.3) + annotate('segment', x=2.8, y=3.8, xend=3.2, yend=4.2, color='red', size=1)) assert p == 'multiple_annotation_geoms'
def plot_train_test(ags): frontiers = data.train_test(ags) frontiers, model = data.train_test_model(frontiers) labs = frontiers.sort_values('train_flops').groupby( 'elo').first().reset_index() desc = f'log₁₀(test) = {model.params[1]:.1f} · log₁₀(train) + {model.params[2]:.1g} · elo + {model.params[0]:.0f}' return ( pn.ggplot( frontiers, pn.aes(x='train_flops', y='test_flops', color='elo', group='elo')) + pn.geom_line(size=.5, show_legend=False) + pn.geom_line(pn.aes(y='test_flops_hat'), size=.25, show_legend=False, linetype='dashed') # + pn.geom_point(size=.5, show_legend=False) + pn.geom_text(pn.aes(label='elo.astype(int)'), labs, show_legend=False, size=6, nudge_y=+.2) + pn.scale_color_cmap(limits=(-1500, 0)) + pn.scale_x_continuous(trans='log10') + pn.scale_y_continuous(trans='log10') + pn.annotate( 'text', 1.5e13, 5e9, label=desc, ha='left', size=6, family='serif') + pn.labs(x='Train-time compute (FLOPS-seconds)', y='Test-time compute (FLOPS-seconds)') + plot.IEEE())
def test_layer_with_only_infs(): df = pd.DataFrame({'x': ['a', 'b']}) p = (ggplot(df, aes('x', 'x')) + annotate('rect', xmin=-np.inf, xmax=np.inf, ymin=-np.inf, ymax=np.inf, fill='black', alpha=.25) + geom_point(color='red', size=3) ) p = p.build_test() assert isinstance(p.scales.get_scales('x'), scale_x_discrete)
def plot_optimal_model_size(ags): from statsmodels.formula import api as smf results = {} for b, g in ags.groupby('boardsize'): ordered = g.sort_values('elo').copy() ordered['params'] = g.width**2 * g.depth left = np.log10(g.train_flops.min()) right = np.log10(g.train_flops.max()) for f in np.linspace(left, right, 11)[1:]: subset = ordered[ordered.train_flops <= 10**f] results[b, 10**f] = subset.params.iloc[-1] df = pd.Series(results).reset_index() df.columns = ['boardsize', 'approx_flops', 'params'] model = smf.ols('np.log10(params) ~ np.log10(approx_flops) + 1', df).fit() left, right = np.log10(df.approx_flops.min()), np.log10( df.approx_flops.max()) preds = pd.DataFrame({'approx_flops': 10**np.linspace(left, right, 21)}) preds['params'] = 10**model.predict(preds) labs = df.sort_values('approx_flops').groupby( 'boardsize').last().reset_index() labs['params'] = labs.apply( lambda r: df[df.approx_flops <= r.approx_flops].params.max(), axis=1) points = df.sort_values('approx_flops').groupby( 'boardsize').last().reset_index() desc = f'log₁₀(params) = {model.params[1]:.2f} · log₁₀(compute) − {-model.params[0]:.1f}' return ( pn.ggplot(df, pn.aes(x='approx_flops', y='params')) + pn.geom_line(pn.aes(color='factor(boardsize)', group='boardsize'), show_legend=False) + pn.geom_line(data=preds, linetype='dashed', size=.25) + pn.geom_point(pn.aes(color='factor(boardsize)', group='boardsize'), data=points, size=.5, show_legend=False) + pn.geom_text(pn.aes( color='factor(boardsize)', group='boardsize', label='boardsize'), data=labs, nudge_y=+.5, show_legend=False, size=6) + pn.annotate( 'text', 1e9, 2e7, label=desc, ha='left', size=6, family='serif') + pn.scale_x_continuous(trans='log10') + pn.scale_y_continuous(trans='log10') + pn.scale_color_hue(l=.4) + pn.labs(x='Train-time compute (FLOPS-seconds)', y='Optimal model size (params)') + plot.IEEE())
def ggimg(image, mapping=None, data=None, dpi=80): w, h = image.size return ( ggplot(mapping=mapping, data=data) + scale_y_reverse(limits=(0, h)) + xlim(0, w) + scale_color_discrete(guide=False) # removes legend for line color + theme_image(w, h, dpi=dpi) + annotate( "rect", xmin=0, xmax=w, ymin=0, ymax=h, color="black", fill=None ) # box around image )
def test_non_geom_raises(): with pytest.raises(PlotnineError): annotate('doesnotexist', x=1) with pytest.raises(PlotnineError): annotate(5) class NotAGeom(): pass with pytest.raises(PlotnineError): annotate(NotAGeom) with pytest.raises(PlotnineError): annotate(geom_point())
def add_mirna_g(g,df, str_name,str_start,str_end,dis_pos,l_s,l_e,l_score=[]): # print(str_name,str_start,str_end,dis_pos,l_s,l_e) df[str_start]= pd.Series(l_s) df[str_end] = pd.Series(l_e) g+= pt.annotate("text", x=0,y=dis_pos,label=str_name) g+= pt.geom_errorbarh(df,pt.aes(xmin=str_start,y=(dis_pos),xmax=str_end,color='mi_name')) g+= pt.geom_segment(df,pt.aes(x=str_start,y=(dis_pos),yend=0,xend=str_start,color='mi_name')) if(l_score): # print(l_score) # pd.options.display.float_format = '{:.1f}'.format score_column_name = 'score'+str_name # print(l_score,score_column_name,str_start,dis_pos) df[score_column_name] = pd.Series(l_score,dtype=np.float).map('{:.0f}'.format) g+= pt.geom_text(df, pt.aes(x=str_start,y=dis_pos,label=score_column_name,color='mi_name'), nudge_x=0.1, nudge_y=0.1)#,adjust_text=adjust_text_dict)
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
def cell_division(adata): """ Plots total_counts as a function of the principal circle nodes to visualize the moment of cell division. Parameters ---------------- adata: AnnData The AnnData object being used for the analysis. Must be previously evaluated by `tl.celldiv_moment`. Returns ------------ A plotnine line-plot to help visualize the moment of cell division and direction of the cell cycle. If method = 'counts' when tl.celldiv_moment was run, cell division is defined by the largest drop in total_counts. The changes in counts are represented by the bars at the bottom, and the suggested moment of cell division is marked in red. The cell cycle should follow an incremental increase in total counts until around the moment of cell division. Alternatively, if method='g2m' in tl.celldiv_moment, the G2-M signature dynamics are used to define the moment of cell division. """ ref_var = adata.uns['scycle']['cell_div_moment']['ref_var'] edge_to_0 = adata.uns['scycle']['cell_div_moment']['cell_div_edge'][0] edges = adata.uns['princirc_gr']['edges'] edges['cell_div'] = edges['e1'] == edge_to_0 cell_div_count = edges[edges['e1'] == edge_to_0]['mean_var'] cell_div_plot = (ggplot(edges, aes('e1', 'mean_var')) + geom_point(aes(y = 'mean_var'), size = 2) + geom_path(aes(y = 'mean_var')) + geom_smooth(aes(y = 'mean_var'), method = 'lm', linetype = 'dashed') + annotate("point", x = edge_to_0, y = cell_div_count, color = 'red', size = 2) + labs(x = 'Edge position', y = ref_var) + geom_col(aes(y = 'diff_var', fill = 'cell_div')) + scale_fill_manual(values = ['darkgrey', 'red'], guide = False) + theme_std) return cell_div_plot
print(g) g = ( p9.ggplot(biorxiv_umap_models_lastest[300]) + p9.aes(x="umap1", y="umap2", color="factor(category)") + p9.geom_point() + p9.labs(title="UMAP of BioRxiv (Word dim: 300)", color="Article Category")) g.save("output/embedding_output/umap/figures/biorxiv_umap_300.png", dpi=500) print(g) # Looking at the plots it seems that documents in the same category mostly cluster together, which is expected. The plots appear to be rotated versions of each other, which suggests that dimensionality doesn't hugely affect umap's results. Interesting cases are the outliers that appear within the plot. Question that arises: what are those outliers? # ## UMAP Outliers g = (p9.ggplot(biorxiv_umap_models_lastest[300]) + p9.aes( x="umap1", y="umap2", color="factor(category)" ) + p9.geom_point() + p9.annotate( "segment", x=10, y=7, xend=9.2, yend=5.2, arrow=p9.arrow(), color="red") + p9.annotate("segment", x=10, y=-2.5, xend=12, yend=-0.6, arrow=p9.arrow(), color="red") + p9.annotate("segment", x=7.5, y=-2.2, xend=6.2, yend=-2.2, arrow=p9.arrow(), color="red") + p9.annotate("segment", x=7.5,
def plot_performance(df, report_year, eval_period): """ Plot metric-specific performance for a set of stocks over time. Reference: https://www.buffettsbooks.com/how-to-invest-in-stocks/intermediate-course/lesson-20/ :param df: DataFrame containing stock tickers and the columns specified below :param report_year: Year of most recent financial report :param eval_period: Number of years prior to most recent report to be analyzed :return: A list of ggplot objects :rtype: List """ start_year = report_year - eval_period df = df.loc[df['year'] >= start_year] df = df[[ 'symbol', 'year', 'eps', 'bookValuePerShare', 'roe', 'currentRatio', 'debtToEquity' ]] df['roe'] = df['roe'].apply(lambda x: x * 100.0) df = df.rename( { 'eps': 'Earnings per Share', 'roe': 'Return on Equity', 'currentRatio': 'Current Ratio', 'debtToEquity': 'Debt to Equity Ratio', 'bookValuePerShare': 'Book Value per Share' }, axis='columns') df.sort_values(by=['symbol', 'year'], inplace=True, ascending=True) df.dropna(inplace=True) # Commenting out for now, API no longer returning this col in income-statement response label_dict = { 'Earnings per Share': 'The EPS shows the company\'s profit per share. This chart ' 'should have a positive slope over time. Stable results ' 'here are extremely important for forecasting future cash ' 'flows. Note: if the company\'s book value has increased ' 'over time, the EPS should demonstrate similar growth.', # 'Dividend per Share': 'This chart shows the dividend history of the company. ' # 'This should have a flat to positive slope over time. If ' # 'you see a drastic drop, it may represent a stock split ' # 'for the company. Note: the dividend is taken from a ' # 'portion of the EPS, the remainder goes to the book value.', 'Book Value per Share': 'The book value represents the liquidation value of the ' 'entire company (per share). It\'s important to see ' 'this number increasing over time. If the company pays a' ' high dividend, the book value may grow at a slower ' 'rate. If the company pays no dividend, the book value ' 'should grow with the EPS each year.', 'Return on Equity': 'Return on equity is very important because it show the ' 'return that management has received for reinvesting the ' 'profits of the company. If using an intrinsic value ' 'calculator, it\'s very important that this number is flat or' ' increasing for accurate results. Find companies with a ' 'consistent ROE above 8%.', 'Current Ratio': 'The current ratio helps measure the health of the company in ' 'the short term. As a rule of thumb, the current ratio should be' ' above 1.0. A safe current ratio is typically above 1.5. Look ' 'for stability trends within the current ratio to see how the ' 'company manages their short term risk.', 'Debt to Equity Ratio': 'The debt to equity ratio helps measure the health of ' 'the company in the long term. As a rule of thumb, the ' 'debt to equity ratio should be lower than 0.5. Look for ' 'stability trends within the debt/equity ratio to see how' ' the company manages their long term risk.' } wrapper = textwrap.TextWrapper(width=120) for key, value in label_dict.items(): label_dict[key] = wrapper.fill(text=value) plots = [] cols = df.columns[2:].tolist() for metric in cols: p = (ggplot(df, aes('year', metric, color='symbol')) + geom_line(size=1, alpha=0.8) + geom_point(size=3, alpha=0.8) + labs(title=metric, x='Report Year', y='', color='Ticker') + theme_538() + theme(legend_position='left', plot_title=element_text(weight='bold')) + scale_x_continuous(breaks=range(min(df['year']), max(df['year']) + 1, 1)) + scale_y_continuous( breaks=range(min(df[metric].astype(int)), max(round(df[metric]).astype(int)) + 2, 1)) + annotate(geom='label', x=statistics.mean((df['year'])), y=max(round(df[metric]).astype(int) + 1), label=label_dict[metric], size=8, label_padding=0.8, fill='#F7F7F7')) plots.append(p) return plots
def test_variable_and_annotate(): p = (g + annotate('point', x=4.5, y=5.5, color='cyan', size=10) + facet_wrap('~g')) assert p == 'variable_and_annotate'
"has_methods": "#d8b365", "no_methods": "#5ab4ac" }) + p9.labs(title="Neuroscience Methods Section")) g.save("output/tsne/neuroscience_missing_methods_overlapped.png", dpi=500) print(g) # In[13]: g = (p9.ggplot( biorxiv_tsne_method_section_df.query("category=='neuroscience'")) + p9.aes(x="tsne1", y="tsne2", color="section") + p9.geom_point() + p9.theme_bw() + p9.scale_color_manual({ "has_methods": "#d8b365", "no_methods": "#5ab4ac" }) + p9.labs(title="Neuroscience Methods Section") + p9.annotate("rect", xmin=-30, xmax=0, ymin=-50, ymax=-30, alpha=0.4)) print(g) # In[14]: (biorxiv_tsne_method_section_df.query("category=='neuroscience'").query( "tsne1 > -30 & tsne1 <=0").query("tsne2 < -30")) # Personal Inspection: # | Document | Comment | # | --- | --- | # | [10.1101/053827](https://doi.org/10.1101/053827) | Dense evolutionary biology paper/neuroscience? | # | [10.1101/164780](https://doi.org/10.1101/164780) | Dense evolutionary biology paper/neuroscience? | # | [10.1101/164780](https://doi.org/10.1101/164780) | has a brief methods section at the end of the paper, but it isn't really a methods section | # | [10.1101/053827](https://doi.org/10.1101/053827) | doesn't have a methods section at all |
plot_df.head() g = (p9.ggplot( plot_df, p9.aes(y="lemma", x="lower_odds", xend="upper_odds", yend="lemma")) + p9.geom_segment(color="#253494", size=6, alpha=0.7) + p9.scale_y_discrete(limits=( plot_df.sort_values("odds_ratio", ascending=True).lemma.tolist())) + p9.scale_x_continuous(limits=(-3, 3)) + p9.geom_vline(p9.aes(xintercept=0), linetype="--", color="grey") + p9.annotate( "segment", x=0.5, xend=2.5, y=1.5, yend=1.5, colour="black", size=0.5, alpha=1, arrow=p9.arrow(length=0.1), ) + p9.annotate( "text", label="bioRxiv Enriched", x=1.5, y=2.5, size=14, alpha=0.7) + p9.annotate( "segment", x=-0.5, xend=-2.5, y=39.5, yend=39.5, colour="black", size=0.5, alpha=1,
y="lemma", xmin="lower_odds", x="odds_ratio", xmax="upper_odds", yend="lemma", ), ) + p9.geom_errorbarh(color="#253494") + p9.scale_y_discrete(limits=( plot_df.sort_values("odds_ratio", ascending=True).lemma.tolist())) + p9.scale_x_continuous(limits=(-3, 3)) + p9.geom_vline(p9.aes(xintercept=0), linetype="--", color="grey") + p9.annotate( "segment", x=0.5, xend=2.5, y=1.5, yend=1.5, colour="black", size=0.5, alpha=1, arrow=p9.arrow(length=0.1), ) + p9.annotate( "text", label="bioRxiv Enriched", x=1.5, y=2.5, size=18, alpha=0.7) + p9.annotate( "segment", x=-0.5, xend=-2.5, y=39.5, yend=39.5, colour="black", size=0.5, alpha=1,
bcolor = '#9e2f68' bcolor_lighter = '#f4d8e6' bcolor_darker = '#631d41' gradient = ((0.99, 0.88, 0.87), (0.98, 0.62, 0.71), (0.86, 0.20, 0.59), bcolor, bcolor, bcolor_darker, bcolor_darker) df1 = df[:n // 3:9] df2 = df[n // 3:2 * n // 3] df3 = df[2 * n // 3::12] p = (ggplot(aes('x', 'y', color='y', fill='y')) + annotate(geom='label', x=0.295, y=0.495, label='pl tnine', label_size=1.5, label_padding=.1, size=24, fill=bcolor_lighter, color=bcolor) + geom_point(df1, size=8, stroke=0, show_legend=False) + geom_line(df2, size=2, color=bcolor_darker, show_legend=False) + geom_bar(df3, aes('x+.06'), stat='identity', size=0, show_legend=False) + scale_color_gradientn(colors=gradient) + scale_fill_gradientn(colors=gradient) + theme_void() + theme(figure_size=(3.6, 3.6))) p.save('logo.pdf', pad_inches=-0.04) # Remove the project name p.layers = p.layers.__class__(p.layers[1:])
bcolor_darker = '#631d41' gradient = ( (0.99, 0.88, 0.87), (0.98, 0.62, 0.71), (0.86, 0.20, 0.59), bcolor, bcolor, bcolor_darker, bcolor_darker) df1 = df[:n//3:9] df2 = df[n//3:2*n//3] df3 = df[2*n//3::12] p = (ggplot(aes('x', 'y', color='y', fill='y')) + annotate(geom='label', x=0.295, y=0.495, label='pl tnine', label_size=1.5, label_padding=.1, size=24, fill=bcolor_lighter, color=bcolor) + geom_point(df1, size=8, stroke=0, show_legend=False) + geom_line(df2, size=2, color=bcolor_darker, show_legend=False) + geom_bar(df3, aes('x+.06'), stat='identity', size=0, show_legend=False) + scale_color_gradientn(colors=gradient) + scale_fill_gradientn(colors=gradient) + theme_void() + theme(figure_size=(3.6, 3.6))) p.save('logo.pdf', pad_inches=-0.04) # Remove the project name p.layers = p.layers.__class__(p.layers[1:]) p.save('logo-small.pdf', pad_inches=-0.04)
def htcalc(air_velocity_inside, air_velocity_outside, t_inside, t_outside, surface, layers, wall_thickness, thermal_conductivity): # We need the convective heat resistance on both sides of the wall res_conv_inside = heattransfer.convective_resistance( heattransfer.heat_transfer_coef(air_velocity_inside), surface) res_conv_outside = heattransfer.convective_resistance( heattransfer.heat_transfer_coef(air_velocity_outside), surface) # We need the total resistance over all wall layers total_layer_resistance = [] total_layer_resistance.append(res_conv_inside) for i in range(layers): total_layer_resistance.append( heattransfer.conductive_resistance(wall_thickness[i], thermal_conductivity[i], surface)) total_layer_resistance.append(res_conv_outside) total_resistance = sum(total_layer_resistance) heat_transfer = heattransfer.conduction(t_inside, t_outside, total_resistance) # Calculating the temperatures between each layer temperatures = [] temperatures.append(t_inside) layer_resistance = 0 for resistance in total_layer_resistance: layer_resistance += resistance temperatures.append( heattransfer.layer_temperature(heat_transfer, layer_resistance, t_inside)) # Preparing the x axis, position of the temperature and transition labels for the graph position = [0, 0.02] labels = ['fluid inside', 'inner surface'] i = 0 for entry in wall_thickness: position.append(position[-1] + entry) i += 1 labels.append("layer" + str(i)) labels[-1] = "outer surface" position.append(position[-1] + 0.02) labels.append("fluid outside") # print(f"\nThe total resistance is {round(total_resistance, 2)} K/W") # print(f"Total heat transfer from inside to outside is {round(heat_transfer, 2)} W\n") df = pd.DataFrame({'pos': position, 'temp': temperatures}) gg = p9.ggplot(df, p9.aes(x='pos', y='temp')) gg += p9.geom_line(p9.aes(color='temp'), size=2) for ws in df.pos.values.tolist(): gg += p9.geom_vline(xintercept=ws, color='grey') # gg += p9.geom_hline(yintercept=110, color='red', size=2, alpha=0.8) gg += p9.ggtitle('heat transfer through wall') gg += p9.scale_x_continuous(name='Position', breaks=df.pos.values.tolist(), labels=labels) gg += p9.scale_y_continuous(name='Temperature') gg += p9.theme(axis_text_x=p9.element_text(angle=45)) gg += p9.scale_colour_gradient(low="yellow", high="orange") i = 0 for temp in temperatures: gg += p9.geom_text( p9.aes(x=position[i], y=temp + 30, label=round(temp, 2))) i += 1 for i in range(layers): labtext = 'Thermal cond.: ' + str( thermal_conductivity[i]) + ' [W/m°K]\nLayer thickness: ' + str( round(wall_thickness[i], 3)) + ' [m]' gg += p9.annotate(geom='text', x=((position[i + 2] - position[i + 1]) / 2) + position[i + 1], y=temperatures[i] + 30, label=labtext, color='blue') return gg
# plot the x axis titles + p9.geom_vline(xintercept=[2.5, 14.5, 26.5, 38.5, 50.5, 62.5, 74.5]) + p9.geom_text(label="2014", x=8.5, y=0, color="black") + p9.geom_text(label="2015", x=20.5, y=0, color="black") + p9.geom_text(label="2016", x=32.5, y=0, color="black") + p9.geom_text(label="2017", x=44.5, y=0, color="black") + p9.geom_text(label="2018", x=56.5, y=0, color="black") + p9.geom_text(label="2019", x=68.5, y=0, color="black") # Plot the overall proportion published + p9.geom_hline( yintercept=0.4196, linetype='solid', color=color_mapper['2018']) + p9.geom_hline(yintercept=published / posted, linetype="solid", color=color_mapper['2020ML']) + p9.annotate("text", x=8.5, y=0.395, label="overall: 0.4196", size=8) + p9.annotate("text", x=8.5, y=0.48, label=f"overall: {published/posted:.4f}", size=8) + p9.theme_seaborn(style='ticks', context='paper', font_scale=1.5) + p9.theme(figure_size=(10, 4.5), axis_text_x=p9.element_blank(), axis_title_x=p9.element_text(margin={"t": 15})) + p9.labs(y="Proportion Published", x="Month")) g.save("output/figures/publication_rate.svg", dpi=250) g.save("output/figures/publication_rate.png", dpi=250) print(g)
geom_point(aes(x=0, y=0), shape = 'o', size = 3) + geom_point(aes(x = sol.Ro4[k].real, y = sol.Ro4[k].imag), shape = 'o', size = 3) + # 2ND LINKAGE geom_segment(aes(x = 0, y = 0, xend = sol.Ra[k].real, yend = sol.Ra[k].imag)) + geom_point(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag), shape = 'o', size = 3) + # AP LINKAGE geom_segment(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag, xend = sol.Rpa[k].real, yend = sol.Rpa[k].imag)) + geom_point(aes(x = sol.Rpa[k].real, y = sol.Rpa[k].imag), shape = 'o', size = 3) + # 3RD LINKAGE geom_segment(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag, xend = sol.Rba[k].real, yend = sol.Rba[k].imag)) + geom_point(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag), shape = 'o', size = 3) + # 4TH LINKAGE geom_segment(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag, xend = sol.Ro4[k].real, yend = sol.Ro4[k].imag)) + geom_point(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag), shape = 'o', size = 3) + # NODES IDENTIFICATION annotate("text", x = 0, y = -10, label = "$O_1$") + annotate("text", x = sol.Ro4[k].real, y = sol.Ro4[k].imag -10, label = "$O_4$") + annotate("text", x = sol.Ra[k].real, y = sol.Ra[k].imag -10, label = "$A$") + annotate("text", x = sol.Rba[k].real -5, y = sol.Rba[k].imag -10, label = "$B$") + annotate("text", x = sol.Rpa[k].real, y = sol.Rpa[k].imag -10, label = "$P$") + # VELOCITIES ARROWS (you may remove if you wish to remove acceleration informations) geom_segment(aes(x = sol.Rba[k].real, y = sol.Rba[k].imag, \ xend = sol.Rba[k].real + sol.Vba[k].real * VEL_SCALE, \ yend = sol.Rba[k].imag + sol.Vba[k].imag * VEL_SCALE),\ colour='orange', arrow=arrow()) + # Point B geom_segment(aes(x = sol.Ra[k].real, y = sol.Ra[k].imag, \ xend = sol.Ra[k].real + sol.Va[k].real * VEL_SCALE, \ yend = sol.Ra[k].imag + sol.Va[k].imag * VEL_SCALE),\ colour='orange', arrow=arrow()) + # Point A geom_segment(aes(x = sol.Rpa[k].real, y = sol.Rpa[k].imag, \ xend = sol.Rpa[k].real + sol.Vpaa[k].real * VEL_SCALE, \
p9.scale_shape_manual(["o", "o", "o"]) # plot the x axis titles + p9.geom_vline(xintercept=[2.5, 14.5, 26.5, 38.5, 50.5, 62.5, 74.5]) + p9.geom_text(label="2014", x=8.5, y=0, color="black", size=13) + p9.geom_text(label="2015", x=20.5, y=0, color="black", size=13) + p9.geom_text(label="2016", x=32.5, y=0, color="black", size=13) + p9.geom_text(label="2017", x=44.5, y=0, color="black", size=13) + p9.geom_text(label="2018", x=56.5, y=0, color="black", size=13) + p9.geom_text(label="2019", x=68.5, y=0, color="black", size=13) # Plot the overall proportion published + p9.geom_hline( yintercept=0.4196, linetype="solid", color=color_mapper["2018"]) + p9.geom_hline(yintercept=published / posted, linetype="solid", color=color_mapper["2020ML"]) + p9.annotate("text", x=8.5, y=0.395, label="overall: 0.4196", size=14) + p9.annotate("text", x=8.5, y=0.48, label=f"overall: {published/posted:.4f}", size=14) + p9.theme_seaborn( style="ticks", context="paper", font="Arial", font_scale=2) + p9.theme( figure_size=(11, 6.5), axis_text_x=p9.element_blank(), axis_title_x=p9.element_text(margin={"t": 15}), ) + p9.labs(y="Proportion Published", x="Month")) g.save("output/figures/publication_rate.svg") g.save("output/figures/publication_rate.png", dpi=250) print(g)
inc_lbl_pos = inc_start + (inc_end - inc_start)/2 hatch_start = iglo_nest[iglo_nest.type == "hatch"].julian.min() hatch_end = min(iglo_nest[iglo_nest.type == "hatch"].julian.max(), iglo.julian.max() + 2) hatch_lbl_pos = hatch_start + (hatch_end - hatch_start)/2 xmin = min(inc_start, iglo.julian.min()) xmax = min(iglo_nest[iglo_nest.type == "hatch"].julian.max(), iglo.julian.max() + 2) (ggplot(data=iglo, mapping=aes(x='julian', y='ACI_mean', colour='site')) #+ facet_grid("panel~", scales="free") + xlab("Day") + ylab("Mean daily ACI (standardized)") + geom_point() # + geom_errorbar(aes(ymin="ACI_mean - ACI_std", ymax="ACI_mean + ACI_std")) + geom_smooth(method="mavg", se=False, method_args={"window": 4, "center": True, "min_periods": 1}) + annotate("rect", xmin=[inc_start, hatch_start], xmax=[inc_end, hatch_end], ymin=-math.inf, ymax=math.inf, alpha=0.1, fill=["red", "blue"]) + annotate("text", x=[inc_lbl_pos, hatch_lbl_pos], y=1.8, label=["Incubation end", "Hatch"]) # + geom_line(data = inc, mapping=aes(x="julian", y="uniqueID"), colour="black") # + geom_smooth(data=inc, mapping=aes(x="julian", y="uniqueID"), colour="black", method="mavg", se=False, method_args={"window": 4, "center": True, "min_periods": 1}) + scale_x_continuous(labels=label_x, limits=[xmin, xmax])).save("figs/ACI_IGLO_endinc_noerr.png", height=8, width=8, dpi=150) iglo_inc = iglo_nest.groupby("julian", as_index=False).uniqueID.count().reset_index() (ggplot(data=iglo_inc, mapping=aes(x="julian", y="uniqueID")) + xlab("Day") + ylab("Number of nest initiation/hatch") + geom_smooth(method="mavg", se=False, method_args={"window": 4, "center": True, "min_periods": 1}) + annotate("rect", xmin=[inc_start, hatch_start], xmax=[inc_end, hatch_end], ymin=-math.inf, ymax=math.inf, alpha=0.1, fill=["red", "blue"]) + annotate("text", x=[inc_lbl_pos, hatch_lbl_pos], y=11, label=["incubation", "hatch"]) + scale_x_continuous(labels=label_x, limits=[xmin, xmax])).save("figs/Nest_IGLO_endinc.png", height=8, width=8, dpi=150)
def plot_sinew_inputs(df, var=None, sweep_ind="sweep_ind"): r"""Inspect a sinew design Create a scatterplot matrix with hues. Often used to visualize a sinew design before evaluating the model functions. Usually called as a dispatch from plot_auto(). Args: df (Pandas DataFrame): Input design data var (list of strings): Variables to plot sweep_ind (string): Sweep index column in df Returns: Seaborn scatterplot matrix Examples: >>> import grama as gr >>> import matplotlib.pyplot as plt >>> from grama.models import make_cantilever_beam >>> md = make_cantilever_beam() >>> ## Dispatch from autoplotter >>> ( >>> md >>> >> gr.ev_sinews(df_det="swp", skip=True) >>> >> gr.pt_auto() >>> ) >>> ## Re-create without metadata >>> ( >>> md >>> >> gr.ev_sinews(df_det="swp") >>> >> gr.pt_sinew_inputs(var=md.var) >>> ) """ if var is None: raise ValueError("Must provide input columns list as keyword var") ## Define helpers labels_blank = lambda v: [""] * len(v) breaks_min = lambda lims: (lims[0], 0.5 * (lims[0] + lims[1]), lims[1]) ## Make blank figure fig = ( df >> ggplot() + geom_blank() + theme_void() ).draw(show=False) gs = gridspec.GridSpec(len(var), len(var)) for i, v1 in enumerate(var): for j, v2 in enumerate(var): ax = fig.add_subplot(gs[i, j]) ## Switch labels if j == 0: labels_y = _sci_format else: labels_y = labels_blank if i == len(var) - 1: labels_x = _sci_format else: labels_x = labels_blank ## Label if i == j: p = ( df >> ggplot() + annotate( "label", x=0, y=0, label=v1, ) + theme_void() + guides(color=None) ) ## Scatterplot else: p = ( df >> ggplot(aes(v2, v1, color="factor("+sweep_ind+")")) + geom_point(size=0.1) + scale_x_continuous( breaks=breaks_min, labels=labels_x, ) + scale_y_continuous( breaks=breaks_min, labels=labels_y, ) + guides(color=None) + theme_minimal() + theme( axis_title=element_text(va="top", size=12), ) ) _ = p._draw_using_figure(fig, [ax]) ## Plot # NB Returning the figure causes a "double plot" in Jupyter.... fig.show()
pred = lm.predict(barrels) r2 = model.score(barrels, crude_rate) ## Plot barrels and crude rate g = (ggplot(beer, aes(x = 'barrels', y = 'crude.rate')) + geom_point(color = 'black') + geom_text(aes(label = 'year'), position = positions.position_nudge(0,1)) + stat_smooth(aes(x = 'barrels', y = 'crude.rate'), color = 'blue', method = 'lm', se = False) + labs(title = "Crude Rate versus Beer Production", x = "Ten Thousands of Barrels", y = "Deaths per Million") + annotate("text", x = 18250, y = 65, label = "R2:" + str(round(r2, 3)))) g.save("figures/allBeer_crudeRate_lm_p9.png") g2 = (ggplot(beer, aes(x = 'year', y = 'barrels')) + geom_point() + labs(title = "National Beer Production 2008-2015", x = "Ten Thousands of Barrels", y = "Year")) g2.save("figures/allBeer_years_p9.png") g3 = (ggplot(beer, aes(x = 'year', y = 'crude.rate')) + geom_point() + labs(title = "National Liver Disease Mortality 2008-2015", x = "Year", y = "Mortality per Million"))
g = (p9.ggplot( published_date_distances, p9.aes(x="factor(version_count)", y="time_to_published"), ) + p9.geom_boxplot(fill="#a6cee3") + p9.geom_line( mapping=p9.aes(x="version_count", y="time_to_published"), stat="smooth", method="lm", linetype="dashed", se=False, alpha=1, size=0.7, inherit_aes=False, ) + p9.scale_y_timedelta(labels=timedelta_format("d")) + p9.annotate( "text", x=9, y=timedelta(days=1470), label=f"Y={results_2.slope:.2f}*X+{results_2.intercept:.2f}", ) + p9.labs(x="# of Preprint Versions", y="Time Elapsed Until Preprint is Published") + p9.theme_seaborn( context="paper", style="ticks", font="Arial", font_scale=1.3)) # g.save("output/version_count_vs_publication_time.svg", dpi=500) # g.save("output/version_count_vs_publication_time.png", dpi=500) print(g) plt.figure(figsize=(8, 5)) g = sns.boxenplot( x="version_count", y="days_to_published", data=published_date_distances, scale="linear", palette="YlGnBu",
published_date_distances["version_count"].tolist()) g = (p9.ggplot(published_date_distances, p9.aes(x="version_count", y="doc_distances")) + p9.geom_boxplot(fill="#b2df8a") + p9.geom_line( data=pd.DataFrame( dict( version_count=np.arange(1, 13), doc_distances=np.arange(1, 13) * 0.02669 + 0.8697, )), linetype="dashed", color="#1f78b4", size=1, ) + p9.annotate( "text", label=f"y={results.slope:0.4f}*X + {results.intercept:0.4f}", x=9, y=7.5, size=13, color="#1f78b4", ) + p9.labs( x="# of Preprint Versions", y="Euclidean Distance of Preprint-Published Versions", ) + p9.theme_seaborn(style="white", context="notebook")) g.save("output/version_count_doc_distances.svg") g.save("output/version_count_doc_distances.png", dpi=600) print(g) # Overall, preprints change with each new version; however, based on the magnitude of the slope I'd argue that these changes are minor compared to substantial changes (~6 distance units) # # Output published dates to Excel # Reviewer asked if manually pursuing preprints that take longer to publish would produce any interesting results. Great question, but not enough time to go into that; however, providing a supplementary file for others to look into could provide an in depth answer.
def plot_pointgraph( plot_df, x_axis_label, left_arrow_label, right_arrow_label, left_arrow_start=-0.5, left_arrow_height=38.5, right_arrow_start=0.5, right_arrow_height=1.5, arrow_length=2, left_arrow_label_x=-1.5, left_arrow_label_y=-1.5, right_arrow_label_x=-1.5, right_arrow_label_y=-1.5, limits=(-3, 3), ): """ This function is designed to plot the an errorbar graph to show each token's odd ratio. The main idea for this graph is to show which corpora a token is enriched Args: plot_df - the data frame to plot, x_axis_label - the label of the x axis, left_arrow_label - the label for the left arrow, right_arrow_label - the label for the right arrow, left_arrow_start - the start of the left arrow to be plotted left_arrow_height - the height at which the arrow needs to be plotted right_arrow_start - the start of the right arrow to be plotted right_arrow_height - - the height at which the arrow needs to be plotted arrow_length - the length of the arrow left_arrow_label_x - the x axis position for the label of the left arrow left_arrow_label_y - the y axis position for the label of the left arrow right_arrow_label_x - the x axis position for the label of the right arrow right_arrow_label_y - the y axis position for the label of the right arrow limits=(-3,3) """ graph = (p9.ggplot( plot_df.assign(lemma=lambda x: pd.Categorical(x.lemma.tolist())), p9.aes( y="lemma", xmin="lower_odds", x="odds_ratio", xmax="upper_odds", yend="lemma", ), ) + p9.geom_errorbarh(color="#253494") + p9.scale_y_discrete(limits=( plot_df.sort_values("odds_ratio", ascending=True).lemma.tolist())) + p9.scale_x_continuous(limits=limits) + p9.geom_vline(p9.aes(xintercept=0), linetype="--", color="grey") + p9.annotate( "segment", x=left_arrow_start, xend=left_arrow_start - arrow_length, y=left_arrow_height, yend=left_arrow_height, colour="black", size=0.5, alpha=1, arrow=p9.arrow(length=0.1), ) + p9.annotate( "text", label=left_arrow_label, x=left_arrow_label_x, y=left_arrow_label_y, size=12, alpha=0.7, ) + p9.annotate( "segment", x=right_arrow_start, xend=right_arrow_start + arrow_length, y=right_arrow_height, yend=right_arrow_height, colour="black", size=0.5, alpha=1, arrow=p9.arrow(length=0.1), ) + p9.annotate( "text", label=right_arrow_label, x=right_arrow_label_x, y=right_arrow_label_y, size=12, alpha=0.7, ) + p9.theme_seaborn( context="paper", style="ticks", font_scale=1, font="Arial") + p9.theme( figure_size=(11, 8.5), panel_grid_minor=p9.element_blank(), text=p9.element_text(size=12), ) + p9.labs(y=None, x=x_axis_label)) return graph
return pd.read_stata( "https://raw.github.com/scunning1975/mixtape/master/" + file) np.random.seed(1) tb = pd.DataFrame({ 'x': np.random.normal(size=10000), 'u': np.random.normal(size=10000) }) tb['y'] = 5.5 * tb['x'].values + 12 * tb['u'].values reg_tb = sm.OLS.from_formula('y ~ x', data=tb).fit() reg_tb.summary() tb['yhat1'] = reg_tb.predict(tb) tb['yhat2'] = 0.1114 + 5.6887 * tb['x'] tb['uhat1'] = reg_tb.resid tb['uhat2'] = tb['y'] - tb['yhat2'] tb.describe() p.ggplot(tb, p.aes(x='x', y='y')) +\ p.ggtitle("OLS Regression Line") +\ p.geom_point(size = 0.05, color = "black", alpha = 0.5) +\ p.geom_smooth(p.aes(x='x', y='y'), method = "lm", color = "black") +\ p.annotate("text", x = -1.5, y = 30, color = "red", label = "Intercept = {}".format(-0.0732608)) +\ p.annotate("text", x = 1.5, y = -30, color = "blue", label = "Slope = {}".format(5.685033))
facet_wrap('~dir') + theme_bw()) g.save(filename="./num_cont_graphs/sim_fwd_rev2.png", format="png", width=8, height=4, units='in', verbose=False) eq = out[out.time == max(out.time)] g = (ggplot(eq) + aes(x='signal', y=response, color='dir') + labs(x="$B_{tot}$", y="$[S^{**}]$", color="") + geom_path(size=2, alpha=0.5) + geom_point(color="black") + theme_bw() + geom_point(color="black") + annotate("point", x=plot_specifications[2][0][0], y=plot_specifications[2][0][1], colour="red", shape="*", size=3.5) + annotate("text", x=plot_specifications[2][0][0], y=plot_specifications[2][0][1], label=plot_specifications[2][0][2])) # + annotate("point", x=plot_specifications[2][1][0], y=plot_specifications[2][1][1], colour="red", shape="*", # size=3.5) # + annotate("text", x=plot_specifications[2][1][0], y=plot_specifications[2][1][1], # label=plot_specifications[2][1][2])) g.save(filename="./num_cont_graphs/sim_bif_diag2.png", format="png", width=6, height=4, units='in',
def plot_scattermat(df, var=None): r"""Create a scatterplot matrix Create a scatterplot matrix. Often used to visualize a design (set of inputs points) before evaluating the functions. Usually called as a dispatch from plot_auto(). Args: var (list of strings): Variables to plot Returns: ggplot: Scatterplot matrix Examples: >>> import grama as gr >>> import matplotlib.pyplot as plt >>> from grama.models import make_cantilever_beam >>> md = make_cantilever_beam() >>> ## Dispatch from autoplotter >>> ( >>> md >>> >> gr.ev_sample(n=100, df_det="nom", skip=True) >>> >> gr.pt_auto() >>> ) >>> ## Re-create plot without metadata >>> ( >>> md >>> >> gr.ev_sample(n=100, df_det="nom") >>> >> gr.pt_scattermat(var=md.var) >>> ) """ if var is None: raise ValueError("Must provide input columns list as keyword var") ## Define helpers labels_blank = lambda v: [""] * len(v) breaks_min = lambda lims: (lims[0], 0.5 * (lims[0] + lims[1]), lims[1]) ## Make blank figure fig = ( df >> ggplot() + geom_blank() + theme_void() ).draw(show=False) gs = gridspec.GridSpec(len(var), len(var)) for i, v1 in enumerate(var): for j, v2 in enumerate(var): ax = fig.add_subplot(gs[i, j]) ## Switch labels if j == 0: labels_y = _sci_format else: labels_y = labels_blank if i == len(var) - 1: labels_x = _sci_format else: labels_x = labels_blank ## Density if i == j: xmid = 0.5 * ( df[v1].min() + df[v1].max() ) p = ( df >> ggplot(aes(v1)) + geom_density() + scale_x_continuous( breaks=breaks_min, labels=labels_x, ) + scale_y_continuous( breaks=breaks_min, labels=labels_y, ) + annotate( "label", x=xmid, y=0, label=v1, va="bottom", ) + theme_minimal() + labs(title=v1) ) ## Scatterplot else: p = ( df >> ggplot(aes(v2, v1)) + geom_point() + scale_x_continuous( breaks=breaks_min, labels=labels_x, ) + scale_y_continuous( breaks=breaks_min, labels=labels_y, ) + theme_minimal() + theme( axis_title=element_text(va="top", size=12), ) ) _ = p._draw_using_figure(fig, [ax]) ## Plot # NB Returning the figure causes a "double plot" in Jupyter.... fig.show()