手把手教你用Matplotlib画一个小清新配色的商业图表
大数据DT
共 5239字,需浏览 11分钟
·
2020-09-05 01:00
导读:本文使用小清新配色对散点图和折线图进行另类的绘制,绘制出让人耳目一新的商业图表可视化作品。
01 数据可视化
1import pandas as pd
2import numpy as np
3import matplotlib.pyplot as plt
4from mpl_toolkits.axes_grid1.inset_locator import inset_axes
5
6x = np.arange(0,len(artist_01),1)
7y = artist_01['data01'].values
8x_label = artist_01['year'].values
9
10plt.style.use('fivethirtyeight')
11
12fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='white',edgecolor='white')
13ax.set_facecolor('white')
14line = ax.plot(x,y,color='#333333',lw=1.,zorder=2)
15#绘制不同散点图
16scatter_out = ax.scatter(x,y,s=500,zorder=1,color='white',ec='grey',alpha=.7,lw=.5)
17for i in artist_01.index.to_list():
18 scatter = ax.scatter(x[i],y[i],s=180,zorder=2,ec='k',lw=.4,color=year_color[i])
19scatter_in = ax.scatter(x,y,s=30,zorder=3,color="#333333")
20#定制化绘制
21ax.grid(color='gray',lw=.5,alpha=.5)
22ax.tick_params(left=False,bottom=False,labelbottom=False,labelsize=10,colors='gray')
23ax.set_ylim(bottom=0,top=43)
24ax.set_yticks(np.arange(0, 45, step=5))
25ax.set_xticks(np.arange(-.5, 8, step=.5))
26#添加横线
27ax.axhline(y=0,color='#45627C',lw=8)
28
29#添加数字标签
30label_text = {"size":13,"color":"k",'weight':'semibold'}
31for a,b in zip(x,y):
32 ax.text(a, b+2.5, '%.0f' % b, ha='center', va= 'bottom',fontdict=label_text,color=year_color[a])
33
34#添加小散点图:重点掌握
35axins = inset_axes(ax, width=.4, height=.4,loc='upper left',
36 bbox_to_anchor=(0.01, 0.22, 1, 1),
37 bbox_transform=ax.transAxes,
38 borderpad=0)
39axins.set_ylim(bottom=8,top=35)
40axins.set_xlim(left=-.5,right=2.5)
41axins.plot(x[:3],y[:3],color='#333333',lw=1.,zorder=1)
42axins.axis('off')
43for spine in ['top','bottom','left','right']:
44 ax.spines[spine].set_color("#FFFFFF")
45
46for i in artist_01.index.to_list()[:3]:
47 axins.scatter(x[i],y[i],s=80,color=year_color[i],zorder=2)
48#绘制小横线:原理同上
49line = inset_axes(ax,width=5.3, height=.4,loc='upper left',
50 bbox_to_anchor=(-0.015, 0.15, 1, 1),
51 bbox_transform=ax.transAxes,
52 borderpad=0)
53line.plot([.1,.7],[.1,.1],color='#45627C',lw=2)
54line.axis('off')
55
56#添加阴影效果
57for i in artist_01.index.to_list():
58 ax.axvspan(i-.35, i+.35, facecolor='gray',alpha=.1,zorder=0)
59
60#添加x轴标签
61label_font = {"size":16,'weight':'bold'}
62for i,x,text in zip(artist_01.index.to_list(),x,x_label):
63 ax.text(x, -5,text ,ha='center', va= 'bottom',fontdict=label_font,color=year_color[i])
64
65ax.text(.39,1.2,'\nLOREM IPSUM DOLOR SIT AMET',transform = ax.transAxes,
66 ha='center', va='center',fontsize = 20,color='#45627C')
67
68ax.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,',
69 transform = ax.transAxes,
70 ha='left', va='center',fontsize = 8,color='#45627C')
71
72ax.text(.91,.05,'\nVisualization by DataCharm',transform = ax.transAxes,
73 ha='center', va='center',fontsize = 6,color='black')
74plt.savefig(r'F:\DataCharm\商业艺术图表仿制\artist_01_1.png',width=6,height=3,
75 dpi=900,bbox_inches='tight',facecolor='white')
76#ax.set_axisbelow(True)
77plt.show()
1color = ("#51C1C8", "#E96279", "#44A2D6", "#536D84",
2 "#51C1C8", "#E96279", "#44A2D6", "#536D84")
3year = artist_01.index.to_list()
4year_color = dict(zip(year,color))
5year_color
1#添加小散点图:重点掌握
2axins = inset_axes(ax, width=.4, height=.4,loc='upper left',
3 bbox_to_anchor=(0.01, 0.22, 1, 1),
4 bbox_transform=ax.transAxes,
5 borderpad=0)
6axins.set_ylim(bottom=8,top=35)
7axins.set_xlim(left=-.5,right=2.5)
8axins.plot(x[:3],y[:3],color='#333333',lw=1.,zorder=1)
9axins.axis('off')
10for spine in ['top','bottom','left','right']:
11 ax.spines[spine].set_color("#FFFFFF")
12
13for i in artist_01.index.to_list()[:3]:
14 axins.scatter(x[i],y[i],s=80,color=year_color[i],zorder=2)
15#绘制小横线:原理同上
16line = inset_axes(ax,width=5.3, height=.4,loc='upper left',
17 bbox_to_anchor=(-0.015, 0.15, 1, 1),
18 bbox_transform=ax.transAxes,
19 borderpad=0)
20line.plot([.1,.7],[.1,.1],color='#45627C',lw=2)
21line.axis('off')
1#添加x轴标签
2label_font = {"size":16,'weight':'bold'}
3for i,x,text in zip(artist_01.index.to_list(),x,x_label):
4 ax.text(x, -5,text ,ha='center', va= 'bottom',fontdict=label_font,color=year_color[i])
03 总结
评论