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

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

作者头像
DataCharm
发布2021-02-22 15:05:22
2000
发布2021-02-22 15:05:22
举报

今天我们就推出一篇商业图表的仿制,之前的学术图表(空间可视化方面)也是很受小伙伴的喜欢,我们要商业学术同时进行哦!毕竟,优秀的商业可视化图表能够更直观的体现数据可视化的魅力。话不多说,我们就直接上教程。

绘制图表介绍

这篇推文是关于中美贸易的文章配图(具体我们这里我们不多做介绍,我们只关注配图的美观设计),文章的插图如下:

配图是真的没话说,特别是配色(这种配色我后面会做一个Excel的配图颜色系会比这个多几个色系。大家如果需求多,我会分享出来的哦)

Python-matplotlib仿制

绘图数据

这里的数据处理步骤暂且不过多介绍,绘图数据如下

可视化绘制

我们直接上代码,大家不会的可以详细看代码中的注释:

代码语言:javascript
复制
china_color = ['#3D71A0','#B70050','#FD9717','#B6CBDF']
china_text = ['Aluminum waste and scrap','Pork','Fruits and nuts','Other']
us_color = ['#3D71A0','#B70050','#FD9717','#B6CBDF','#3DAE5E','#133831']
us_list = ['64%', '12%', '10%', '8%', '6%', '']
us_text = ['Aluminum','Long','Pipe/tube','Stainless','Flat',""]
barWidth = .6
lefts_china = 0
lefts_us = 0

fig,ax = plt.subplots(figsize=(8,4.5),dpi=200)

for bars, col,text,text2 in zip(china['2017 exports, in actual dollars'], china_color,china['percent%'],china_text):
    ax.barh(1, bars, left=lefts_china, color=col, height=barWidth,align='center')
    lefts_china += bars
    #大家注意这里:可以详细看一下
    if lefts_china == china['2017 exports, in actual dollars'][0]:
        ax.text(bars/2,1,text,color="k",fontweight='extra bold',size=13,ha='center',va='center')
        ax.text(bars/2,1.4,text2,color="k",fontweight='light',size=8,ha='center',va='center')
    else:
        ax.text(lefts_china-bars/2,1,text,color="k",fontweight='extra bold',size=13,ha='center',va='center')
        ax.text(lefts_china-bars/2,1.4,text2,color="k",fontweight='light',size=8,ha='center',va='center')
#添加暗黑色柱形图
ax.barh(.6,china['2017 exports, in actual dollars'].sum(),color='#172A39',height=.35,zorder=0)


