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

发布时间:2021-07-10 13:55:51 作者:Leah
来源:亿速云 阅读:144

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

数据可视化设计

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

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

test_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实现数据可视化

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

推荐阅读:
  1. python数据可视化之 Matplotlib
  2. Python数据可视化教程之Matplotlib实现各种图表实例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python matplotlib

上一篇:vbs如何搜索文件名或者得到目录列表

下一篇:Imail密码加密算法及VBS的实现方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》