def test_limits(): p = (ggplot(df, aes('x')) + stat_function(fun=np.cos, size=2, color='blue', arrow=arrow(ends='first')) + stat_function(fun=np.cos, xlim=(10, 20), size=2, color='red', arrow=arrow(ends='last'))) assert p == 'limits'
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_arrow(): p = (ggplot(df, aes('x', 'y', xend='xend', yend='yend')) + geom_segment(aes('x+2', xend='xend+2'), arrow=arrow(), size=2) + geom_segment( aes('x+4', xend='xend+4'), arrow=arrow(ends='first'), size=2) + geom_segment( aes('x+6', xend='xend+6'), arrow=arrow(ends='both'), size=2)) assert p == 'arrow'
def test_arrow(): p = (ggplot(df, aes('x', 'y')) + geom_path(size=2, arrow=arrow(ends='both', type='closed')) + geom_path(aes(y='y+2'), color='red', size=2, arrow=arrow(angle=60, length=1, ends='first')) + geom_path(aes(y='y+4'), color='blue', size=2, arrow=arrow(length=1))) assert p == 'arrow'
def test_arrow(): p = (ggplot(df, aes('x', 'y', xend='xend', yend='yend')) + geom_segment(aes('x+2', xend='xend+2'), arrow=arrow(), size=2) + geom_segment(aes('x+4', xend='xend+4'), arrow=arrow(ends='first'), size=2) + geom_segment(aes('x+6', xend='xend+6'), arrow=arrow(ends='both'), size=2) ) assert p == 'arrow'
def test_arrow_facets(): df = pd.DataFrame({ 'x': [1, 3, 2, 4], 'y': [10, 9, 10, 9], 'z': ['a', 'a', 'b', 'b'] }) p = (ggplot(df, aes('x', 'y')) + geom_path(size=2, arrow=arrow(length=.25)) + facet_grid('~ z')) assert p == 'arrow_facets'
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, \ yend = sol.Rpa[k].imag + sol.Vpaa[k].imag * VEL_SCALE),\ colour='orange', arrow=arrow()) + # Point C # VELOCITIES TEXTS (you may comment if you wish to remove acceleration informations) # inputting text between '$ $' makes plotnine produce beautiful LaTeX text # positions of the velocities texts may be altered in case the plot gets hard to read annotate("text", x = sol.Rba[k].real-1, y = sol.Rba[k].imag-25, label = f'${np.absolute(sol.Vba[k])/1000:.2f}~m/s$', colour='orange') + annotate("text", x = sol.Ra[k].real, y = sol.Ra[k].imag+20, label = f'${np.absolute(sol.Va[k])/1000:.2f}~m/s$', colour='orange') + annotate("text", x = sol.Rpa[k].real-10, y = sol.Rpa[k].imag-10, label = f'${np.absolute(sol.Vpaa[k])/1000:.2f}~m/s$', colour='orange') + # ACCELERATIONS ARROWS (you may remove if you wish to remove acceleration informations)
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_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
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, arrow=p9.arrow(length=0.1), ) + p9.annotate( "text", label="PMC Enriched", x=-1.5, y=38.5, size=18, alpha=0.7) + p9.theme_seaborn(
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, arrow=p9.arrow(length=0.1), ) + p9.annotate( "text", label="PMC Enriched", x=-1.5, y=38.5, size=14, alpha=0.7) + p9.theme_seaborn(