for bars, col,text,text2 in zip(us['2017 imports, in actual dollars2'], us_color,us_list,us_text):
    ax.barh(3, bars, left=lefts_us, color=col, height=barWidth,align='center')
    lefts_us += bars
    if lefts_us == us['2017 imports, in actual dollars2'][0]:
        ax.text(bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center')
        ax.text(bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center')
    else:
        ax.text(lefts_us-bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center')
        ax.text(lefts_us-bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center')
ax.barh(2.6,us['2017 imports, in actual dollars2'].sum(),color='#172A39',height=.35,zorder=0)    
#定制化设置
ax.axis('off')
ax.axvline(x=0,ymin=.05,color='k',lw=.8)
ax.set_ylim(bottom=0,top=4)

#添加文本信息
ax.text(.02,.95,"Chinese exports covered by US Section 232 tariffs",
       transform = ax.transAxes,color='k',ha='left',va='center',size=9,fontweight='semibold')

ax.text(.3,.64,"$2.8 BILLION EXPORT VALUE,2017",
       transform = ax.transAxes,color='white',ha='left',va='center',size=8.5,fontweight='semibold')

#添加文本信息
ax.text(.02,.45,"US exports covered by Chinese retaliatory tariffs for US Section 232 tariffs",
       transform = ax.transAxes,color='k',ha='left',va='center',size=9,fontweight='semibold')

ax.text(.2,.14,"$2.4 BILLION EXPORT VALUE,2017",
       transform = ax.transAxes,color='white',ha='left',va='center',size=8.5,fontweight='semibold')

#再单独添加 semi-finished 描述部分
ax.text(us['2017 imports, in actual dollars2'].sum(),3,"<1%",ha='left',va='center',size=13,
       fontweight='extra bold')
ax.text(us['2017 imports, in actual dollars2'].sum(),3.4,"Semi-finished",ha='left',va='center',size=8,
       fontweight='light')

#添加小横线
ax.plot([us['2017 imports, in actual dollars2'][:1].sum(),us['2017 imports, in actual dollars2'].sum()],[3.55,3.55],
       color='k',lw=.6)
text_loc = us['2017 imports, in actual dollars2'][:3].sum()
ax.text(text_loc,3.64,"STEEL PRODUCTS",ha='center',va='center')
#添加刻度文本
ax.text(-.02,.2,"CHINESE\nACTION IN\nEFFECT\nAPRIL 2",ha='right',va='center',fontweight='medium',size=11,
       transform = ax.transAxes)
ax.text(-.02,.7,"US ACTION\nINEFFECT\nMARCH 23",ha='right',va='center',fontweight='medium',size=11,
       transform = ax.transAxes)
#添加标题
ax.text(-.1,1.15,"How Is China Retaliating for US Nation\nSecurity Tariffs on Steel and Aluminum?",
       transform = ax.transAxes,color='k',ha='left',va='center',size=20,fontweight='extra bold')

ax.text(.91,.05,'\nVisualization by DataCharm',transform = ax.transAxes,
        ha='center', va='center',fontsize = 7,color='black')
plt.savefig(r'F:\DataCharm\商业艺术图表仿制\PIEE_Charts\China US\PIEE_china_us02.png',width=8,height=4,
            dpi=900,bbox_inches='tight',facecolor='white')
#ax.set_axisbelow(True)
plt.show()

最终我们绘制的效果如下:

知识点

  • 横向堆积柱形图绘制 这里举一个小栗子大家看下就可以:
代码语言:javascript
复制
from matplotlib import pyplot as plt
import numpy as np
N = 5
r = range(N)
bars1 = np.random.binomial(20, .7, N)
bars2 = np.random.binomial(20, .5, N)
bars3 = np.random.binomial(20, .4, N)

colors = ['#7f6d5f', '#557f2d', '#2d7f5e']
labels = ["Lasso", "Random Forest", "Decision Tree"]

barWidth = 1
lefts = 0

fig,ax = plt.subplots(figsize=(8,4.5),dpi=200)
for bars, col, label in zip([bars1, bars2, bars3], colors, labels):
    plt.barh(r, bars, left=lefts, color=col, edgecolor='white', height=barWidth, label=label)
    lefts += bars
plt.legend()
plt.ylim(-0.5, len(bars) - 0.5)

ax.text(.91,-.08,'\nVisualization by DataCharm',transform = ax.transAxes,
        ha='center', va='center',fontsize = 7,color='black')
plt.savefig(r'F:\DataCharm\商业艺术图表仿制\PIEE_Charts\China US\PIEE_china_example.png',width=8,height=4,
            dpi=900,bbox_inches='tight')
plt.show()

结果如下:

  • 文本自动添加 虽然代码中有明确解释,但这里我们还是单独进行展示:
代码语言:javascript
复制
for bars, col,text,text2 in zip(us['2017 imports, in actual dollars2'], us_color,us_list,us_text):
    ax.barh(3, bars, left=lefts_us, color=col, height=barWidth,align='center')
    lefts_us += bars
    if lefts_us == us['2017 imports, in actual dollars2'][0]:
        ax.text(bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center')
        ax.text(bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center')
    else:
        ax.text(lefts_us-bars/2,3,text,color="k",fontweight='extra bold',size=13,ha='center',va='center')
        ax.text(lefts_us-bars/2,3.4,text2,color="k",fontweight='light',size=8,ha='center',va='center')

注意里面的if条件语句设置哦!

总结

本期推文我们进行商业图表第6弹的绘制,学习了横向堆积柱状图的绘制方法,此外,颜色的配置也是值得参考和学习的。抽时间将该色系制作成可以直接供excel直接使用的xml文件。关注公众号DataCharm,后台回复"商业图表第6弹"即可获取本文所使用数据的下载方式。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 绘制图表介绍
  • Python-matplotlib仿制
    • 绘图数据
      • 可视化绘制
        • 知识点
        • 总结
        相关产品与服务
        图数据库 KonisGraph
        图数据库 KonisGraph(TencentDB for KonisGraph)是一种云端图数据库服务,基于腾讯在海量图数据上的实践经验,提供一站式海量图数据存储、管理、实时查询、计算、可视化分析能力;KonisGraph 支持属性图模型和 TinkerPop Gremlin 查询语言,能够帮助用户快速完成对图数据的建模、查询和可视化分析。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档