本期还是推出Python-matplotlib "小清新"商业图表的绘制推文,在发现ax.plot()绘图函数的多类别图表功能后,经过不断和点、文本等尝试搭配后,所能构建的图表也就多了起来,下面就直接上教程。
本期我们构建一组简单的时间变化图表数据,当然还有我们常用的颜色字典构建。具体如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
test_dict = {'x':[0,5,10,15,20,25,30],'year':['1990','1995','2000','2005','2010','2015','2020']}
artist_04 = pd.DataFrame(test_dict)
color = ("#F5A34D", "#F26F77", "#48AEBA", "#A3BA74","#958298", "#B88357",'#608CB1' )
data = artist_04['x'].to_list()
data_color = dict(zip(data,color))
data_color
颜色字典如下:
{0: '#F5A34D',
5: '#F26F77',
10: '#48AEBA',
15: '#A3BA74',
20: '#958298',
25: '#B88357',
30: '#608CB1'}
详细绘图代码如下:
fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='#FFF7F2',edgecolor='#FFF7F2')
ax.set_facecolor('#FFF7F2')
#绘制中间横线
ax.set_ylim(-.5,1.5)
#绘制具有端点形状的直线
ax.plot([-3,38],[.5,.5],"-o",lw=1.2,color='gray',markerfacecolor="w",mec='gray',ms=5,
markeredgewidth=1.,zorder=1)
#分上下情况绘制点、线混合图形
for x in [0,10,20,30]:
#绘制横线上的散点,颜色不同
ax.scatter(x,.5,s=120,color=data_color[x],zorder=2)
#绘制叠加在颜色散点之上的散点,颜色为白色
ax.scatter(x,.5,s=50,zorder=3,color='white')
#绘制散点和圆柱之间的连接线,端点为圆点
ax.plot([x,x],[.5,.5+.6],"-o",color=data_color[x],lw=.6,mfc="w",ms=5,mew=1.2,zorder=3)
#绘制横置圆柱图
ax.plot([x,x+7.5],[.5+.6,.5+.6],lw=15,color=data_color[x],solid_capstyle='round',zorder=1)
ax.scatter(x,.5+.6,s=80,zorder=3,color='white')
ax.text(x+4,.5+.6,s='Lorem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center',
va='center')
#添加年份
ax.text(x-1.4,.5+.2,s=artist_04.loc[artist_04['x']==x,'year'].values[0],color='#686866',fontsize=12,
fontweight='bold',rotation=90)
#添加描述文字
ax.text(x+.5,.5+.3,'Optionally, the text can bedisplayed\n in anotherpositionxytext.Anarrow\npointingfrom the text totheannotated\npoint xy canthen beaddedbydefining\narrowprops.',
ha='left', va='center',fontsize = 4,color='gray')
for x in [5,15,25]:
#绘制横线上的散点,颜色不同
ax.scatter(x,.5,s=120,color=data_color[x],zorder=2)
#绘制叠加在颜色散点之上的散点,颜色为白色
ax.scatter(x,.5,s=50,zorder=3,color='white')
#绘制散点和圆柱之间的连接线,端点为圆点
ax.plot([x,x],[.5,.5-.6],"-o",color=data_color[x],lw=.6,mfc="w",ms=5,mew=1.2,zorder=3)
#绘制横置圆柱图
ax.plot([x,x+7.5],[.5-.6,.5-.6],lw=15,color=data_color[x],solid_capstyle='round',zorder=1)
ax.scatter(x,.5-.6,s=80,zorder=3,color='white')
ax.text(x+4,.5-.6,s='Lorem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center',
va='center')
#添加描述文字
ax.text(x+.5,.5-.3,'Optionally, the text can bedisplayed\n in anotherpositionxytext.Anarrow\npointingfrom the text totheannotated\npoint xy canthen beaddedbydefining\narrowprops.',
ha='left', va='center',fontsize = 4,color='gray')
#添加年份
ax.text(x-1.4,.5-.4,s=artist_04.loc[artist_04['x']==x,'year'].values[0],color='#686866',fontsize=12,
fontweight='bold',rotation=90)
#添加题目文本
ax.axis('off')
ax.text(.49,1.15,'\nTIMELINE INFOGRAPHICS',transform = ax.transAxes,
ha='center', va='center',fontsize = 20,color='gray',fontweight='light')
ax.text(.92,.00,'\nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 5,color='black')
plt.savefig(r'F:\DataCharm\商业艺术图表仿制\artist_04.png',width=8,height=4,
dpi=900,bbox_inches='tight',facecolor='#FFF7F2')
知识点:
(1)熟悉ax.plot()函数方法,其他参数设置不同对结果也不同。
(2)ax.scatter()绘制散点。
(3)ax.text()文本的灵活添加。
(4)颜色的合理选择。
结果图表如下:
(也有小伙伴提出这个图可能用PPT制作会更快,我也偷偷试了下
,咋感觉还没我这个方便呢
,可能使用工具的不同吧····别喷我啊
·),我们的目的是更好的熟悉matplotlib 的那些容易忽略却功能强大的绘图函数,使我们在绘制不同图表时节省时间,更好的完成绘制任务
)
这一期的推文教程整体不是很难哦,整体上还是对Matplotlib的部分绘图函数进行练习,下期准备写写matplotlib的ax.annotate()的应用,应该也可以 设计出不错的可视化作品吧
。当然大家也可以后台或者进群交流学习讨论啊。