Python-matplotlib商业图表绘制的第二篇教程也已经推出,本期的推文主要涉及到文本、annotate()、散点以及颜色搭配等内容的讲解,话不多说,直接上教程
02. 数据处理
本期的数据属于比较简单的那种,数据和具体的颜色设置如下:
颜色设置如下:
#颜色字典color = ("#008FD5", "#FC4F30", "#E5AE38", "#6D904F","#8B8B8B", "#810F7C" )data = artist_02.data.to_list()data_color = dict(zip(data,color))data_color
03. 数据可视化设计
可视化内容代码具体如下:
#fig,ax = plt.subplots(figsize=(4,6),dpi=400,facecolor='#CACACA',edgecolor='#CACACA')fig,ax = plt.subplots(figsize=(4,6),dpi=400)#ax.set_facecolor('#CACACA')for y,text in zip(artist_02['data'].values,artist_02['year'].values):scatter_bottom = ax.scatter(.5,y,s=1300,color='white',zorder=0)scatter = ax.scatter(.5,y,s=1000,ec='k',lw=.3,zorder=1)scatter_top = ax.scatter(.5,y,s=800,color='white',ec='k',lw=.3,zorder=2)year = ax.text(.5,y,text,ha='center', va='center',fontsize = 8,color='black',fontweight='bold')#定制化绘制ax.set_ylim(bottom=-1,top=11)ax.grid(False)#添加文本#题目部分ax.text(.49,10.9,'TIMELINE', ha='center', va='center',fontsize = 13,color='gray',fontweight='light')ax.text(.49,10.5,'INFOGRAPHICS', ha='center', va='center',fontsize = 7,color='gray',fontweight='light')left_data = [0,4,8]for y_text in left_data:ax.annotate('',xy=(.496,y_text),xytext=(.492,y_text),ha="center",va="center",arrowprops=dict(arrowstyle="wedge,tail_width=0.1",fc= data_color[y_text],ec=data_color[y_text]))ax.text(.484,y_text,'LOREM IPSUM',ha='left', va='center',fontsize = 8,color=data_color[y_text],weight='bold')ax.text(.484,y_text-.8,'Optionally, the text can be displayed in another\npositionxytext. An arrow pointing from the text\nto theannotated point xy can then be added by\ndefining arrowprops.',ha='left', va='center',fontsize = 5,color='k')right_data = [2,6,10]for y_text in right_data:#这里是和left_data 不一样的地方,因为指向不同ax.annotate('',xy=(.504,y_text),xytext=(.508,y_text),ha="center",va="center",arrowprops=dict(arrowstyle="wedge,tail_width=0.1",fc= data_color[y_text],ec=data_color[y_text]))ax.text(.516,y_text,'LOREM IPSUM',ha='right', va='center',fontsize = 8,color=data_color[y_text],weight='bold')ax.text(.516,y_text-.8,'Optionally, the text can be displayed in another\npositionxytext. An arrow pointing from the text\nto theannotated point xy can then be added by\ndefining arrowprops.',ha='right', va='center',fontsize = 5,color='k')#去除刻度等信息ax.axis('off')ax.text(.96,.0,'\nVisualization by DataCharm',transform = ax.transAxes,ha='center', va='center',fontsize = 4,color='black')plt.savefig(r'F:\DataCharm\商业艺术图表仿制\artist_02.pdf',width=4,height=6,dpi=900,bbox_inches='tight')
(1)循环设置散点及颜色,如下:
for y,text in zip(artist_02['data'].values,artist_02['year'].values):scatter_bottom = ax.scatter(.5,y,s=1300,color='white',zorder=0)scatter = ax.scatter(.5,y,s=1000,ec='k',lw=.3,zorder=1)scatter_top = ax.scatter(.5,y,s=800,color='white',ec='k',lw=.3,zorder=2)year = ax.text(.5,y,text,ha='center', va='center',fontsize = 8,color='black',fontweight='bold')
颜色设置还是使用了颜色字典设置,此外,这里散点的x位置我们设置固定,y位置为具体的data数据,文本内容也为year内容。
(2)使用ax.annotate()方法添加了"指引"指标
left_data = [0,4,8]for y_text in left_data:ax.annotate('',xy=(.496,y_text),xytext=(.492,y_text),ha="center",va="center",arrowprops=dict(arrowstyle="wedge,tail_width=0.1",fc= data_color[y_text],ec=data_color[y_text]))ax.text(.484,y_text,'LOREM IPSUM',ha='left', va='center',fontsize = 8,color=data_color[y_text],weight='bold')ax.text(.484,y_text-.8,'Optionally, the text can be displayed in another\npositionxytext. An arrow pointing from the text\nto theannotated point xy can then be added by\ndefining arrowprops.',ha='left', va='center',fontsize = 5,color='k')
由于左右位置的不同,ax.annotate()中xy和xytext设置有所不同,这里主要根据和ax.annotate()指向方式(wedge)的不同设置。颜色还是使用了颜色字典定制化设计。
(3)文本的va和ha设置
由于文本我们需要使用左对齐或者右对齐,这里我们分别设置:
ha='left', va='center'
ha='right', va='center'
此外,在文本字符串中,我们还设置了换号符号(\n):
' the text can be displayed another\npositionxytext'
最终绘制的效果如如下:
(代码中生产的效果要更好点哦
)
04. 总结
本期推文主要涉及的可视化设计技巧不多,但也是定制化绘制中比较常用的方法,希望小伙伴们可以掌握哦,特别是ax.annotate()方法,可以设计出很多“很炫”的可视化作品。
近期文章
“
分享
”和“
在看
”是更好的支持!