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)
效果图