【腾学汇的第2个实验代码】应用Matplotlib绘制图标分析

import matplotlib.pyplot as plt
import numpy as np
#Jupter Notebook 里面显示图片
%matplotlib inline
#1.1.1 线形图
np.random.seed(42) #产生随机种子
y = np.random.randn(30) #产生随机数
plt.plot(y, "r--o")#绘图:红色--虚线--圆形

在这里插入图片描述

# 1.1.2 线条颜色、线型、标记形状
x = np.random.randn(30)
y = np.random.randn(30)

plt.title("Example")
plt.title("Example")

在这里插入图片描述

y1  = np.random.randn(30)#产生随机数 
y2  = np.random.randn(30)#产生随机数 

print(y1,y2)

plt.title("Example")#图表标题 
plt.xlabel("X")#设置横轴标题 
plt.ylabel("Y")#设置纵轴标题 

y1,= plt.plot(y1,"r--o") 
y2, = plt.plot(y2,"b-*") 
plt.legend([y1,y2],["Y1","Y2"])#设置对应图例文字描述

在这里插入图片描述

#1.1.4子图
a = np.random.randn(30)#产生随机数
b = np.random.randn(30)#产生随机数
c = np.random.randn(30)#产生随机数
d=np.random.randn(30)#产生随机数

fig=plt.figure()#定义画布
ax1=fig.add_subplot(2,2,1)#第-个子图画布
ax2=fig.add_subplot(2,2,2)#第二个子图画布
ax3=fig.add_subplot(2,2,3)#第三个子图画布
ax4=fig.add_subplot(2,2,4)#第三个子图画布

A,=ax1.plot(a,"r-o")#绘制第-个子图
ax1.legend([A],["A"])

B,=ax2.plot(b,"b-*")#绘制第二个子图
ax2.legend([B],["B"])

C,=ax3.plot(c,"g-.+")#绘制第三个子图
ax3.legend([C],["C"])
            
D,=ax4.plot(d,"m:x")#绘制第四个子图
ax4.legend ([D],["D"])

在这里插入图片描述

#1,1,5散点图绘制
x = np.random.randn(30)
#产生随机数
y  = np.random.randn(30)
#产生随机数
plt.scatter(x,y,c="g",marker="o",label="(X,Y)") #c:颜色;marker:标记形状;label:图例
plt.title("Example")#图表标题
plt.xlabel("X")
#设置横轴标题
plt.ylabel("Y")
#设置纵轴标题
plt.legend(loc=1)     #图例位置设置
                    #10c=0:图例使用最好位置
                    #10c=1:强制图例使用图中右上角位置
                    #L0C=2:强制图例使用图中左上角位置
                    #10c=3:强制图例使用图中左下角位置
                    #10c=4:强制图例使用图中右下角位置
plt.show()

在这里插入图片描述

#1.1.6直方图绘制
X=np.random.randn(1000)#产生随机数
plt.hist(x,bins=20,color="g")#X:数据;bins:条纹数量;color:颜色
plt.title("Example")#图表标题
plt.xlabel("X")
#设置横轴标题
plt.ylabel("Y")
#设置纵轴标题
plt.show()

在这里插入图片描述

#1.1.7饼图绘制
labels =["TDos","Cats","Birds"]
sizes=[15,50,35]
plt.pie(sizes,explode=(0,0,0.1),labels=labels,autopct="%1.1f%%",startangle=90)#explode:每部分数据系列之间的间隔:autopct:数据以浮点精
plt.axis('equal')
plt.show()

在这里插入图片描述

import random
x  = ["20{}year".format(i) for i in range(18,23)]
y  = [random.randint(1,20) for i in range(5)]
for i in range(len(x)):
    plt.bar(x[i],y[i])
    
plt.title("title")
plt.xlabel("year")
plt.ylabel("number")
plt.show()

在这里插入图片描述

for i in range(len(x)):
                plt.bar(x[i],y[i],color=(0.2*i,0.2*i,0.2*i),linestyle="--",hatch="o",edgecolor="r")
        #i=0,color = (0,0,0); i=1,color=(0.2,0.2,0.2)
        #color = (R,G,B)

在这里插入图片描述

