matplotlib绘图颜色搭配

您所在的位置:网站首页 matplotlib画图颜色 matplotlib绘图颜色搭配

matplotlib绘图颜色搭配

2022-05-09 11:42| 来源: 网络整理| 查看: 265

01. 引言

色彩搭配对图表的第一印象至关重要,合理的设置对图表的颜值提升有着很大的帮助,本期推文结合一个具体例子对图表颜色搭配进行讲解。

02. 美化过程

2.1 默认图表

这里首先设置一组数据,使用matplotlib的默认绘制方法进行图表绘制,具体数据如下:

array([10, 25, 12, 15, 28, 13, 20, 26, 23, 40, 39, 35, 42, 31, 29, 16, 27], dtype=int64)

默认绘图代码如下:

fig,ax = plt.subplots(figsize=(10,5),dpi=200) x = np.arange(0,len(test_data),1) y = test_data['test'].values ax.plot(x,y) ax.scatter(x[-1],y[-1],s=200,lw=2,zorder=2) ax.set_title('Matplotlib Color Set exercise_01 \n',fontsize=20,color='black') ax.text(.85,-.1,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 8.5,color='black') plt.savefig(r'E:\Data_resourses\DataCharm 公众号\Python\商业图表绘制\color_set_region.png',width=7,height=4, dpi=900,bbox_inches='tight')

这里使用了ax.plot()和ax.scatter()进行了绘图,效果如下:

可以看出,图中无论折线、散点还是刻度等的设置都是比较粗糙的,和"美观"一点都挂不上钩。下面就从数据处理、填充面积以及色彩选择三个方面进行处理,来使这幅图变得高大上起来

2.2 数据插值

默认数据绘制的折线图可能不是那么的平滑,当然,这也是和我们选择较少的数据有关,要想达到平滑效果,需要对原始数据进行插值处理(Excel中选中图表右击,点击 设置数据系列格式,选择最后的平滑线  也能完成平滑处理)。python Scipy 包提供了interpolate模块可以实现对一维二维数据的插值处理,在对一维数据时,interp1d()提供了如nearest、quadradic等多种插值方式,详细内容大家可以查看官网啊。这里选择的插值方式为 "quadradic"方法,具体插值代码及绘图代码如下:

from scipy import interpolate fig,ax = plt.subplots(figsize=(10,5),dpi=200,) f = interpolate.interp1d(x,y,kind="quadratic")#采用quadratic插值处理 x_new = np.linspace(x.min(),x.max(),1000) y_new = f(x_new) #绘图 ax.plot(x_new,y_new,lw=3,zorder=1) ax.scatter(x[-1],y[-1],s=200,lw=2,zorder=2) ax.set_title('Matplotlib Color Set exercise_01 \n',fontsize=20,color='black') ax.text(.85,-.1,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 8.5,color='black') plt.savefig(r'E:\Data_resourses\DataCharm 公众号\Python\商业图表绘制\color_set_region_ed.png',width=7,height=4, dpi=900,bbox_inches='tight') plt.show()

结果如下:

2.3 面积填充

加上填充面积可以使得图表看起来不那么单一,而且········(好吧·····我编不下去了,我就是为了最后成图好看下而已)。添加如下代码即可添加填充面积效果。

ax.fill_between(x_new, y_new,alpha=.15,lw=.1,zorder=2) # 填充两条线间的颜色

这里设置填充面积的线边框宽度为0.1,会使得填充边框不那么明显。效果如下:

如果不设置线宽lw,图中红色圆圈内将会有明显的横线效果,影响美观。

2.4 颜色选择

记得好多小伙伴后台留言或者直接和我沟通 关于色彩搭配的设置,其实,我也是一名学习者,可能就是看的色彩搭配网站以及精美可视化作品比较多,还是那句话 多模仿 多练习 对比不同色系对同一幅图表所展示的效果,这是一个慢慢积累的过程,要是硬要我给建议,我也就多推几个可视化的色彩搭配网站给你们。下面就本期推文设置相关颜色,具体代码如下:

#对数据进行插值,使其润滑 #导入需要的库 from scipy import interpolate fig,ax = plt.subplots(figsize=(10,5),dpi=200,facecolor ='#16151C') ax.set_facecolor('#16151C') #插值处理 f = interpolate.interp1d(x,y,kind="quadratic")#采用quadratic插值处理 x_new = np.linspace(x.min(),x.max(),1000) y_new = f(x_new) #绘图 ax.plot(x_new,y_new,c="#12D3B0",lw=3,zorder=1) ax.scatter(x[-1],y[-1],s=200,c="#F1404B",ec='k',lw=1.5,zorder=2) ax.scatter(x[-1],y[-1],s=600,c="#F1404B",ec='none',lw=2,alpha=.4,zorder=1) ax.fill_between(x_new, y_new, color="#12D3B0",alpha=.15,ec="none",lw=.1,zorder=2) #填充两条线间的颜色 for spine in ['left','top', 'right', 'bottom']: ax.spines[spine].set_color("none") #进行精修 ax.grid(axis='y',color='white',lw=.5,alpha=.1,zorder=0) ax.tick_params(labelsize=13,labelcolor='white',grid_color='white',left=False,bottom=False) ax.set_ylim(bottom=0,top=50) ax.set_xlim(left=-0.5) ax.set_xticklabels(labels=['2011','2012','2013','2014','2015','2016','2017','2018','2019','2020']) ax.set_title('Matplotlib Color Set exercise_02 \n',fontsize=20,color='white') ax.text(.85,-.1,'\nVisualization by DataCharm',transform = ax.transAxes, ha='center', va='center',fontsize = 8.5,color='white') plt.savefig(r'E:\Data_resourses\DataCharm 公众号\Python\商业图表绘制\color_set_02.png',width=7,height=4, dpi=900,bbox_inches='tight',facecolor='#16151C') #ax.set_axisbelow(True) plt.show()

涉及的颜色设置代码如下:

fig,ax = plt.subplots(figsize=(10,5),dpi=200,facecolor ='#16151C') ax.set_facecolor('#16151C') ax.plot(x_new,y_new,c="#12D3B0",lw=3,zorder=1) ax.scatter(x[-1],y[-1],s=200,c="#F1404B",ec='k',lw=1.5,zorder=2) ax.scatter(x[-1],y[-1],s=600,c="#F1404B",ec='none',lw=2,alpha=.4,zorder=1) ax.fill_between(x_new, y_new, color="#12D3B0",alpha=.15,ec="none",lw=.1,zorder=2) plt.savefig(r'E:\Data_resourses\DataCharm 公众号\Python\商业图表绘制\color_set_02.png',width=7,height=4, dpi=900,bbox_inches='tight',facecolor='#16151C')

效果如下:

同时也可以设置如下效果:

●分析了10万条弹幕,发现歪嘴战神终极奥义!

●12000+字超详细 SQL 语法速成!

后台回复“入群”即可加入小z干货交流群

干货????



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3