Python-joypy和 R-ggridges 峰巒圖制作

Python-joypy 制作

Python 制作峰巒圖有直接的第三方庫(kù)joypy進(jìn)行繪制,該庫(kù)可以直接通過(guò)pip安裝??梢暬a如下:

importmatplotlib.pyplotaspltplt.rcParams['font.family'] = ['Times New Roman']colors = ['#791E94','#58C9B9','#519D9E','#D1B6E1']fig,axs = joypy.joyplot(data_ed, by="source",fill=True, legend=True,alpha=.8,? ? ? ? ? ? ? ? ? ? ? ? range_style='own',xlabelsize=22,ylabelsize=22,? ? ? ? ? ? ? ? ? ? ? ? grid='both', linewidth=.8,linecolor='k', figsize=(12,6),color=colors,? ? ? ? ? ? ? ? ? ? ? )ax = plt.gca()#設(shè)置x刻度為時(shí)間形式x = np.arange(6)xlabel=['8-21','8-28','9-4','9-11','9-18','9-25']ax.set_xlim(left=-.5,right=5.5)ax.set_xticks(x)ax.set_xticklabels(xlabel)ax.text(.47,1.1,"Joyplot plots of media shares (TV, Online News and Google Trends)",? ? ? ? transform = ax.transAxes,ha='center', va='center',fontsize =25,color='black')ax.text(.5,1.03,"Python Joyplot Test",? ? ? ? transform = ax.transAxes,ha='center', va='center',fontsize =15,color='black')ax.text(.90,-.11,'\nVisualization by DataCharm',transform = ax.transAxes,? ? ? ? ha='center', va='center',fontsize =12,color='black')plt.savefig(r'F:\DataCharm\Artist_charts_make_python_R\joyplots\Joyplot_python.png',? ? ? ? ? ? width=7,height=5,dpi=900,bbox_inches='tight')

可視化結(jié)果如下:

R-ggridges 繪制

借助于R語(yǔ)言豐富且強(qiáng)大的第三方繪圖包,在應(yīng)對(duì)不同類(lèi)型圖表時(shí),機(jī)會(huì)都會(huì)有對(duì)應(yīng)的包進(jìn)行繪制。本次就使用ggridges包(https://wilkelab.org/ggridges/)進(jìn)行峰巒圖的繪制。官網(wǎng)的例子如下:

ggplot(lincoln_weather, aes(x =`Mean Temperature [F]`, y = Month, fill = stat(x))) +geom_density_ridges_gradient(scale =3, rel_min_height =0.01, gradient_lwd =1.) +scale_x_continuous(expand = c(0,0)) +scale_y_discrete(expand = expand_scale(mult = c(0.01,0.25))) +scale_fill_viridis_c(name ="Temp. [F]", option ="C") +labs(? ? title ='Temperatures in Lincoln NE',? ? subtitle ='Mean temperatures (Fahrenheit) by month for 2016') +theme_ridges(font_size =13, grid = TRUE) +theme(axis.title.y = element_blank())

結(jié)果如下:

這里我們沒(méi)有使用 geom_density_ridges_gradient()進(jìn)行繪制,使用了 geom_ridgeline() 進(jìn)行類(lèi)似于 山脊線(xiàn) 圖的繪制。

繪制代碼如下:

library(ggthemes)library(hrbrthemes)plot<-ggplot(all_data,aes(x=date,y=source))+geom_ridgeline(aes(height=value,fill=factor(hurricane)),size=0.1,scale=0.8,alpha=0.8)+labs(title="Ridgeline plots of media shares (TV, Online News and Google Trends)",subtitle="ggridges ridgeline plot test",caption="Visualization by DataCharm",y=NULL,x=NULL)+scale_x_date(expand=c(0,0))+scale_fill_manual(values=c('#791E94','#58C9B9','#D1B6E1','#519D9E'),name="Hurricane")+theme_ipsum()+theme(text=element_text(family='Poppins',face='bold'),axis.text.y=element_text(vjust=-2))plot

可視化結(jié)果如下:

上述所涉及到的函數(shù)都是基本,在熟悉ggpot2 繪圖體系后可以輕松理解。

有什么問(wèn)題歡迎大家在評(píng)論區(qū)留言討論,還沒(méi)有關(guān)注小編的關(guān)注一下哦,小編每天都會(huì)給大家分享干貨知識(shí),帶大家學(xué)習(xí)Python,一起走上人生的巔峰。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容