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

Python-matplotlib 商业图表仿制第7弹

作者头像
DataCharm
发布2021-02-22 12:01:31
2900
发布2021-02-22 12:01:31
举报

虽然小伙伴们喜欢空间绘图方面的居多(毕竟这方面的小伙伴居多),但商业图表的绘制也不能放下哦!本期就推出一篇商业图表的仿制教程。主要涉及内容如下:

  • Python-matplotlib 散点图绘制
  • 文本条件添加

Python-matplotlib 散点图绘制

本篇推文的原始图片还是来自于PIIE网站的一篇文章配图,文章的插图如下:

配图是真的没话说,特别是配色(这种配色我做一个Excel的颜色主题xml文件,如果需求多,可以看下这篇文章代码绘图繁琐量多?!Excel 了解一下)

所使用的数据形式如下:

接下来,我们给出直接仿制的代码,如下:

代码语言:javascript
复制
#开始绘图

fig,ax = plt.subplots(figsize=(3,3),dpi=400,facecolor='white',edgecolor='white')
ax.set_facecolor('white')

ax.hlines(y=data_02.index, xmin=0, xmax=data_02['Imp_gr_from_ROW'], color="#3D71A0",lw=.8)
#绘制散点
for i,j,text in zip(data_02.index,data_02['Imp_gr_from_ROW'],data_02['Imp_gr_from_ROW']):
    ax.scatter(j,i,s=6,color='#3D71A0',zorder=2)
    ax.text(j,i-.2,s=int(round(text*100,2)),size=3.5,color='#3D71A0',ha='left',va='bottom',fontweight='bold')

ax.hlines(y=data_02.index+.03, xmin=0, xmax=data_02['Imp_gr_from_China'], color="#B70050",lw=.8)
#绘制散点
for i,j,text in zip(data_02.index,data_02['Imp_gr_from_China'],data_02['Imp_gr_from_China']*100):
    ax.scatter(j,i+.03,s=6,color='#B70050',zorder=2)
    ax.text(j,i-.2,s=int(round(text,2)),size=3.5,color='#B70050',ha='right',va='bottom',fontweight='bold')
ax.set_xlim(left=-1,right=1)
#绘制竖线
ax.plot([0,0],[0,15],color='k',lw=.3)
#绘制竖线上散点
for i in data_02.index:
    ax.scatter(0,i,color='#172A3A',ec='k',s=2,zorder=3)
for index,i in enumerate(zip(data_02.index,data_02['Imp_gr_from_China'],data_02['Product'])):
    if i[1] > 0 :
        ax.text(0-.05,index,i[2],color='#3D71A0',size=3,ha='right',va='center',fontweight='bold')
    else:
        ax.text(i[1]-.05,index,i[2],color='#3D71A0',size=3,ha='right',va='center',fontweight='bold')
ax.axis('off')

#添加图例
ax.plot([],[],marker='o', label='China',color="#B70050")
ax.plot([],[],marker='o', label='Rest of world',color="#3D71A0")
legend_text = {'size':3,
               'weight':'bold'}
ax.legend(loc='upper left',frameon=False,markerscale=.3,ncol=2,prop=legend_text,columnspacing=.5,
          bbox_to_anchor=(-.1, 1.05))
ax.invert_yaxis()  #y轴反向
#添加标题
#添加标题
ax.text(-.1,1.15,"The trade war resulted in fewer American purchases of critical\nmedical products from China in 2019",
       transform = ax.transAxes,color='k',ha='left',va='center',size=5.5,fontweight='extra bold')
ax.text(-.1,1.07,"US import value growth rate, 2017-2019, percent",
       transform = ax.transAxes,color='#3D71A0',ha='left',va='center',size=3,fontweight='extra bold')

ax.text(.94,.01,'\nVisualization by DataCharm',transform = ax.transAxes,
        ha='center', va='center',fontsize = 3,color='black')
plt.savefig(r"F:\DataCharm\商业艺术图表仿制\PIEE_Charts\Trump's trade policy is hampering the US fight against COVID-19\fig01.png",
            width=3,height=4,dpi=900,bbox_inches='tight',facecolor='white')

最终可视化结果如下:

文本条件添加

本推文的绘图教程中,文本的添加还是值得学习一下的,代码如下:

代码语言:javascript
复制
#绘制竖线上散点
for i in data_02.index:
    ax.scatter(0,i,color='#172A3A',ec='k',s=2,zorder=3)
for index,i in enumerate(zip(data_02.index,data_02['Imp_gr_from_China'],data_02['Product'])):
    if i[1] > 0 :
        ax.text(0-.05,index,i[2],color='#3D71A0',size=3,ha='right',va='center',fontweight='bold')
    else:
        ax.text(i[1]-.05,index,i[2],color='#3D71A0',size=3,ha='right',va='center',fontweight='bold')

上述代码就可以实现可视化结果中左侧文本的设置效果;

此外,由于matplotlib 无法实现向ggplot的绘图元素映射,导致有时绘制图例就麻烦些,但可以使用以下方法单独绘制图例:

代码语言:javascript
复制
#添加图例
ax.plot([],[],marker='o', label='China',color="#B70050")
ax.plot([],[],marker='o', label='Rest of world',color="#3D71A0")
legend_text = {'size':3,
               'weight':'bold'}
ax.legend(loc='upper left',frameon=False,markerscale=.3,ncol=2,prop=legend_text,columnspacing=.5,
          bbox_to_anchor=(-.1, 1.05))

以上,就是本期商业图表绘制的全部内容,当然,如果后面可能我会使用R-ggplot2 进行仿制。

总结

本期推文我们进行商业图表第7弹的绘制,学习了散点图系列的绘制方法,此外,颜色的配置也是值得参考和学习的。大家可以直接关注公号:DataCharm,直接获取EXCEL颜色主题xml文件。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python-matplotlib 散点图绘制
  • 文本条件添加
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档