文章目录

  • Python数据分析
  • 一、数据分析的介绍
  • 二、matplotlib
  • 三、总结:
  • 四、对比常用统计图
  • 五、绘制散点图
  • 六、绘制条形图
  • 七、绘制直方图
  • 八、matplotlib常见问题总结


Python数据分析

一、数据分析的介绍

python数据分析与挖掘实战书签 python数据分析手册_python


python数据分析与挖掘实战书签 python数据分析手册_图例_02


1、数据分析是用适当的方法对收集来的大量数据进行分析,帮助人们作出判断,以便采取适当行动。

2、数据分析的流程

python数据分析与挖掘实战书签 python数据分析手册_字符串_03

二、matplotlib

1、介绍

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_04


python数据分析与挖掘实战书签 python数据分析手册_python_05


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

python数据分析与挖掘实战书签 python数据分析手册_System_06


python数据分析与挖掘实战书签 python数据分析手册_System_07


假设:x坐标:时间,y坐标:温度

pyplot:专门画图

python数据分析与挖掘实战书签 python数据分析手册_字符串_08

from matplotlib import pyplot as plt

x=range(2,26,2)
y=[15,13,14,5,20,25,26,26,24,22,18,15]
plt.plot(x,y)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_09


python数据分析与挖掘实战书签 python数据分析手册_图例_10


2、

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_11

import matplotlib.pyplot as plt
#圖片大小:寬20,高80,dpi:每英寸點的個數
#設置圖片大小
 fig=plt.figure(figsize=(20,8),dpi=80)
x=range(2,26,2)
y=[15,13,14,5,20,25,26,26,24,22,18,15]
plt.plot(x,y)
plt.savefig("./sig_size.png")
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_12

python数据分析与挖掘实战书签 python数据分析手册_System_13


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_14


eg:

import matplotlib.pyplot as plt
#圖片大小:寬20,高80,dpi:每英寸點的個數
#設置圖片大小
 fig=plt.figure(figsize=(20,8),dpi=80)
x=range(2,26,2)
y=[15,13,14,5,20,25,26,26,24,22,18,15]
plt.plot(x,y)
plt.xticks(x)
plt.savefig("./sig_size.png")
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_15


如果刻度再密集一點

eg:

plt.xticks(range(2,25))

python数据分析与挖掘实战书签 python数据分析手册_python_16


eg:

#設置x軸的刻度
_xtick_labels=[i/2 for i in range(4,49)]
plt.xticks(_xtick_labels[::3])#每隔3取一個

python数据分析与挖掘实战书签 python数据分析手册_图例_17

#設置x軸的刻度
_xtick_labels=[i/2 for i in range(4,49)]
plt.xticks(_xtick_labels(25,50)

python数据分析与挖掘实战书签 python数据分析手册_字符串_18


python数据分析与挖掘实战书签 python数据分析手册_字符串_19


对Y轴操作

xtick_labels=[i/2 for i in range(4,49)]
plt.xticks(_xtick_labels(25,50)
plt.yticks(range(min(y),max(y)+1))

python数据分析与挖掘实战书签 python数据分析手册_图例_20


实例:

1、

python数据分析与挖掘实战书签 python数据分析手册_图例_21

from matplotlib import pyplot as plt
import random
x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.plot(x,y)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_22

from matplotlib import pyplot as plt
import random
x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_23


3、

python数据分析与挖掘实战书签 python数据分析手册_python_24


x显示字符串

from matplotlib import pyplot as plt
import random
x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度,
_x=x
_xtick_labels=["hello,{}".format(i) for i in _x]
plt.xticks(x,_xtick_labels)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_25

from matplotlib import pyplot as plt
import random
x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度,
_x=list(x)[::10]
_xtick_labels=["hello,{}".format(i) for i in _x]
plt.xticks(x,_xtick_labels)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_26

from matplotlib import pyplot as plt
import random
x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度,
_xtick_labels=["10点{}分".format(i)for i in range(60)] #10点0分——10点59分
_xtick_labels += ["11点{}分".format(i-60)for i in range(60,120)]#11点0分——11点119分
#_xtick_labels=["11点{}分".format(i)for i in range(60)] #11点0分——11点59分
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3])
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_27

