19

Python-matplotlib 商业图表绘制 03

 3 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzI1MTE2ODg4MA%3D%3D&%3Bmid=2650074829&%3Bidx=1&%3Bsn=0082ee77b01465a48d2ec6e716f0219f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

腾讯课堂 |  Python网络爬虫与文本分析

01. 引言

本篇推文为python-matplotlib  商业图表绘制的第三篇文章,主要内容为 圆润 柱状图 的绘制,这也是我之前一直想做的事情,在浏览Matplotlib官网时,发现了一个方法,就试着进行绘制,效果还不错。接下来,我们开始

02. 数据处理

数据如下:(其实index列可以不要的,但构造了就懒的删啦)

R3QzIbu.png!mobile

还是老规矩,构造颜色字典,代码如下:

color = ("#F5B720", "#E6766A", "#48AEBA", "#599EE4","#856E88")
data = artist_03['index'].to_list()
data_color = dict(zip(data,color))
data_color

结果:

别嫌我烦啊,重要、好用的方法我要多说几遍

03. 可视化设计

这里的柱形图我们使用了ax.plot()方法,对里面的solid_capstyle参数设置为'round',就可以达到效果了,官网的例子如下:

fig, ax = plt.subplots(figsize=(8, 2))
ax.set_title('Cap style')


for x, style in enumerate(['butt', 'round', 'projecting']):
ax.text(x+0.25, 1, style, ha='center')
xx = [x, x+0.5]
yy = [0, 0]
ax.plot(xx, yy, lw=12, color='tab:blue', solid_capstyle=style)
ax.plot(xx, yy, lw=1, color='black')
ax.plot(xx, yy, 'o', color='tab:red', markersize=3)
ax.text(2.25, 0.7, '(default)', ha='center')


ax.set_ylim(-.5, 1.5)
ax.set_axis_off()

效果如下:

zmeEVvE.png!mobile

本期的推文就是使用了“round”方法,再配上合理的颜色搭配即可,先上完整代码:

x = np.arange(0,len(artist_03),1)
y = artist_03['data'].values


#fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='white',edgecolor='white')
fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='white',edgecolor='white')
ax.set_facecolor('white')
text_l = ['Lorem ipsum\nDolo Sit Amet','Consectetur\nAdipisicing','Elit Sed DO\nEiusmod Tempor',
'Incididunt Ut\nLabore Et Dolore','Magna Alique\nUt Emin Ad Minim']
label_text = {"size":28,"color":"white",'weight':'light'}
for x,y,z in zip(artist_03['index'].to_list(),artist_03['data'].to_list(),text_l):
ax.plot([x,x],[0,y],lw=22,color=data_color[x],solid_capstyle='round')
ax.text(x,y/2,x,ha='center', va= 'center',fontdict=label_text)
#绘制空心圆
ax.scatter(x,0,s=150,c='white',zorder=3)
ax.scatter(x,0,s=60,c=data_color[x],zorder=4)
ax.scatter(x,0,s=15,c='white',zorder=5)
#绘制文本label
ax.text(x,-12,z,ha='center', va= 'center',color=data_color[x],fontsize=7,fontweight='light')


#绘制空心圆线段
ax.plot([.36,5.64],[0,0],"-o",color="grey", lw=2,ls='--',markerfacecolor="w",mec='gray',ms=7,
markeredgewidth=2,zorder=1)


ax.set_ylim(bottom=-10,top=75)
ax.set_xlim(left=0.3,right=5.7)
ax.set_yticks(np.arange(0, 60, step=5))
ax.grid(which='major',axis='y',ls='-',c='k',alpha=.2,lw=.5)
for spine in ['top','bottom','left','right']:
ax.spines[spine].set_color("white")
ax.tick_params(left=False,bottom=False,labelbottom=False,labelleft=False)
ax.text(.49,.95,'\nLorem Ipsum Dolor Sit Amet',transform = ax.transAxes,
ha='center', va='center',fontsize = 22,color='gray',fontweight='light')
ax.text(.91,-.07,'\nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 5,color='black')
#去除刻度等信息
#ax.axis('off')
ax.set_axisbelow(True)
plt.savefig(r'F:\DataCharm\商业艺术图表仿制\artist_03_1.png',width=6,height=4,
dpi=900,bbox_inches='tight')

(1) 柱状图绘制

for x,y,z in zip(artist_03['index'].to_list(),artist_03['data'].to_list(),text_l):
ax.plot([x,x],[0,y],lw=22,color=data_color[x],solid_capstyle='round')

结合了颜色字典即可。

(2)多重圆的绘制

for x,y,z in zip(artist_03['index'].to_list(),artist_03['data'].to_list(),text_l):
ax.plot([x,x],[0,y],lw=22,color=data_color[x],solid_capstyle='round')
ax.text(x,y/2,x,ha='center', va= 'center',fontdict=label_text)
#绘制空心圆
ax.scatter(x,0,s=150,c='white',zorder=3)
ax.scatter(x,0,s=60,c=data_color[x],zorder=4)
ax.scatter(x,0,s=15,c='white',zorder=5)

这方法也是和之前推文类似,大家就当练习啦

(3) ax.plot()绘制线段

这里使用了ax.plot()方法绘制了另类线段,不仅可以设置线段类型,对线段的始末的形状也可以进行定制设计,代码如下:

#绘制空心圆线段
ax.plot([.36,5.64],[0,0],"-o",color="grey", lw=2,ls='--',markerfacecolor="w",mec='gray',ms=7,
markeredgewidth=2,zorder=1)

ls:linestyle 设置线段类别;marker :设置首末形状,此外还有 markerfacecolor ,  ma rkeredgewidth 等参数都可以进行灵活设置,具体内容大家可以参考官网啊

最后效果如下:

RVRrQbN.png!mobile

后面打算给每个柱状图上 添加矢量小图标 ,增强每个柱状图的个性化属性,也在探索过程中,如果小伙伴们需要,后期推文会进行讲解的。

04. 总结

本期推文介绍了“圆润”柱形图的绘制,相比于常规柱状图,此类效果更加片平化,配上合理颜色后颜值也较高,适合小清新的风格哦。

近期文章

Python网络爬虫与文本数据分析

rpy2库 | 在jupyter中调用R语言代码

tidytext | 耳目一新的R-style文本分析库

reticulate包 | 在Rmarkdown中调用Python代码

plydata库 | 数据操作管道操作符>>

七夕礼物 | 全网最火的钉子绕线图制作教程

读完本文你就了解什么是文本分析

文本分析在经管领域中的应用概述

综述:文本分析在市场营销研究中的应用

plotnine: Python版的ggplot2作图库

小案例: Pandas的apply方法   

stylecloud:简洁易用的词云库  

用Python绘制近20年地方财政收入变迁史视频   

Wow~70G上市公司定期报告数据集

漂亮~pandas可以无缝衔接Bokeh

YelpDaset: 酒店管理类数据集10+G

分享 ”和“ 在看 ”是更好的支持!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK