千家信息网

Python中怎么使用matplotlib实现数据可视化

发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,Python中怎么使用matplotlib实现数据可视化,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。数据可视化设计本期我们构建一组简单
千家信息网最后更新 2025年02月08日Python中怎么使用matplotlib实现数据可视化

Python中怎么使用matplotlib实现数据可视化,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

数据可视化设计

本期我们构建一组简单的时间变化图表数据,当然还有我们常用的颜色字典构建。具体如下:

import pandas as pdimport numpy as npimport matplotlib.pyplot as plttest_dict = {'x':[0,5,10,15,20,25,30],'year':['1990','1995','2000','2005','2010','2015','2020']}artist_04 = pd.DataFrame(test_dict)color = ("#F5A34D", "#F26F77", "#48AEBA", "#A3BA74","#958298", "#B88357",'#608CB1' )data = artist_04['x'].to_list()data_color = dict(zip(data,color))data_color

颜色字典如下:

{0: '#F5A34D', 5: '#F26F77', 10: '#48AEBA', 15: '#A3BA74', 20: '#958298', 25: '#B88357', 30: '#608CB1'}

详细绘图代码如下:

fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='#FFF7F2',edgecolor='#FFF7F2')ax.set_facecolor('#FFF7F2')#绘制中间横线ax.set_ylim(-.5,1.5)#绘制具有端点形状的直线ax.plot([-3,38],[.5,.5],"-o",lw=1.2,color='gray',markerfacecolor="w",mec='gray',ms=5,        markeredgewidth=1.,zorder=1)#分上下情况绘制点、线混合图形for x in [0,10,20,30]:    #绘制横线上的散点,颜色不同    ax.scatter(x,.5,s=120,color=data_color[x],zorder=2)    #绘制叠加在颜色散点之上的散点,颜色为白色    ax.scatter(x,.5,s=50,zorder=3,color='white')    #绘制散点和圆柱之间的连接线,端点为圆点    ax.plot([x,x],[.5,.5+.6],"-o",color=data_color[x],lw=.6,mfc="w",ms=5,mew=1.2,zorder=3)    #绘制横置圆柱图    ax.plot([x,x+7.5],[.5+.6,.5+.6],lw=15,color=data_color[x],solid_capstyle='round',zorder=1)    ax.scatter(x,.5+.6,s=80,zorder=3,color='white')    ax.text(x+4,.5+.6,s='Lorem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center',             va='center')    #添加年份    ax.text(x-1.4,.5+.2,s=artist_04.loc[artist_04['x']==x,'year'].values[0],color='#686866',fontsize=12,            fontweight='bold',rotation=90)        #添加描述文字    ax.text(x+.5,.5+.3,'Optionally, the text can bedisplayed\n in anotherpositionxytext.Anarrow\npointingfrom the text totheannotated\npoint xy canthen beaddedbydefining\narrowprops.',            ha='left', va='center',fontsize = 4,color='gray')for x in [5,15,25]:    #绘制横线上的散点,颜色不同    ax.scatter(x,.5,s=120,color=data_color[x],zorder=2)    #绘制叠加在颜色散点之上的散点,颜色为白色    ax.scatter(x,.5,s=50,zorder=3,color='white')    #绘制散点和圆柱之间的连接线,端点为圆点    ax.plot([x,x],[.5,.5-.6],"-o",color=data_color[x],lw=.6,mfc="w",ms=5,mew=1.2,zorder=3)    #绘制横置圆柱图    ax.plot([x,x+7.5],[.5-.6,.5-.6],lw=15,color=data_color[x],solid_capstyle='round',zorder=1)    ax.scatter(x,.5-.6,s=80,zorder=3,color='white')    ax.text(x+4,.5-.6,s='Lorem Ipsum',color='white',fontsize=7.5,fontweight='semibold',ha='center',             va='center')    #添加描述文字    ax.text(x+.5,.5-.3,'Optionally, the text can bedisplayed\n in anotherpositionxytext.Anarrow\npointingfrom the text totheannotated\npoint xy canthen beaddedbydefining\narrowprops.',            ha='left', va='center',fontsize = 4,color='gray')    #添加年份    ax.text(x-1.4,.5-.4,s=artist_04.loc[artist_04['x']==x,'year'].values[0],color='#686866',fontsize=12,            fontweight='bold',rotation=90)    #添加题目文本ax.axis('off')ax.text(.49,1.15,'\nTIMELINE INFOGRAPHICS',transform = ax.transAxes,        ha='center', va='center',fontsize = 20,color='gray',fontweight='light')ax.text(.92,.00,'\nVisualization by DataCharm',transform = ax.transAxes,        ha='center', va='center',fontsize = 5,color='black')plt.savefig(r'F:\DataCharm\商业艺术图表仿制\artist_04.png',width=8,height=4,            dpi=900,bbox_inches='tight',facecolor='#FFF7F2')

知识点:

(1)熟悉ax.plot()函数方法,其他参数设置不同对结果也不同。

(2)ax.scatter()绘制散点。

(3)ax.text()文本的灵活添加。

(4)颜色的合理选择。

结果图表如下:

看完上述内容,你们掌握Python中怎么使用matplotlib实现数据可视化的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0