from matplotlib import pyplot as plt
import random
x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度,
_xtick_labels=["10点{}分".format(i)for i in range(60)] #10点0分——10点59分
_xtick_labels += ["11点{}分".format(i-60)for i in range(60,120)]#11点0分——11点119分
#_xtick_labels=["11点{}分".format(i)for i in range(60)] #11点0分——11点59分
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=90)#rotation表示旋转的度数
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_28


当rotatinotallow=270

python数据分析与挖掘实战书签 python数据分析手册_字符串_29


当rotatinotallow=45

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_30


4、

中文不显示的

fc-list:查看支持的中文

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_31


1、Windows和Linux设置字体的方式

from matplotlib import pyplot as plt
import random
import matplotlib
font = {'family' : 'MicroSoft YaHei',
        'weight' : ;'bold',
        'size' : 'larger'}
matplotlib.rc("font",**font)
matplotlib.rc("font",family='MicroSoft YaHei',weight : "bold")
x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度,
_xtick_labels=["10点{}分".format(i)for i in range(60)] #10点0分——10点59分
_xtick_labels += ["11点{}分".format(i-60)for i in range(60,120)]#11点0分——11点119分
#_xtick_labels=["11点{}分".format(i)for i in range(60)] #11点0分——11点59分
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=90)#rotation表示旋转的度数
plt.show()

注意:如果不知道代码怎么用,可以看源码ctrl+B

python数据分析与挖掘实战书签 python数据分析手册_System_32


python数据分析与挖掘实战书签 python数据分析手册_python_33


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_34


python数据分析与挖掘实战书签 python数据分析手册_System_35


python数据分析与挖掘实战书签 python数据分析手册_python_36


2、第二种方式设置字体大小的方式

from matplotlib import pyplot as plt
import random
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc"#字体的路径

x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度,
_xtick_labels=["10点{}分".format(i)for i in range(60)] #10点0分——10点59分
_xtick_labels += ["11点{}分".format(i-60)for i in range(60,120)]#11点0分——11点119分
#_xtick_labels=["11点{}分".format(i)for i in range(60)] #11点0分——11点59分
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45,fontproperties=my_font)#rotation表示旋转的度数
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_37


python数据分析与挖掘实战书签 python数据分析手册_字符串_38


5、

python数据分析与挖掘实战书签 python数据分析手册_System_39

from matplotlib import pyplot as plt
import random
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc"#字体的路径

x=range(0,120)
y=[random.randint(20,35)for i in range(120)]
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#调整x轴的刻度,
_xtick_labels=["10点{}分".format(i)for i in range(60)] #10点0分——10点59分
_xtick_labels += ["11点{}分".format(i-60)for i in range(60,120)]#11点0分——11点119分
#_xtick_labels=["11点{}分".format(i)for i in range(60)] #11点0分——11点59分
#取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=45,fontproperties=my_font)#rotation表示旋转的度数

#添加描述信息
plt.xlabel("时间")
plt.ylabel("温度 单位(℃)")
plt.title("10点到12点每分钟的气温变化情况")
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_40


注意:中文没有显示,中文显示需要添加fontproperties,把myfont传进去

plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度 单位(℃)",fontproperties=my_font)
plt.title("10点到12点每分钟的气温变化情况",fontproperties=my_font)

python数据分析与挖掘实战书签 python数据分析手册_字符串_41


6、练习

python数据分析与挖掘实战书签 python数据分析手册_python_42


python数据分析与挖掘实战书签 python数据分析手册_图例_43


代码实现:

x轴显示岁数,Y轴显示个数

from matplotlib import pyplot as plt
from matplotlib import font_manager

y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
x = range(11,31)#11岁到31岁刚好20个数


#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#设置X轴刻度
_xtick_labels = ["{}岁",format(i) for i in x]
plt.xticks(x,_xtick_labels)

#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_44


7、

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc"#字体的路径

y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
x = range(11,31)#11岁到31岁刚好20个数


