平面图绘制(1.Matplotlib库使用)

平面图绘制(1.Matplotlib库使用)

安装和导入库

安装:pip install matplotlib

导入:import matplotlib

#导入文件所需库
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

#中文显示乱码问题解决办法
plt.rcParams['font.sans-serif']=['SimHei']

#符号显示不正常解决办法
plt.rcParams['axes.unicode_minus']=False
1.折线图
#单条折线图绘制

#给x轴和y轴一些数值
x = np.array([10,20,30,40,50,60,70,80,90,100])
y = np.random.randint(101,1000,10)

#画布以及坐标轴创建
plt.figure(figsize=(10,5))   #创建画布
plt.xlim(0,100)  #x轴坐标轴
plt.ylim((0, 1000))   #y轴坐标轴
plt.xlabel('X')   #x轴标签
plt.ylabel('Y')   #y轴标签

plt.plot(x,y,marker = 'o',c = 'b')   #进行绘制   marker对点进行标记   c折线颜色

plt.show()    #显示绘图

单条折线图绘制

#多条折线图绘制

#给x轴和y轴一些数值
x = np.array([10,20,30,40,50,60,70,80,90,100])
y1 = np.random.randint(0,100,10)
y2 = np.random.randint(101,200,10)
y3 = np.random.randint(201,300,10)
y4 = np.random.randint(301,400,10)

#画布以及坐标轴创建
plt.figure(figsize=(10,5))   #创建画布
plt.xlim(0,100)  #x轴坐标轴
plt.ylim((0,500))   #y轴坐标轴
plt.xlabel('X')   #x轴标签
plt.ylabel('Y')   #y轴标签

plt.plot(x,y1,label = 'Y1',marker = 'o')   #进行绘制   label对该条折线进行标记
plt.plot(x,y2,label = 'Y2',marker = 'o')   #进行绘制   label对该条折线进行标记
plt.plot(x,y3,label = 'Y3',marker = 'o')
plt.plot(x,y4,label = 'Y4',marker = 'o')

plt.legend()    #显示折线标记

plt.show()    #显示绘图

多条折线图

2.柱状图
#单柱状图

#给x轴和y轴一些数值
x = np.array([10,20,30,40,50,60,70,80,90,100])
y = np.random.randint(101,1000,10)

#画布以及坐标轴创建
plt.figure(figsize=(10,5))   #创建画布
plt.xlim(0,100)  #x轴坐标轴
plt.ylim((0, 1000))   #y轴坐标轴
plt.xlabel('X')   #x轴标签
plt.ylabel('Y')   #y轴标签

plt.bar(x, y)
plt.show()    #显示绘图

单柱状图

#多柱状图(垂直)

df = pd.DataFrame(np.random.rand(6,4),columns = ['a','b','c','d'])   #使用DataFrame类型数据进行绘图

df.plot(kind = 'bar', grid = True , colormap = 'summer_r')    #多柱状图(垂直)

df.plot(kind = 'bar',  grid = True, colormap = 'Blues_r', stacked = True)   #堆叠图(垂直)
<AxesSubplot:>

多柱状图(垂直)

堆叠图(垂直)

#横向柱状图(barh函数)

df = pd.DataFrame(np.random.rand(6,4),columns = ['a','b','c','d'])   #使用DataFrame类型数据进行绘图

df.plot.barh( grid = True, colormap = 'BuGn_r')   #多柱状图(水平)

df.plot(kind = 'bar',  grid = True, colormap = 'Blues_r', stacked = True)   #堆叠图(水平)
<AxesSubplot:>

多柱状图(水平)

堆叠图(垂直)

3.散点图
#散点图绘制

#给x轴和y轴一些数值
x = np.array([10,20,30,40,50,60,70,80,90,100])
y = np.random.randint(101,1000,10)

#画布以及坐标轴创建
plt.figure(figsize=(10,5))   #创建画布
plt.xlim(0,100)  #x轴坐标轴
plt.ylim((0, 1000))   #y轴坐标轴
plt.xlabel('X')   #x轴标签
plt.ylabel('Y')   #y轴标签

plt.scatter(x,y,s = np.random.randint(600,1000))   #绘制  s点的大小  c颜色

plt.show()    #显示绘图

散点图

4.饼图
#饼图绘制

x = np.array([10,20,30,40,50,60,70,80,90,100])   #数值
label = ['a','b','c','d','e','f','g','h','i','j']    #部分名称

plt.pie( x , labels = label , autopct='%1.1f%%')  #绘制  labels每部分名称  autopct计算百分比

plt.show()#显示绘图

饼图

5.直方图
#直方图绘制

datas = pd.Series(np.random.randn(1000))  #随机取数值

datas.hist( bins = 20, histtype = 'bar', align = 'mid', orientation = 'vertical')
<AxesSubplot:>

直方图

后续学习请看下一篇文章!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值