【Matplotlib】数据可视化实例分析

简介:

1 折线图的制作


1.1 需求描述

使用matplotlib绘制一个简单的折线图,在对其进行定制,以实现信息更加丰富的数据可视化,绘制(1,2,3,4,5)的平方折线图。

1.2 源码

#coding=utf-8import matplotlib as mplimport matplotlib.pyplot as pltimport pylab# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False# squares = [1,35,43,3,56,7]input_values = [1,2,3,4,5]
squares = [1,4,9,16,25]# 设置折线粗细plt.plot(input_values,squares,linewidth=5)# 设置标题和坐标轴plt.title('平方数图',fontsize=24)
plt.xlabel('值',fontsize=14)
plt.ylabel('平方值',fontsize=14)# 设置刻度大小plt.tick_params(axis='both',labelsize=14)

plt.show()

1.3 生成结果

2 scatter()绘制散点图


2.1 需求描述

 使用matplotlib绘制一个简单的散列点图,在对其进行定制,以实现信息更加丰富的数据可视化,绘制(1,2,3,4,5)的散点图。 

2.2 源码

#coding=utf-8import matplotlib as mplimport matplotlib.pyplot as pltimport pylab# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False# 设置散列点纵横坐标值x_values = [1,2,3,4,5]
y_values = [1,4,9,16,25]# s设置散列点的大小,edgecolor='none'为删除数据点的轮廓plt.scatter(x_values,y_values,c='red',edgecolor='none',s=40)# 设置标题和坐标轴plt.title('平方数图',fontsize=24)
plt.xlabel('值',fontsize=14)
plt.ylabel('平方值',fontsize=14)# 设置刻度大小plt.tick_params(axis='both',which='major',labelsize=14)# 自动保存图表,参数2是剪裁掉多余空白区域plt.savefig('squares_plot.png',bbox_inches='tight')

plt.show()

 

2.3 生成结果

 

2.4  需求改进

 使用matplotlib绘制一个简单的散列点图,在对其进行定制,以实现信息更加丰富的数据可视化,绘制1000个数的散点图。并自动统计数据的平方,自定义坐标轴

2.5  源码改进

#coding=utf-8import matplotlib as mplimport matplotlib.pyplot as pltimport pylab# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False# 设置散列点纵横坐标值# x_values = [1,2,3,4,5]# y_values = [1,4,9,16,25]# 自动计算数据x_values = list(range(1,1001))
y_values = [x**2 for x in x_values]# s设置散列点的大小,edgecolor='none'为删除数据点的轮廓# plt.scatter(x_values,y_values,c='red',edgecolor='none',s=40)# 自定义颜色c=(0,0.8,0.8)红绿蓝# plt.scatter(x_values,y_values,c=(0,0.8,0.8),edgecolor='none',s=40)# 设置颜色随y值变化而渐变plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Reds,edgecolor='none',s=40)# 设置标题和坐标轴plt.title('平方数图',fontsize=24)
plt.xlabel('值',fontsize=14)
plt.ylabel('平方值',fontsize=14)#设置坐标轴的取值范围plt.axis([0,1100,0,1100000])# 设置刻度大小plt.tick_params(axis='both',which='major',labelsize=14)# 自动保存图表,参数2是剪裁掉多余空白区域plt.savefig('squares_plot.png',bbox_inches='tight')

plt.show()

2.6 改进结果

 

3 随机漫步图


3.1 需求描述

随机漫步是每次步行方向和步长都是随机的,没有明确的方向,结果由一系列随机决策决定的。本实例中random_walk决策步行的左右上下方向和步长的随机性,rw_visual是图形化展示。

3.2 源码

random_walk.py

from random import choiceclass RandomWalk():    '''一个生成随机漫步数据的类'''
    def __init__(self,num_points=5000):        '''初始化随机漫步属性'''
        self.num_points = num_points
        self.x_values = [0]
        self.y_values = [0]    def fill_walk(self):        '''计算随机漫步包含的所有点'''
        while len(self.x_values)<self.num_points:            # 决定前进方向及沿着该方向前进的距离
            x_direction = choice([1,-1])
            x_distance = choice([0,1,2,3,4])
            x_step = x_direction*x_distance

            y_direction = choice([1,-1])
            y_distance = choice([0,1,2,3,4])
            y_step = y_direction*y_distance            # 拒绝原地踏步
            if x_step == 0 and y_step == 0:                continue

            # 计算下一个点的x和y
            next_x = self.x_values[-1] + x_step
            next_y = self.y_values[-1] + y_step

            self.x_values.append(next_x)
            self.y_values.append(next_y)

rw_visual.py

#-*- coding: utf-8 -*-#coding=utf-8import matplotlib as mplimport matplotlib.pyplot as pltimport pylabfrom random_walk  import RandomWalk# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False# 创建RandomWalk实例rw = RandomWalk()
rw.fill_walk()

plt.figure(figsize=(10,6))

point_numbers = list(range(rw.num_points))# 随着点数的增加渐变深红色plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Reds,edgecolors='none',s=1)# 设置起始点和终点颜色plt.scatter(0,0,c='green',edgecolors='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='blue',edgecolors='none',s=100)# 设置标题和纵横坐标plt.title('随机漫步图',fontsize=24)
plt.xlabel('左右步数',fontsize=14)
plt.ylabel('上下步数',fontsize=14)# 隐藏坐标轴plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)

plt.show()

3.3 生成结果

 

 

4 Pygal模拟掷骰子


4.1 需求描述

对掷骰子的结果进行分析,生成一个掷筛子的结果数据集并根据结果绘制出一个图形。

4.2 源码

 Die类

import randomclass Die:    """
    一个骰子类    """
    def __init__(self, num_sides=6):
        self.num_sides = num_sides    def roll(self):        # 返回一个1和筛子面数之间的随机数
        return random.randint(1, self.num_sides)

die_visual.py

#coding=utf-8from die import Dieimport pygalimport matplotlib as mpl# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False


die1 = Die()
die2 = Die()
results = []for roll_num in range(1000):
    result =die1.roll()+die2.roll()
    results.append(result)# print(results)# 分析结果frequencies = []
max_result = die1.num_sides+die2.num_sidesfor value in range(2,max_result+1):
    frequency = results.count(value)
    frequencies.append(frequency)print(frequencies)# 直方图hist = pygal.Bar()

hist.title = '骰子投掷1000次各面结果统计图'hist.x_labels =[x for x in range(2,max_result+1)]
hist.x_title ='结果'hist.y_title = '结果分布'hist.add('D6+D6',frequencies)
hist.render_to_file('die_visual.svg')# hist.show()

4.3 生成结果

 

 

5 同时掷两个骰子


5.1 需求描述

对同时掷两个骰子的结果进行分析,生成一个掷筛子的结果数据集并根据结果绘制出一个图形。

5.2 源码

 

#conding=utf-8from die import Dieimport pygalimport matplotlib as mpl# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False

die1 = Die()
die2 = Die(10)

results = []for roll_num in range(5000):
    result = die1.roll() + die2.roll()
    results.append(result)# print(results)# 分析结果frequencies = []
max_result = die1.num_sides+die2.num_sidesfor value in range(2,max_result+1):
    frequency = results.count(value)
    frequencies.append(frequency)# print(frequencies)hist = pygal.Bar()
hist.title = 'D6 和 D10 骰子5000次投掷的结果直方图'# hist.x_labels=['2','3','4','5','6','7','8','9','10','11','12','13','14','15','16']hist.x_labels=[x for x in range(2,max_result+1)]
hist.x_title = 'Result'hist.y_title ='Frequency of Result'hist.add('D6 + D10',frequencies)
hist.render_to_file('dice_visual.svg')

5. 生成结果

 

6 绘制气温图表


6.1 需求描述

对csv文件进行处理,提取并读取天气数据,绘制气温表,在图表中添加日期并绘制最高气温和最低气温的折线图,并对气温区域进行着色。

6.2 源码

csv文件中2014年7月部分数据信息

 View Code

 

highs_lows.py文件信息

import csvfrom datetime import datetimefrom matplotlib import pyplot as pltimport matplotlib as mpl# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False# Get dates, high, and low temperatures from file.filename = 'death_valley_2014.csv'with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)    # print(header_row)

    # for index,column_header in enumerate(header_row):
    #     print(index,column_header)
    dates, highs,lows = [],[], []    for row in reader:        try:
            current_date = datetime.strptime(row[0], "%Y-%m-%d")
            high = int(row[1])
            low = int(row[3])        except ValueError: # 处理
            print(current_date, 'missing data')        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)# 汇制数据图形fig = plt.figure(dpi=120,figsize=(10,6))
plt.plot(dates,highs,c='red',alpha=0.5)# alpha指定透明度plt.plot(dates,lows,c='blue',alpha=0.5)
plt.fill_between(dates,highs,lows,facecolor='orange',alpha=0.1)#接收一个x值系列和y值系列,给图表区域着色#设置图形格式plt.title('2014年加利福尼亚死亡谷日气温最高最低图',fontsize=24)
plt.xlabel('日(D)',fontsize=16)
fig.autofmt_xdate() # 绘制斜体日期标签plt.ylabel('温度(F)',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)# plt.axis([0,31,54,72]) # 自定义数轴起始刻度plt.savefig('highs_lows.png',bbox_inches='tight')