#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#设置X轴刻度
_xtick_labels = ["{}岁",format(i) for i in x]
plt.xticks(x,_xtick_labels,fontproperties=my_font)

#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_45


8、添加个网格

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc"#字体的路径

y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
x = range(11,31)#11岁到31岁刚好20个数


#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#设置X轴刻度
_xtick_labels = ["{}岁",format(i) for i in x]
plt.xticks(x,_xtick_labels,fontproperties=my_font)

#绘制网格
plt.grid()

#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_46


9、使网格更详细一点

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc"#字体的路径

y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
x = range(11,31)#11岁到31岁刚好20个数


#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y)
#设置X轴刻度
_xtick_labels = ["{}岁",format(i) for i in x]
plt.xticks(x,_xtick_labels,fontproperties=my_font)
plt.yticks(range(0,9))
#绘制网格
plt.grid()

#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_图例_47


注:如果想网格颜色浅些,添加透明度

10、添加透明度

#绘制网格
plt.grid(alpha=0.4)

python数据分析与挖掘实战书签 python数据分析手册_python_48

#绘制网格
plt.grid(alpha=0.1)

python数据分析与挖掘实战书签 python数据分析手册_图例_49


11、实例:

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_50

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc"#字体的路径
y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y_2 =[1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,1] 

x = range(11,31)#11岁到31岁刚好20个数


#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.plot(x,y_1)
plt.plot(x,y_2)
#设置X轴刻度
_xtick_labels = ["{}岁",format(i) for i in x]
plt.xticks(x,_xtick_labels,fontproperties=my_font)
#plt.yticks(range(0,9))
#绘制网格
plt.grid(alpha=0.4)

#展示
plt.show()

(黄线:同桌,蓝色:自己)

python数据分析与挖掘实战书签 python数据分析手册_python_51


12、如何辨别黄线和蓝线代表的是谁

plt.plot(x,y_1,label="自己")
plt.plot(x,y_2,label="同桌")

#添加图例
plt.legend()
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_52

plt.plot(x,y_1,label="自己")
plt.plot(x,y_2,label="同桌")

#添加图例
plt.legend(prop=my_font)
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_53

plt.plot(x,y_1,label="自己")
plt.plot(x,y_2,label="同桌")

#添加图例
plt.legend(prop=my_font,loc="upper left")#显示在左上角
#展示
plt.show()

注意:以下代码是图例要放的位置(upper right:右上,upper left:左上,lower right:右下,center left:左边的中间,lower center:下面的中间,upper center:上面的中间;传数字=传字符串(eg:loc="upper left"或者loc=2

python数据分析与挖掘实战书签 python数据分析手册_图例_54

python数据分析与挖掘实战书签 python数据分析手册_字符串_55


13、自己设置线条颜色、线条的风格、线条的粗细和透明度:

python数据分析与挖掘实战书签 python数据分析手册_字符串_56


python数据分析与挖掘实战书签 python数据分析手册_图例_57

plt.plot(x,y_1,label="自己",color="orange") #颜色定为橙色
plt.plot(x,y_2,label="同桌",color="cyan") #颜色定为浅蓝色

#添加图例
plt.legend(prop=my_font,loc="upper left")#显示在左上角
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_58


线条的风格:linestyle=’- -’

线条的粗细:linestyle=5

透明度:alpha=0.5

plt.plot(x,y_1,label="自己",color="orange",linestyle=':' ) #颜色为橙色,线条为纯虚线
plt.plot(x,y_2,label="同桌",color="cyan",linestyle="-.") #颜色为浅蓝色,线条为点划线

#添加图例
plt.legend(prop=my_font,loc="upper left")#显示在左上角
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_59


也可以改网格的线条

plt.plot(x,y_1,label="自己",color="orange",linestyle=':' ) #颜色为橙色,线条为纯虚线
plt.plot(x,y_2,label="同桌",color="cyan",linestyle="-.") #颜色为浅蓝色,线条为点划线
#绘制网格
plt.grid(alpha=0.4,linestyle=':')
#添加图例
plt.legend(prop=my_font,loc="upper left")#显示在左上角
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_图例_60


注意:线条的颜色也可以十六进制表示

python数据分析与挖掘实战书签 python数据分析手册_python_61


python数据分析与挖掘实战书签 python数据分析手册_字符串_62


python数据分析与挖掘实战书签 python数据分析手册_System_63


python数据分析与挖掘实战书签 python数据分析手册_图例_64


python数据分析与挖掘实战书签 python数据分析手册_System_63


eg:

plt.plot(x,y_1,label="自己",color="orange",linestyle=':' ) #颜色为橙色,线条为纯虚线
plt.plot(x,y_2,label="同桌",color="DB7093",linestyle="-.") #颜色为浅蓝色,线条为点划线

python数据分析与挖掘实战书签 python数据分析手册_python_66


14、

python数据分析与挖掘实战书签 python数据分析手册_python_67

三、总结:

python数据分析与挖掘实战书签 python数据分析手册_字符串_68


matplotlib绘图实例

python数据分析与挖掘实战书签 python数据分析手册_字符串_69


python数据分析与挖掘实战书签 python数据分析手册_图例_70


python数据分析与挖掘实战书签 python数据分析手册_python_71


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_72


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_73

四、对比常用统计图

python数据分析与挖掘实战书签 python数据分析手册_字符串_74

五、绘制散点图

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_75


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_76

from matplotlib import pyplot as plt
from matplotlib import font_manager
#X轴绘制时间(3月份和10月份是31天),3月份和10月份的气温绘制在Y轴上
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]
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]
x = range(1,32)
plt.scatter(x,y_3)
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_77


