matplotlib-基本图形

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#线形图   正弦波    两条线放到同一张图片中
x = np.linspace(0,2*np.pi,100)
y = np.sin(x)
plt.plot(x,y,color = 'red')
#余弦波
y2 = np.cos(x)
plt.plot(x,y2,color = 'g',linewidth = 4,linestyle = '--')
y3 = np.sin(2*x)
color = np.random.rand(3)
color
plt.plot(x,y3,color = color)  #三原色
plt.legend(['sin(x)','cos(x)','sin(2x)'],loc = 1)
plt.xlabel('X',fontsize = 25,color = 'red',rotation = 60)
plt.ylabel('fun',fontsize = 25,color = 'green',rotation = 90)
plt.title('三种常见常见函数',color = 'red')

效果图
在这里插入图片描述

plt.rcParams['font.sans-serif'] = 'KaiTi'
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-2,2,1500)
# 两个函数
y1 = np.sqrt(1 - (np.abs(x) - 1)**2)
y2 = -3*np.sqrt(1 - (np.abs(x)/2)**0.5)
plt.figure(figsize=(6,6))
plt.fill_between(x,y1,color = 'red')
plt.fill_between(x,y2,color = 'r')
#_ = plt.text(-0.5,-0.2,'我爱你',fontproperties = 'FangSong',color = 'blue',fontsize = 25)  另一种方法
_ = plt.text(-0.5,-0.2,'我爱你',color = 'blue',fontsize = 25)

效果图
在这里插入图片描述

#条形图
x = np.arange(10)
y = np.random.randint(1,15,size = 10)
plt.bar(x,y,color = ['r','g','blue'],width = 0.6)
plt.grid(axis='y')

效果图
在这里插入图片描述

#饼图
lables = ['工作','sleeping','playing','eating']
p=[0.3,0.3,0.3,0.1]
_ = plt.pie(p,labels=lables,explode=[0,0,0.2,0],
            textprops={'color':'red','fontproperties':'KaiTi','fontsize':20},
            autopct='%0.1f%%')

效果图
在这里插入图片描述

from matplotlib.colors import ListedColormap
color = ListedColormap(['red','blue'])
X = np.random.randn(200,2)
y = (X[:,0]**2+X[:,1]**2>2.25)   #  y是类别
#  X[:,0],   X[:,1]  相当于点的坐标
plt.figure(figsize=(5,5))
plt.scatter(X[:,0],X[:,1],c = y,cmap = color)
x = np.linspace(-1.5,1.5,100)
cicle = lambda x: np.sqrt(2.25-x**2)
plt.plot(x,cicle(x),color = 'green')
plt.plot(x,-cicle(x),color = 'green')
plt.axis('equal')   # 设置横纵坐标的值 相等

效果图
在这里插入图片描述

#直方图
x = np.random.randint(0,10,size = 20)
#bins 将数据等分成多少分
#rwidth 制定等分的范围
plt.hist(x,rwidth =0.5,bins = 5)

效果图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值