Python-joypy和 R-ggridges 峰峦图制作
source link: http://www.cnblogs.com/hhh188764/p/13774253.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
以下文章来源于DataCharm,作者 宁海涛
转载地址
https://www.zhihu.com/people/qi-shi-huan-hao-la-14/posts
Python-joypy 制作
Python 制作峰峦图有直接的第三方库joypy进行绘制,该库可以直接通过pip安装。可视化代码如下:
import matplotlib.pyplot as plt plt.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()#设置x刻度为时间形式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')
可视化结果如下:
关于 joypy库其他详细的参数设置,可以去官网(https://github.com/sbebo/joypy) 下载 Joyplot.ipynb 文件查看,最好查看所绘制数据的格式,有助于更好绘制峰峦图。
R-ggridges 绘制
借助于R语言丰富且强大的第三方绘图包,在应对不同类型图表时,机会都会有对应的包进行绘制。本次就使用ggridges包(https://wilkelab.org/ggridges/)进行峰峦图的绘制。官网的例子如下:
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())
结果如下:
这里我们没有使用 geom_density_ridges_gradient()进行绘制,使用了 geom_ridgeline() 进行类似于 山脊线 图的绘制。
绘制代码如下:
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
可视化结果如下:
上述所涉及到的函数都是基本,在熟悉ggpot2 绘图体系后可以轻松理解。更多有趣的可视化作品,大家可以去官网查看。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK