Pythonmatplotlib数据可视化:商业图表折线图
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
以下文章来源于DataCharm,作者 宁海涛
转载地址
https://www.zhihu.com/people/qi-shi-huan-hao-la-14/posts
引言
在绘制学术图表之余,我们也会进行商业图表的绘制,毕竟学术图表的配色有点单调和严谨啊。今天这篇推文就使用小清新配色对散点图和折线图进行另类的绘制,绘制出让人耳目一新的可视化作品。
02. 数据可视化
本期推文的可视化绘制技巧相对简单,对其进行合理组合和颜色优化,就能呈现出不一样的效果,因为构造数据比较简单,这里直接给出整个的绘制代码:
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1.inset_locator import inset_axesx
= np.arange(0,len(artist_01),1)y
= artist_01["data01"].valuesx_label
= artist_01["year"].valuesplt.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,"
LOREM IPSUM DOLOR SIT AMET",transform = ax.transAxes,
ha="center", va="center",fontsize = 20,color="#45627C")
ax.text(.02,1.05,"
It is a line chart with a title and some series labels,
Time 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,"
Visualization 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)颜色字典的使用
我之前的推文也说过了很多次,合理的运用字典,可以使你事半功倍,这里的代码如下:
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
该方法可以实现负责图表的灵活搭配,本推文题目中的小散点图和题目下的横线由于超出刻度范围而采用此方法,代码如下:
#添加小散点图:重点掌握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进行绘制,颜色设置则使用之前的颜色字典。如下:
#添加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])
最终绘制的效果如下:
以上是 Pythonmatplotlib数据可视化:商业图表折线图 的全部内容, 来源链接: utcz.com/z/530572.html