对图像进行放大,同时把10月份的也绘制上去

from matplotlib import pyplot as plt
from matplotlib import font_manager
#X轴绘制时间(3月份和10月份是31天),3月份和10月份的气温绘制在Y轴上
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]
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]
x_3= range(1,32)
#这两个x轴回传到一起,如果不想传到一起需要把x轴网右移,所以后面的10要往后移
x_10=range(51,82)
#设置图形大小
plt.fingure(figsize=(20,8),dpi=80)
plt.scatter(x_3,y_3)
plt.scatter(x_10,y_10) #这两个x轴回传到一起,如果不想传到一起需要把x轴网右移,所以后面的10要往后移
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_78


需要调整刻度

from matplotlib import pyplot as plt
from matplotlib import font_manager
#X轴绘制时间(3月份和10月份是31天),3月份和10月份的气温绘制在Y轴上
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]
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]
x_3= range(1,32)
#这两个x轴回传到一起,如果不想传到一起需要把x轴网右移,所以后面的10要往后移
x_10=range(51,82)
#设置图形大小
plt.fingure(figsize=(20,8),dpi=80)
plt.scatter(x_3,y_3)
plt.scatter(x_10,y_10) 
#调整X轴的刻度
_x = list(x_3)+list(x_10)#因为32到50的数字缺少,因为我们需要展示62个点
_xtick_labels = ["3月{}日".format(i)for i in_3]
_xtick_labels += ["10月{}日".format(i-50)for i in x_10]#这里减50是因为从51开始的
plt.xticks(_x,_xtick_labels)

#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_79


中文太密集,无法显示

from matplotlib import pyplot as plt
from matplotlib import font_manager
#X轴绘制时间(3月份和10月份是31天),3月份和10月份的气温绘制在Y轴上
my_font = font_manger.fontproperties(fname="/System/Library/Fonts/Hiragino Sans GB.ttc"
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]
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]
x_3= range(1,32)
#这两个x轴回传到一起,如果不想传到一起需要把x轴网右移,所以后面的10要往后移
x_10=range(51,82)
#设置图形大小
plt.fingure(figsize=(20,8),dpi=80)
plt.scatter(x_3,y_3)
plt.scatter(x_10,y_10) 
#调整X轴的刻度
_x = list(x_3)+list(x_10)#因为32到50的数字缺少,因为我们需要展示62个点
_xtick_labels = ["3月{}日".format(i)for i in_3]
_xtick_labels += ["10月{}日".format(i-50)for i in x_10]#这里减50是因为从51开始的
plt.xticks(_x,_xtick_labels,fontproperties=my_font,rotation=45)

