2021-09-14数据分析:matplotlib绘制散点图、条形图、直方图

文章目录

      • 1.matplotlib绘制散点图
      • 2.matplotlib绘制条形图
      • 3.matplotlib绘制直方图

1.matplotlib绘制散点图

plt.scatter()

# -*- coding: GBK -*-
from matplotlib import pyplot as plt
from matplotlib import font_manager as fm
import random
my_font = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
x1 = range(1,32)
x2 = range(51,82)

y1 = [random.randint(0,100) for i in range(31)]
y2 = [random.randint(0,100) for i in range(31)]

#设置图形大小
plt.figure(figsize=(10,5),dpi=100)
plt.scatter(x1,y1,label = '3月')
plt.scatter(x2,y2,label = '10月')

#调整x的刻度
_x = list(x1)+list(x2)
_xtick_labels = ['3月{0}日'.format(i) for i in x1]+ ['10月{0}日'.format(i-50) for i in x2]
plt.xticks(_x[::5],_xtick_labels[::5],rotation = 45,fontproperties = my_font)  #一一对应

#描述信息
plt.xlabel('时间',fontproperties = my_font)
plt.ylabel('温度',fontproperties = my_font)
plt.title('3月和10月的温度图',fontproperties = my_font)
plt.legend(loc = 'upper right',prop = my_font)

plt.show()

2021-09-14数据分析:matplotlib绘制散点图、条形图、直方图_第1张图片

2.matplotlib绘制条形图

plt.bar()
当x的某一个字太长,可以用\n换行打印

import random
my_font = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')

x = ['红','橙','黄','绿','青','蓝','紫','白','黑黑\n黑黑','灰灰\n灰灰\n灰','亮','暗','明']
y = [random.randint(0,100) for i in range(len(x))]

#图片大小
plt.figure(figsize=(5,8),dpi=100)
plt.bar(range(len(x)),y,width=0.5)         #竖着width,横着height
#plt.barh(range(len(x)),y,height=0.5)      横着的条形图

#设置x轴
plt.xticks(list(range(len(x))),x,fontProperties =my_font)

#网格
plt.grid(alpha = 0.3)

#保存
plt.savefig('./color.png')
plt.show()

2021-09-14数据分析:matplotlib绘制散点图、条形图、直方图_第2张图片

from matplotlib import pyplot as plt
import matplotlib.font_manager as fm
my_font = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')

a =['星球崛起','敦刻尔克','蜘蛛侠','战狼2']
b_16 = [15746,312,4497,319]
b_15 = [12345,156,2045,168]
b_14 = [2348,399,2358,362]

'''
这样写的话,同部电影不同时间是分开的:
_a1 = [i for i in range(1,14,4)]
_a2 = [i for i in range(2,15,4)]
_a3 = [i for i in range(3,16,4)]
'''
########重点#########
bar_width = 0.2
_a1 = list(range(len(a)))
_a2 = [i+bar_width for i in _a1]
_a3 = [i+bar_width*2 for i in _a1]

plt.bar(_a1,b_14,width=bar_width,color='blue',label = '9月14日')
plt.bar(_a2,b_15,width=bar_width,color='yellow',label = '9月15日')
plt.bar(_a3,b_16,width=bar_width,color='red',label = '9月16日')

plt.xticks(_a2,a,fontproperties =my_font)

plt.legend(prop = my_font)
plt.show()

2021-09-14数据分析:matplotlib绘制散点图、条形图、直方图_第3张图片

3.matplotlib绘制直方图

plt.hist(a,numbins)
numbins为分的组数
在这里插入图片描述

from matplotlib import pyplot as plt
import matplotlib.font_manager as fm
import random
my_font = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')

a = [random.randint(0,100) for i in range(1000)]
#计算组数
d = 5 #组距
num_bins = (max(a)-min(a))//d
plt.hist(a,num_bins)

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

plt.grid(alpha = 0.3)
plt.show()

2021-09-14数据分析:matplotlib绘制散点图、条形图、直方图_第4张图片
不是所有的都可以用hist(),仅有原数据时候才可以

####统计好的数据######
from matplotlib import pyplot as plt
import matplotlib.font_manager as fm
my_font = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')

interval = [0,5,10,15,20,25,30,35,40,45,60,90]
width = [5,5,5,5,5,5,5,5,5,15,30,60]
quanlity = [826,2727,3724,3856,3545,2456,1234,688,788,477,233,189]

plt.bar(range(12),quanlity,width=1)

#设置x轴
_x = [i-0.5 for i in range(13)]
_xtick_label = interval+[150]
plt.xticks(_x,_xtick_label)

plt.show()

2021-09-14数据分析:matplotlib绘制散点图、条形图、直方图_第5张图片
4.网上资源
百度ECharts
plotly
seaborn

你可能感兴趣的:(深度学习笔记,python,pycharm,机器学习)