x = ["20{}year".format(i)for i in range(18,23)]
y = list(random.randint(1,20)for i in range(5))
#y = [random.randint(1,20)for i in range(5)]
y2  = list(random.randint(1,20)for i in range(5))
plt.bar(x,y,lw=0.5,fc="r")
# lw:length wide ,fc:face color
plt.bar(x,y2,Lw=0.5,fc="b",bottom=y)
# bottom:控制哪个图显示在底部

在这里插入图片描述

x = ["20{}year".format(i)for i in range(18,23)]
y = list(random.randint(1,20)for i in range(5))
y2 = list(random.randint(1,20)for i in range(5))

x_width = range(0,len(x))
x2_width = [i+0.3 for i in x_width]

plt.bar(x_width,y,lw=0.5,fc="r",width=0.3)
plt.bar(x2_width,y2,lw=0.5,fc="b",width=0.3)

plt.xticks(range(0,5),x)
#(刻度位置,标签)

在这里插入图片描述

x = ["20{}year".format(i)for i in range(18,23)]
y = list(random.randint(1,20)for i in range(5))
y2 = list(random.randint(1,20)for i in range(5))
x_width = range(0,len(x))
x2_width = [i+0.3 for i in x_width]
plt.barh(x_width,y,lw=0.5,fc="r",height=0.3,label="cat")
plt.barh(x2_width,y2,Lw=0.5,fc="b",height=0.3,label="dog")
plt.yticks(range(0,5),x)
plt.legend()
plt.title("title")
plt.ylabel("year")
plt.xlabel("number")
plt.show()

在这里插入图片描述

x = ["20{}year".format(i)for i in range(18,23)]
y = list(random.randint(1,20)for i in range(5))
y2 = list(random.randint(1,20)for i in range(5))

plt.plot(x,y,color="pink",linestyle="--")
plt.plot(x,y2,color="skyblue",linestyle="-.")

#柱状图
plt.bar(x,y,lw=0.5,fc="r",width=0.3,alpha=0.5)
plt.bar(x,y2,lw=0.5,fc="b",width=0.3,alpha=0.5,bottom=y)
#alpha:控制透明度,[0,1]
for i,j in zip(x,y):
    plt.text(i,j,"%d"%j,ha="center",va="bottom")
    
for i2,j2 in zip(x,y2):
    plt.text(i2,j2,"%d"%j2,ha="center",va="bottom")

在这里插入图片描述

x = ["20{}year".format(i)for i in range(18,23)]
y = list(random.randint(1,20)for i in range(5))
y2 = list(random.randint(-20,-1)for i in range(5))

ax = plt.gca()
# 获取当前的axes
ax.spines ["bottom"].set_position(('data',0))
# ax.spinesp["bottom"]:底部边界线(x轴)
# ax.spines["bottom"].set_position():设置x轴位置
plt.bar(x,y,lw=0.5,fc="r",width=0.3)
plt.bar(x,y2,lw=0.5,fc="b",width=0.3)
for i,j in zip(x,y):
    plt.text(i,j,"%d"%j,ha="center",va="top")
for i2,j2 in zip(x,y2):
    plt.text(12,-j2,"%d"%j2,ha="center",va="bottom")

在这里插入图片描述

import matplotlib.pyplot as plt#导入绘图库

from sklearn.linear_model import LogisticRegression
#逻辑回归模型
from sklearn import metrics
from sklearn.datasets import load_breast_cancer#数据集
from sklearn.model_selection import train_test_split

import warnings
warnings.filterwarnings('ignore')
#读取数据
breast_cancer = load_breast_cancer()
X = breast_cancer.data
y = breast_cancer.target
model = LogisticRegression()

trainx,testx,trainy,testy = train_test_split(X,y,test_size=0.2,random_state=42)
model.fit(trainx,trainy)#对训练集进行训练
#模型预测
prey=model.predict(testx)#预测的类标签--O或者1
preproba=model.predict_proba(testx)#preproba包含样本为0的概率和为l的概率
p,r,th  = metrics.precision_recall_curve(testy,preproba[:,1])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('PR')
plt.plot(r,p)
plt.show()

在这里插入图片描述

fpr,tpr,threshold =  metrics.roc_curve(testy,preproba[:,1]) # roc_curve:ROC
#TPR:true positive rate=召回率
#FPR:
roc_auc  = metrics.auc(fpr,tpr)
plt.plot(fpr,tpr,label='Val AUC =%0.3f'%roc_auc)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.title('ROC')
plt.legend(loc='lower right')

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KevinGuo457

哈哈哈资助我买两包辣条叭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值