01. 引言
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 anothernpositionxytext. An arrow pointing from the textnto theannotated point xy can then be added byndefining 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 anothernpositionxytext. An arrow pointing from the textnto theannotated point xy can then be added byndefining 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 anothernpositionxytext. An arrow pointing from the textnto theannotated point xy can then be added byndefining 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 anothernpositionxytext'
最终绘制的效果如如下:
(代码中生产的效果要更好点哦)
04. 总结
本期推文主要涉及的可视化设计技巧不多,但也是定制化绘制中比较常用的方法,希望小伙伴们可以掌握哦,特别是ax.annotate()方法,可以设计出很多“很炫”的可视化作品。
近期文章
Python网络爬虫与文本数据分析 rpy2库 | 在jupyter中调用R语言代码 tidytext | 耳目一新的R-style文本分析库 reticulate包 | 在Rmarkdown中调用Python代码 plydata库 | 数据操作管道操作符>> plotnine: Python版的ggplot2作图库 七夕礼物 | 全网最火的钉子绕线图制作教程 读完本文你就了解什么是文本分析 文本分析在经管领域中的应用概述 综述:文本分析在市场营销研究中的应用 plotnine: Python版的ggplot2作图库 小案例: Pandas的apply方法 stylecloud:简洁易用的词云库 用Python绘制近20年地方财政收入变迁史视频 Wow~70G上市公司定期报告数据集 漂亮~pandas可以无缝衔接Bokeh YelpDaset: 酒店管理类数据集10+G
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论