相对R,python在图形可视化相对弱化一些,考虑python其他强大功能,逐步学习python实现图的一些代码,学习DataCharm公众号的柱状图后,实现效果如下:
实现代码如下:
import numpy as npimport matplotlib.pyplot as plt#数据labels = ['L1', 'L2', 'L3', 'L4', 'L5']data_a = [20, 34, 30, 35, 27]data_b = [25, 32, 34, 20, 25]data_c = [12, 20, 24, 17, 16]#画图x = np.arange(len(labels))fig,ax = plt.subplots(figsize=(5,3),dpi=300)bar_a = ax.bar(x-width, data_a,width,label='category_A',color='#130074',ec='black',lw=.5)bar_b = ax.bar(x, data_b, width,label='category_B',color='#CB181B',ec='black',lw=.5)bar_c = ax.bar(x+width, data_c,width,label='category_C',color='#008B45',ec='black',lw=.5)#对图的样式做调整ax.set_xticks(x)ax.set_xticklabels(labels,size=10)ax.set_ylim(bottom=0,top=40)ax.set_yticks(np.arange(0, 50, step=5))for spine in ['top','right']: ax.spines[spine].set_color('none')ax.legend(fontsize=7,frameon=False)text_font = {'size':'14','weight':'bold','color':'black'}ax.text(0.03,.93,"(A)",transform = ax.transAxes,fontdict=text_font,zorder=4)ax.text(.87,-.10,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 5,color='black',fontweight='bold',family='DejaVu Sans')
逐步喜欢上了用python来绘制科研用图