python的matplotlib的热门可视化动图

1.图

2.代码

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.animation as animation
#导出数据,当然这个数据也可以是直接在网上下载,是的有点慢
#网址:https://gist.githubusercontent.com/johnburnmurdoch/4199dbe55095c3e13de8d5b2e5e5307a/raw/fa018b25c24b7b5f47fd0568937ff6c04e384786/city_populations
df = pd.read_csv(‘city_populations.csv‘,
                 usecols=[‘name‘, ‘group‘, ‘year‘, ‘value‘]) #将数据下载下来放在指定默认的目录和文件夹下,city_populations.csv
#定义
current_year = 2018
dff=()

fig, ax = plt.subplots(figsize=(15, 8))

colors = dict(zip(
    [‘India‘, ‘Europe‘, ‘Asia‘, ‘Latin America‘,
     ‘Middle East‘, ‘North America‘, ‘Africa‘],
    [‘#adb0ff‘, ‘#ffb3ff‘, ‘#90d595‘, ‘#e48381‘,
     ‘#aafbff‘, ‘#f7bb5f‘, ‘#eafb50‘]
))

group_lk = df.set_index(‘name‘)[‘group‘].to_dict()

def draw_barchart(year):
    dff = df[df[‘year‘].eq(year)].sort_values(by=‘value‘, ascending=True).tail(10)
    ax.clear() #每次清空、刷新
    ax.barh(dff[‘name‘], dff[‘value‘], color=[colors[group_lk[x]] for x in dff[‘name‘]])
    dx = dff[‘value‘].max() / 200
    for i, (value, name) in enumerate(zip(dff[‘value‘], dff[‘name‘])):
        ax.text(value-dx, i,     name,           size=14, weight=600, ha=‘right‘, va=‘bottom‘)
        ax.text(value-dx, i-.25, group_lk[name], size=10, color=‘#444444‘, ha=‘right‘, va=‘baseline‘)
        ax.text(value+dx, i,     f‘{value:,.0f}‘,  size=14, ha=‘left‘,  va=‘center‘)
    #显示文字,x=0,y=1.10,坐标,ha=水平对准=水平线平放
    #ax.text()格式=(x,y,string,fontsize=15,verticalalignment="top",horizontalalignment="right")
    #string=字符串=‘文字内容‘
    ax.text(0, 1.10, ‘The most populous cities in the world from 1968 to 2018‘,
            transform=ax.transAxes, size=18, weight=600, ha=‘left‘) #文字标题,第1层
    ax.text(0, 1.04, ‘Population (thousands)‘, transform=ax.transAxes, size=12, color=‘#777777‘) #显示文字,第2层
    ax.text(1, 0.4, year, transform=ax.transAxes, color=‘#777777‘, size=46, ha=‘right‘, weight=800) #右边固定显示动图年份
    #va=verticalalignment="top",垂直对准
    #ha=horizontalalignment="right",alignment=对准,水平对准
    ax.xaxis.set_ticks_position(‘top‘)  #x轴在上面

    ax.set_yticks([]) #默认是显示y轴的名称,左边垂直的城市名字,设为[]就是不显示
    ax.margins(0, 0.01) #不设置就是默认值,缩放比例(0,0.05)
    ax.grid(which=‘major‘, axis=‘x‘, linestyle=‘--‘) #垂直线,布局和格式
    ax.set_axisbelow(True)  #默认是true的

    plt.box(False) #默认是True,False之后不显示黑色线框

animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1968, 2019)) #以animator形式展现动画
plt.show() #以plt的形式展现图片

原文地址:https://www.cnblogs.com/ysysbky/p/12189816.html

时间: 2024-08-06 01:50:31

python的matplotlib的热门可视化动图的相关文章

手把手教你做一个python+matplotlib的炫酷的数据可视化动图

1.效果图 2.注意: 上述资料是虚拟的,为了学习制作动图,构建的. 仅供学习, 不是真实数据,请别误传. 当自己需要对真实数据进行可视化时,可进行适当修改. 3.代码: #第1步:导出模块,固定 import pandas as pd import matplotlib.pyplot as plt import matplotlib.ticker as ticker import matplotlib.animation as animation #第2步:中文字体显示设置1,导出通用字体设置

python中matplotlib绘图封装类之折线图、条状图、圆饼图

DrawHelper.py封装类源码: 1 import matplotlib 2 import matplotlib.pyplot as plt 3 import numpy as np 4 5 class DrawHelper: 6 def __init__(self): 7 # 指定默认字体 下面三条代码用来解决绘图中出现的乱码 8 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] 9 matplotlib.rcParams['font

Python+pandas+matplotlib数据分析与可视化案例

问题描述:运行下面的程序,在当前文件夹中生成饭店营业额模拟数据文件data.csv 然后完成下面的任务: 1)使用pandas读取文件data.csv中的数据,创建DataFrame对象,并删除其中所有缺失值: 2)使用matplotlib生成折线图,反应该饭店每天的营业额情况,并把图形保存为本地文件first.jpg: 3)按月份进行统计,使用matplotlib绘制柱状图显示每个月份的营业额,并把图形保存为本地文件second.jpg: 4)按月份进行统计,找出相邻两个月最大涨幅,并把涨幅最

