数据分析(一)----画图

conda:data science package & environment manager

创建环境:
conda create --name python3 python=3
conda create -n python3 python=3

切换环境:
windows:activate python3
linux/macos:source activate python3

matplotlib: 最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建.

Matplotlib画图

  • 官方文档有具体的示例以及demo,可以方便查看。

官方网址:https://matplotlib.org/gallery/index.html

Matplotlib画图-画折线图.

# coding:utf-8
import matplotlib.pyplot as plt
import matplotlib
from matplotlib import font_manager
import random

# 百度经验中认为只支持ttc类型不支持TTF的字体-2018.8.3;但是截至2019.1.5已经支持TTF字体.
# my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\SIMLI.TTF") # windows
my_font = font_manager.FontProperties(fname="/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc") # linux

# 不同的操作系统有不同的字体文件保存路径.
# 查看方式:fc-list :lang=zh-cn,list之后的空格不要丢掉.

# 设置图形大小
plt.figure(figsize=(25,12),dpi=80)

# 设置(x,y_1),(x,y_2),设置折线的数据源.
x = range(0,120)
y_1 = [random.randint(20,35) for i in range(120)]
y_2 = [random.randint(20,35) for i in range(120)]

# 调整x轴的刻度
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45,fontproperties=my_font) #rotaion旋转的度数

# 添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度 单位(℃)",fontproperties=my_font)
plt.title("气温变化情况:",fontproperties=my_font)

# 颜色可以参看https://www.114la.com/other/rgb.htm
plt.plot(x,y_1,label="First",color="#EE9A00")
plt.plot(x,y_2,label="Second",color="#EE1289",linestyle="--")


# 绘制网格
plt.grid(alpha=0.4,linestyle=':')

# 添加图例,位置,loc=1,2...10
plt.legend(prop=my_font,loc="upper left")

# 保持图片.
plt.savefig("./NewFig.png")

# 展示
plt.show()

显示效果:

Matplotlib画图-画散点图.

  • 与折线图基本上没有区别,只是函数的名称不一样。
  • plot -> scatter即可.
# 画散点图的所有参数,示例demo.
# coding=utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager

# window下字体位置.
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/SIMLI.TTF")

# 设置数据源.
x_3 = range(1,32)
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]

x_10 = range(51,82)
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]


# 设置图形大小
plt.figure(figsize=(20,8),dpi=80)

# 使用scatter方法绘制散点图,和之前绘制折线图的唯一区别.
plt.scatter(x_3,y_3,label="3月份")
plt.scatter(x_10,y_10,label="10月份")

# 调整x轴的刻度
_x = list(x_3)+list(x_10)
_xtick_labels = ["3月{}日".format(i) for i in x_3]
_xtick_labels += ["10月{}日".format(i-50) for i in x_10]
plt.xticks(_x[::3],_xtick_labels[::3],fontproperties=my_font,rotation=45)

# 添加图例,ctl可以查看源代码,可以用数字代表具体的位置.
plt.legend(loc="upper left",prop=my_font)

# 添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度",fontproperties=my_font)
plt.title("标题",fontproperties=my_font)

# 保存.
plt.savefig("./Newfig.svg")

# 展示
plt.show()

Matplotlib画图-画条形图-横着.

  • plt.barh(range(len(a)),b_temp,height=0.3,color="orange") 是绘制横着的.
# 画条形图-横着图的所有参数,示例demo.
#绘制横着的条形图
from matplotlib import pyplot as plt
from matplotlib import font_manager
import copy

# 设置字体属性.
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/SIMLI.TTF")


a=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊",]

b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]

# 缺陷,要保证排序中最大->最小的数值,不能简单的使用b.sort(),默认升序.
# list.sort( key=None, reverse=False),默认reverse=False,升序.
# b.sort()

# 反转.
# a.reverse()
# b.reverse()

# 若数据无序,要使得有序并且自大而小,从上而下.
b_temp = copy.deepcopy(b)
a_temp = list()
# b_temp.sort(reverse = False),绘图方式为远离远点的方式.
b_temp.sort()

# 排序方法一:
# for x_b_temp in b_temp:	
# 	count = 0
# 	for x_b in b:		
# 		if x_b == x_b_temp:
# 			a_temp.append(a[count])
# 		count += 1

# 排序方法二:
# list.index(obj)
for x_b_temp in b_temp:	
	a_temp.append(a[b.index(x_b_temp)])
		
# 此时排序完成,a_temp与b_temp一一对应,自大而小.
# 设置图形大小
plt.figure(figsize=(20,8),dpi=80)

# 绘制条形图
plt.barh(range(len(a)),b_temp,height=0.3,color="orange")

# 设置字符串到x轴
plt.yticks(range(len(a)),a_temp,fontproperties=my_font)

plt.grid(alpha=0.3)
# plt.savefig("./NewTemp.svg"),svg为矢量图.

plt.show()

制图效果:

Matplotlib画图-画条形图-竖着.

  • plt.bar(range(len(a)),b_temp,height=0.3,color="orange") 绘制竖着的图.
# 画条形图-竖着图的所有参数,示例demo.

Matplotlib画图-直方图

  • 直方图与条形图,柱形图不太一样,但是极为相似。

  • 个人认为区别在于:

    • 直方图是一个事物的二维描述,描述的是一件事情,比如不同电影(此时为电影,不在区分类别,具体电影)的时间长度(只有一种属性描述:时间)。

    • 柱形图是描述的不同事物的一维情况,比如不同电影(不同事物)的收益(一维),时间长度(一维)。

# coding=utf-8
import math
from matplotlib import pyplot as plt
from matplotlib import font_manager

# 字体属性设置.
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/SIMLT.TTF")

# x坐标数据.
a = [131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
a[a.index(156)] = 158
print(a)
#计算组数
"""
# 不修改数据只能d=3.
d = 3 #组距
num_bins = (max(a)-min(a))//d

#设置图形的大小
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a,num_bins,normed=True)

#设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d))

# 绘制网格线-由刻度描述.
plt.grid()

# 展示最后的效果图.
plt.show()
"""


d = 5  #组距
num_bins = (max(a)-min(a))//d

# 用此法也无法解决对称的问题,只能保证祖间距*组数=max-min.
if not (num_bins*d + min(a)) == max(a):
    num_bins += 1

print(max(a),min(a),num_bins)


# num_bins += 1,在设置刻度位置+d实现不错乱位置.
# 要是利用num_bins += 1错乱位置.
# 设置图形的大小
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a,num_bins,normed=True)

#设置x轴的刻度
plt.xticks(range(min(a),(num_bins*d+min(a)+d),d))

# 绘制网格线-由刻度描述.
plt.grid()

# 展示最后的效果图.
plt.show()

效果图:

猜你喜欢

转载自blog.csdn.net/CS_GaoMing/article/details/85867069