#展示
plt.show()

注意:fc—list

python数据分析与挖掘实战书签 python数据分析手册_字符串_80

plt.xticks(_x[::3],_xtick_labels[::3],fontproperties=my_font,rotation=45)

python数据分析与挖掘实战书签 python数据分析手册_图例_81


加上X轴表示时间,y轴表示气温

#设置图形大小
plt.fingure(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)#因为32到50的数字缺少,因为我们需要展示62个点
_xtick_labels = ["3月{}日".format(i)for i in_3]
_xtick_labels += ["10月{}日".format(i-50)for i in x_10]#这里减50是因为从51开始的
plt.xticks(_x,_xtick_labels,fontproperties=my_font,rotation=45)
#添加图例
plt.legend(loc="upper left",prop=my_font)
#添加描述信息
plt.xlabel("时间",fontproperties=my_font)
plt.ylabel("温度",fontproperties=my_font)
plt.title("标题",fontproperties=my_font)
#展示
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_82


python数据分析与挖掘实战书签 python数据分析手册_python_83


python数据分析与挖掘实战书签 python数据分析手册_python_84

六、绘制条形图

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_85


python数据分析与挖掘实战书签 python数据分析手册_python_86

from matplotlib import pyplot as plt
from matplotlib import font_manager

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]

#绘制条形图
plt.bar(range(len(a)),b,width=0.3)

plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_87

from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manger.fontproperties(fname="/System/Library/Fonts/Hiragino Sans GB.ttc"

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

#设置图形大小
plt.figure(figsize(20,8),dpi=80)
#绘制条形图
plt.bar(range(len(a)),b,width=0.3)
#设置字符串到X轴
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=45)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_88

#设置字符串到X轴
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_89


图片大些

#设置图形大小
plt.figure(figsize(20,15),dpi=80)
#绘制条形图
plt.bar(range(len(a)),b,width=0.3)
#设置字符串到X轴
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)
plt.savefig("./movie.png")
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_90


python数据分析与挖掘实战书签 python数据分析手册_System_91


绘制横着的条形图