plt.show()

6.3 生成结果

  

7 制作世界人口地图:JSON格式


7.1 需求描述

下载json格式的人口数据,并使用json模块来处理。

7.2 源码

 json数据population_data.json部分信息


countries.py

from pygal.maps.world import COUNTRIESfor country_code in sorted(COUNTRIES.keys()):    print(country_code, COUNTRIES[country_code])

 countries_codes.py

from pygal.maps.world import COUNTRIESdef get_country_code(country_name):    """Return the Pygal 2-digit country code for the given country."""
    for code, name in COUNTRIES.items():        if name == country_name:            return code    # If the country wasn't found, return None.
    returnprint(get_country_code('Thailand'))# print(get_country_code('Andorra'))

americas.py

import pygal

wm =pygal.maps.world.World()
wm.title = 'North, Central, and South America'wm.add('North America', ['ca', 'mx', 'us'])
wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv'])
wm.add('South America', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf',    'gy', 'pe', 'py', 'sr', 'uy', 've'])
wm.add('Asia', ['cn', 'jp', 'th'])
wm.render_to_file('americas.svg')

 

world_population.py

#conding = utf-8import jsonfrom matplotlib import pyplot as pltimport matplotlib as mplfrom country_codes import get_country_codeimport pygalfrom pygal.style import RotateStylefrom pygal.style import LightColorizedStyle# 解决中文乱码问题mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False# 加载json数据filename='population_data.json'with open(filename) as f:
    pop_data = json.load(f)    # print(pop_data[1])# 创建一个包含人口的字典cc_populations={}# cc1_populations={}# 打印每个国家2010年的人口数量for pop_dict in pop_data:    if pop_dict['Year'] == '2010':
        country_name = pop_dict['Country Name']
        population = int(float(pop_dict['Value'])) # 字符串数值转化为整数
        # print(country_name + ":" + str(population))
        code = get_country_code(country_name)        if code:
            cc_populations[code] = population    # elif pop_dict['Year'] == '2009':
    #     country_name = pop_dict['Country Name']
    #     population = int(float(pop_dict['Value'])) # 字符串数值转化为整数
    #     # print(country_name + ":" + str(population))
    #     code = get_country_code(country_name)
    #     if code:
    #         cc1_populations[code] = populationcc_pops_1,cc_pops_2,cc_pops_3={},{},{}for cc,pop in cc_populations.items():    if pop <10000000:
        cc_pops_1[cc]=pop    elif pop<1000000000:
        cc_pops_2[cc]=pop    else:
        cc_pops_3[cc]=pop# print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3))wm_style = RotateStyle('#336699',base_style=LightColorizedStyle)
wm =pygal.maps.world.World(style=wm_style)
wm.title = '2010年世界各国人口统计图'wm.add('0-10m', cc_pops_1)
wm.add('10m-1bm',cc_pops_2)
wm.add('>1bm',cc_pops_3)# wm.add('2009', cc1_populations)wm.render_to_file('world_populations.svg')

 

7.3 生成结果

countries.py

world_population.py

 

8 Pygal可视化github仓库


8.1 需求描述

调用web API对GitHub数据仓库进行可视化展示:https://api.github.com/search/repositories?q=language:python&sort=stars

8.2 源码

python_repos.py

# coding=utf-8import requestsimport pygalfrom pygal.style import LightColorizedStyle as LCS, LightenStyle as LS# Make an API call, and store the response.url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'r = requests.get(url)print("Status code:", r.status_code) # 查看请求是否成功,200表示成功response_dict = r.json()# print(response_dict.keys())print("Total repositories:", response_dict['total_count'])# Explore information about the repositories.repo_dicts = response_dict['items']print("Repositories returned:",len(repo_dicts))# 查看项目信息# repo_dict =repo_dicts[0]# print('\n\neach repository:')# for repo_dict in repo_dicts:#     print("\nName:",repo_dict['name'])#     print("Owner:",repo_dict['owner']['login'])#     print("Stars:",repo_dict['stargazers_count'])#     print("Repository:",repo_dict['html_url'])#     print("Description:",repo_dict['description'])# 查看每个项目的键# print('\nKeys:',len(repo_dict))# for key in sorted(repo_dict.keys()):#     print(key)names, plot_dicts = [], []for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    plot_dicts.append(repo_dict['stargazers_count'])# 可视化my_style = LS('#333366', base_style=LCS)

my_config = pygal.Config() # Pygal类Config实例化my_config.x_label_rotation = 45 # x轴标签旋转45度my_config.show_legend = False # show_legend隐藏图例my_config.title_font_size = 24 # 设置图标标题主标签副标签的字体大小my_config.label_font_size = 14my_config.major_label_font_size = 18my_config.truncate_label = 15 # 较长的项目名称缩短15字符my_config.show_y_guides = False # 隐藏图表中的水平线my_config.width = 1000 # 自定义图表的宽度chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Most-Starred Python Projects on GitHub'chart.x_labels = names
chart.add('', plot_dicts)
chart.render_to_file('python_repos.svg')

 

8.3 生成结果


本文转自  zddnd   51CTO博客,原文链接: http://blog.51cto.com/13013666/1949246
相关文章
|
1月前
|
数据可视化 数据挖掘 API
Python数据可视化利器Matplotlib详解
本文将深入探讨Python中常用的数据可视化库Matplotlib,介绍其基本概念、常见绘图函数和实例应用。通过学习Matplotlib,读者可以掌握如何利用Python进行数据可视化,展示数据分析结果。
|
29天前
|
数据可视化 数据挖掘 Python
Python数据可视化:探索Matplotlib的强大功能
数据可视化在如今的数据分析和展示中扮演着至关重要的角色。本文将介绍Python中常用的数据可视化库Matplotlib,深入探讨其功能和应用,帮助读者更好地利用Matplotlib进行数据可视化。
|
30天前
|
数据可视化 数据处理 Python
Python数据可视化库Matplotlib的应用与优势探究
本文将深入探讨Python中强大的数据可视化库Matplotlib的应用与优势。通过介绍Matplotlib的基本概念和常用功能,结合具体案例展示其在数据分析和图表绘制中的灵活性和实用性,帮助读者更好地利用这一工具进行数据可视化。
|
1月前
|
数据可视化 数据挖掘 UED
Python中的数据可视化:使用Matplotlib创建交互式图表
传统的数据可视化工具通常只能生成静态图表,而在数据分析和展示中,交互式图表能够提供更丰富的用户体验和更深入的数据探索。本文将介绍如何利用Python中的Matplotlib库创建交互式图表,让数据分析变得更加生动和直观。
|
2月前
|
数据可视化 Python
Python数据可视化利器Matplotlib实战教程
本文将介绍如何使用Python中强大的数据可视化工具Matplotlib,通过丰富的示例和实战操作,帮助读者快速掌握Matplotlib的基本用法和高级技巧,实现数据可视化的艺术。
|
28天前
|
机器学习/深度学习 数据可视化 数据处理
Python数据可视化:探索Matplotlib库的强大功能
本文将深入探讨Python中用于数据可视化的重要工具之一——Matplotlib库。通过介绍Matplotlib库的基本概念、常用功能和实际应用案例,帮助读者更好地了解如何利用Matplotlib创建各种吸引人的数据图表。
|
1月前
|
数据可视化 搜索推荐 数据挖掘
Python数据可视化——探索Matplotlib库的强大功能
数据可视化在数据分析和展示中扮演着至关重要的角色,而Matplotlib作为Python中最流行的数据可视化库之一,具有丰富的功能和灵活性。本文将深入探讨Matplotlib库的基本用法和高级功能,带您领略数据可视化的魅力。
|
30天前
|
数据可视化 数据挖掘 Python
Python中的数据可视化利器Matplotlib详解
本文将深入探讨Python中一款强大的数据可视化工具——Matplotlib,介绍其基本用法、常见图表类型以及高级定制技巧,帮助读者更好地利用Matplotlib实现数据可视化需求。
|
1月前
|
数据可视化 数据挖掘 Python
Python数据可视化库Matplotlib应用实践
【2月更文挑战第10天】 在数据分析和可视化领域,Python语言的Matplotlib库无疑是一把强大的利器。本文将介绍Matplotlib库的基本用法以及在数据可视化中的应用实践,通过示例代码演示如何利用Matplotlib库创建各种类型的图表,帮助读者更好地理解和运用这一强大工具。
18 0
|
24天前
|
数据可视化 数据挖掘 Python
Python中的数据可视化工具Matplotlib简介与实践
在本文中,我们将介绍Python中常用的数据可视化工具Matplotlib,包括其基本概念、常用功能以及实际应用。通过学习Matplotlib,读者可以更好地理解和运用数据可视化技术,提升数据分析与展示的能力。