前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python-matplotlib 商业图表绘制02

Python-matplotlib 商业图表绘制02

作者头像
DataCharm
发布2021-02-22 15:09:30
3350
发布2021-02-22 15:09:30
举报

01. 引言

Python-matplotlib商业图表绘制的第二篇教程也已经推出,本期的推文主要涉及到文本、annotate()、散点以及颜色搭配等内容的讲解,话不多说,直接上教程

02. 数据处理

本期的数据属于比较简单的那种,数据和具体的颜色设置如下:

颜色设置如下:

代码语言:javascript
复制
#颜色字典
color = ("#008FD5", "#FC4F30", "#E5AE38", "#6D904F","#8B8B8B", "#810F7C" )
data = artist_02.data.to_list()
data_color = dict(zip(data,color))
data_color

03. 数据可视化设计

可视化内容代码具体如下:

代码语言:javascript
复制
#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)循环设置散点及颜色,如下:

代码语言:javascript
复制
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()方法添加了"指引"指标

代码语言:javascript
复制
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设置

由于文本我们需要使用左对齐或者右对齐,这里我们分别设置:

代码语言:javascript
复制
ha='left', va='center' 
代码语言:javascript
复制
 ha='right', va='center'

此外,在文本字符串中,我们还设置了换号符号(\n):

代码语言:javascript
复制
' the text can be displayed another\npositionxytext'

最终绘制的效果如如下:

(代码中生产的效果要更好点哦

)

04. 总结

本期推文主要涉及的可视化设计技巧不多,但也是定制化绘制中比较常用的方法,希望小伙伴们可以掌握哦,特别是ax.annotate()方法,可以设计出很多“很炫”的可视化作品。能力有限,如有错误的地方可以后台留言或加群讨论,此外,此类数据较简单,我也会第一时间分享到群里,期待你的加入

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 01. 引言
  • 02. 数据处理
  • 03. 数据可视化设计
  • 04. 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档