#绘制横着的条形图
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manger.fontproperties(fname="/System/Library/Fonts/Hiragino Sans GB.ttc"

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

#设置图形大小
plt.figure(figsize(20,15),dpi=80)
#绘制条形图
plt.barh(range(len(a)),b,width=0.3)#b和width传的值一样
#设置字符串到X轴
plt.xticks(range(len(a)),a,fontproperties=my_font,rotation=90)
plt.savefig("./movie.png")
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_92

#设置图形大小
plt.figure(figsize(20,8),dpi=80)
#绘制条形图
plt.barh(range(len(a)),b,height=0.3)
#设置字符串到X轴
plt.yticks(range(len(a)),a,fontproperties=my_font)
plt.savefig("./movie.png")
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_93


添加一些颜色和网格

#绘制条形图
plt.barh(range(len(a)),b,height=0.3,color="orange")#width表示竖着条形图线条的粗细
#添加透明度、网格
plt.grid(alpha=0.3)

python数据分析与挖掘实战书签 python数据分析手册_字符串_94


绘制竖着条形图

plt.bar(range(len(a)),b,width=0.3)#width表示竖着条形图线条的粗细

绘制横着条形图

plt.bar(range(len(a)),b,height=0.3)#width表示横着条形图线条的高低

实例:

python数据分析与挖掘实战书签 python数据分析手册_python_95


14_16三天

from matplotlib import pyplot as plt
from matplotlib import font_manager
a=["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
b_16=[15746,312,4497,319]
b_15=[12357,156,2045,168]
b_16=[2358,399,2358,362]

bar_width =0.2

x_14= list(range(len(a)))
x_15=[i+bar_width for i in x_14] #绘制x轴的时候往右移动0.2
x_16=[i+bar_width*2 for i in x_14]
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(len(a)),b_14,width=bar_width)

plt.bar(x_15,b_15,width=bar_width)
plt.bar(x_16,b_16,width=bar_width)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_96


缺少字符串与数字对应

my_font = font_manger.fontproperties(fname="/System/Library/Fonts/Hiragino Sans GB.ttc"
#设置x轴的刻度
plt.xticks(x_15,a,fontproperties=my_font)

python数据分析与挖掘实战书签 python数据分析手册_字符串_97


添加图例

bar_width=0.2#为了防止条形图重叠
plt.bar(range(len(a)),b_14,width=bar_width,label="9月14日")
plt.bar(x_15,b_15,width=bar_width,label="9月15日")
plt.bar(x_16,b_16,width=bar_width,label="9月16日")
#设置图例
plt.legend(prop=my_font)

python数据分析与挖掘实战书签 python数据分析手册_python_98


python数据分析与挖掘实战书签 python数据分析手册_图例_99

七、绘制直方图

python数据分析与挖掘实战书签 python数据分析手册_图例_100


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_101


实例:

from matplotlib import pyplot as plt
from matplotlib import font_manager
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]
plt.hist(a,20)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_102

#计算组数
d =5 #组距
num_bins=max(a)-min(a)//d  #num_bins:组数,a的最大值-a的最小值,//d:取整
plt.hist(a,num_bins)
#设置x轴的刻度
plt.xticks(range(min(a),max(a),d)) #不包含max(a)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_103

#计算组数
d =5 #组距
num_bins=max(a)-min(a)//d  #num_bins:组数,a的最大值-a的最小值,//d:取整
plt.hist(a,num_bins)
#设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d)) #+d包含最大值
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_字符串_104


画网格,设定图形大小

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

python数据分析与挖掘实战书签 python数据分析手册_字符串_105


d(组距)=3时

#计算组数
d =3 #组距
num_bins=max(a)-min(a)//d  #num_bins:组数,a的最大值-a的最小值,//d:取整
plt(max(a),min(a),max(a)-min(a)) #156 78 78
print(num_bins)# 26
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a,num_bins)

#设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d)) #+d包含最大值
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python_106

#计算组数
d =3 #组距
num_bins=max(a)-min(a)//d  #num_bins:组数,a的最大值-a的最小值,//d:取整
plt(max(a),min(a),max(a)-min(a)) #156 78 78
print(num_bins)# 26
#设置图形大小
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a,num_bins,normed=True)

#设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d)) #+d包含最大值
plt。grid()
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_System_107


python数据分析与挖掘实战书签 python数据分析手册_图例_108


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_109

from matplotlib import pyplot as plt
from matplotlib import font_manager

interval = [0,5,10,15,20,25,30,35,40,45,60,90] #时间段,绘制到x轴
width = [5,5,5,5,5,5,5,5,5,15,30,60]#组距
quantity =[836,2737,3723,3926,3956,1438,3273,642,824,613,215,47]

#设置图形大小
plt.figure(figsize(20,8),dpi=80)
plt.bar(range(len(quantity)),quantity,width=width)
plt.show()

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_110


宽度统一

plt.bar(range(len(quantity)),quantity)

python数据分析与挖掘实战书签 python数据分析手册_图例_111

print(lan(interval),len(width),len(quantity))#12 12 12
plt.bar(range(len(quantity)),quantity,width=1)
#设置x轴的刻度
_x = [i-0.5 for i in range(13)]
_xtick_labels = interval=[150]
plt.xticks(_x,_xtick_labels)
#plt.grid(alpha=0.4) #网格的细度

python数据分析与挖掘实战书签 python数据分析手册_python_112


python数据分析与挖掘实战书签 python数据分析手册_python_113

八、matplotlib常见问题总结

1、

python数据分析与挖掘实战书签 python数据分析手册_字符串_114


2、

python数据分析与挖掘实战书签 python数据分析手册_字符串_115


python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_116


拓展:百度ECHART、seaborn

python数据分析与挖掘实战书签 python数据分析手册_python数据分析与挖掘实战书签_117


python数据分析与挖掘实战书签 python数据分析手册_python_118


python数据分析与挖掘实战书签 python数据分析手册_图例_119