Pythonmatplotlib数据可视化:商业图表条形图

python

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

 

以下文章来源于DataCharm,作者 宁海涛

转载地址

https://www.zhihu.com/people/qi-shi-huan-hao-la-14/posts

 

绘制图标介绍

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

 

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

Python matplotlib

绘图数据

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

 

 

可视化

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

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

ACTION IN

EFFECT

APRIL 2",ha="right",va="center",fontweight="medium",size=11,

transform = ax.transAxes)

ax.text(-.02,.7,"US ACTION

INEFFECT

MARCH 23",ha="right",va="center",fontweight="medium",size=11,

transform = ax.transAxes)

#添加标题

ax.text(-.1,1.15,"How Is China Retaliating for US Nation

Security Tariffs on Steel and Aluminum?",

transform = ax.transAxes,color="k",ha="left",va="center",size=20,fontweight="extra bold")

ax.text(.91,.05,"

Visualization by DataCharm",transform = ax.transAxes,

ha="center", va="center",fontsize = 7,color="black")

plt.savefig(r"F:DataCharm商业艺术图表仿制PIEE_ChartsChina USPIEE_china_us02.png",width=8,height=4,

dpi=900,bbox_inches="tight",facecolor="white")

#ax.set_axisbelow(True)

plt.show()

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

 

知识点

  • 横向堆积柱形图绘制 这里举一个小栗子大家看下就可以:

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,"

Visualization by DataCharm",transform = ax.transAxes,

ha="center", va="center",fontsize = 7,color="black")

plt.savefig(r"F:DataCharm商业艺术图表仿制PIEE_ChartsChina USPIEE_china_example.png",width=8,height=4,

dpi=900,bbox_inches="tight")

plt.show()

结果如下:

 

  • 文本自动添加 虽然代码中有明确解释,但这里我们还是单独进行展示:

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条件语句设置哦!

以上是 Pythonmatplotlib数据可视化:商业图表条形图 的全部内容, 来源链接: utcz.com/z/530945.html

回到顶部