手把手教你用Matplotlib画一个小清新配色的商业图表

2020-09-08 14:21:18 浏览数 (1)

导读:本文使用小清新配色对散点图和折线图进行另类的绘制,绘制出让人耳目一新的商业图表可视化作品。

作者:宁海涛

来源:DataCharm

01 数据可视化

本文的可视化绘制技巧相对简单,对其进行合理组合和颜色优化,就能呈现出不一样的效果,因为构造数据比较简单,这里直接给出整个的绘制代码:

代码语言:javascript复制
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

x = np.arange(0,len(artist_01),1)
y = artist_01['data01'].values
x_label = artist_01['year'].values

plt.style.use('fivethirtyeight')

fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='white',edgecolor='white')
ax.set_facecolor('white')
line = ax.plot(x,y,color='#333333',lw=1.,zorder=2)
#绘制不同散点图
scatter_out = ax.scatter(x,y,s=500,zorder=1,color='white',ec='grey',alpha=.7,lw=.5)
for i in artist_01.index.to_list():
    scatter = ax.scatter(x[i],y[i],s=180,zorder=2,ec='k',lw=.4,color=year_color[i])
scatter_in = ax.scatter(x,y,s=30,zorder=3,color="#333333")
#定制化绘制
ax.grid(color='gray',lw=.5,alpha=.5)
ax.tick_params(left=False,bottom=False,labelbottom=False,labelsize=10,colors='gray')
ax.set_ylim(bottom=0,top=43)
ax.set_yticks(np.arange(0, 45, step=5))
ax.set_xticks(np.arange(-.5, 8, step=.5))
#添加横线
ax.axhline(y=0,color='#45627C',lw=8)

#添加数字标签
label_text = {"size":13,"color":"k",'weight':'semibold'}
for a,b in zip(x,y):
    ax.text(a, b 2.5, '%.0f' % b, ha='center', va= 'bottom',fontdict=label_text,color=year_color[a])

#添加小散点图:重点掌握   
axins = inset_axes(ax, width=.4, height=.4,loc='upper left',
                   bbox_to_anchor=(0.01, 0.22, 1, 1),
                   bbox_transform=ax.transAxes,
                   borderpad=0)
axins.set_ylim(bottom=8,top=35)
axins.set_xlim(left=-.5,right=2.5)
axins.plot(x[:3],y[:3],color='#333333',lw=1.,zorder=1)
axins.axis('off')
for spine in ['top','bottom','left','right']:
    ax.spines[spine].set_color("#FFFFFF")

for i in artist_01.index.to_list()[:3]:
    axins.scatter(x[i],y[i],s=80,color=year_color[i],zorder=2)
#绘制小横线:原理同上
line = inset_axes(ax,width=5.3, height=.4,loc='upper left',
                  bbox_to_anchor=(-0.015, 0.15, 1, 1),
                  bbox_transform=ax.transAxes,
                  borderpad=0)
line.plot([.1,.7],[.1,.1],color='#45627C',lw=2)
line.axis('off')

#添加阴影效果
for i in artist_01.index.to_list():
    ax.axvspan(i-.35, i .35, facecolor='gray',alpha=.1,zorder=0)

#添加x轴标签
label_font = {"size":16,'weight':'bold'}
for i,x,text in zip(artist_01.index.to_list(),x,x_label):
    ax.text(x, -5,text ,ha='center', va= 'bottom',fontdict=label_font,color=year_color[i])

ax.text(.39,1.2,'nLOREM IPSUM DOLOR SIT AMET',transform = ax.transAxes,
        ha='center', va='center',fontsize = 20,color='#45627C')

ax.text(.02,1.05,'nIt is a line chart with a title and some series labels,nTime line chart: This chart shows the changes in number of queries,',
        transform = ax.transAxes,
        ha='left', va='center',fontsize = 8,color='#45627C')

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

本人认为比较重要和需要掌握的有以下几点:

1. 颜色字典的使用

我之前的推文也说过了很多次,合理的运用字典,可以使你事半功倍,这里的代码如下:

代码语言:javascript复制
color = ("#51C1C8", "#E96279", "#44A2D6", "#536D84",
         "#51C1C8", "#E96279", "#44A2D6", "#536D84")
year = artist_01.index.to_list()
year_color = dict(zip(year,color))
year_color

后面的颜色设置也是依赖于此。

2. from mpl_toolkits.axes_grid1.inset_locator import inset_axes

该方法可以实现负责图表的灵活搭配,本推文题目中的小散点图和题目下的横线由于超出刻度范围而采用此方法,代码如下:

代码语言:javascript复制
#添加小散点图:重点掌握
axins = inset_axes(ax, width=.4, height=.4,loc='upper left',
                   bbox_to_anchor=(0.01, 0.22, 1, 1),
                   bbox_transform=ax.transAxes,
                   borderpad=0)
axins.set_ylim(bottom=8,top=35)
axins.set_xlim(left=-.5,right=2.5)
axins.plot(x[:3],y[:3],color='#333333',lw=1.,zorder=1)
axins.axis('off')
for spine in ['top','bottom','left','right']:
    ax.spines[spine].set_color("#FFFFFF")

for i in artist_01.index.to_list()[:3]:
    axins.scatter(x[i],y[i],s=80,color=year_color[i],zorder=2)
#绘制小横线:原理同上
line = inset_axes(ax,width=5.3, height=.4,loc='upper left',
                  bbox_to_anchor=(-0.015, 0.15, 1, 1),
                  bbox_transform=ax.transAxes,
                  borderpad=0)
line.plot([.1,.7],[.1,.1],color='#45627C',lw=2)
line.axis('off')

3. 文本text()的灵活应用

有时候标题和部分刻度lebel也是使用文本进行绘制,其定制化更高。本期就是使用文本对x轴刻度label进行绘制,颜色设置则使用之前的颜色字典。如下:

代码语言:javascript复制
#添加x轴标签
label_font = {"size":16,'weight':'bold'}
for i,x,text in zip(artist_01.index.to_list(),x,x_label):
    ax.text(x, -5,text ,ha='center', va= 'bottom',fontdict=label_font,color=year_color[i])

02 最终绘制的效果

03 总结

Python-Matplotlib绘制此类图表的灵活性还是不错的(当然,前提是比较属性各个绘图函数

),本期的推文主要涉及到的就是点、线、颜色、子图的合理搭配,希望可以给你们提供绘图灵感,详细的每一步在代码中都有解释。能力有限,有错的地方,大家可以指出啊。

0 人点赞