Python进阶(四十)-数据可视化の使用matplotlib进行绘图

Python进阶(四十)-数据可视化の使用matplotlib进行绘图 前言 ??matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包.我将在这篇文章中介绍matplotlib API的核心对象,并介绍如何使用这些对象来实现绘图.实际上,matplotlib的对象体系严谨而有趣,为使用者提供了巨大的发挥空间.用户在熟悉了核心对象之后,可以轻易的定制图像.matplotlib的对象体系也是计算机图形学的一个优秀范例.即使你不是Python程序员,你也可以从文中

python数据分析实战-第7章-用matplotlib实现数据可视化

第7章 用matplotlib实现数据可视化 149 7.1 matplotlib库 149 7.2 安装 150 7.3 IPython和IPython QtConsole 150 7.4 matplotlib架构 151 7.4.1 Backend层 152 7.4.2 Artist层 152 7.4.3 Scripting层(pyplot) 153 7.4.4 pylab和pyplot 153 7.5 pyplot 154 7.5.1 生成一幅简单的交互式图表 154 123 import

python的matplotlib饼状图

在python的matplotlib画图函数中,饼状图的函数为pie pie函数参数解读 plt.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(

38个常用Python库:数值计算、可视化、机器学习等8大领域都有了

一.数值计算 数值计算是数据挖掘.机器学习的基础.Python提供多种强大的扩展库用于数值计算,常用的数值计算库如下所示. 1. NumPy 支持多维数组与矩阵运算,也针对数组运算提供大量的数学函数库.通常与SciPy和Matplotlib一起使用,支持比Python更多种类的数值类型,其中定义的最重要的对象是称为ndarray的n维数组类型,用于描述相同类型的元素集合,可以使用基于0的索引访问集合中元素. 2. SciPy 在NumPy库的基础上增加了众多的数学.科学及工程计算中常用的库函数,

广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小

迭代公式的指数,使用的1+5j,这是个复数,所以是广义mandelbrot集,大家可以自行修改指数,得到其他图形.各种库安装不全的,自行想办法,可以在这个网站找到几乎所有的python库 http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib #encoding=utf-8 import numpy as np import pylab as pl import time from matplotlib import cm from math

python中matplotlib实现最小二乘法拟合的过程详解

这篇文章主要给大家介绍了关于python中matplotlib实现最小二乘法拟合的相关资料,文中通过示例代码详细介绍了关于最小二乘法拟合直线和最小二乘法拟合曲线的实现过程,需要的朋友可以参考借鉴,下面来一起看看吧. 前言 最小二乘法Least Square Method,做为分类回归算法的基础,有着悠久的历史(由马里·勒让德于1806年提出).它通过最小化误差的平方和寻找数据的最佳函数匹配.利用最小二乘法可以简便地求得未知的数据,并使得这些求得的数据与实际数据之间误差的平方和为最小.最小二乘法还