# -*- coding: utf-8 -*-
"""
Created on Sun Oct 28 17:35:48 2018
@author: User
"""
import matplotlib
matplotlib.use('GTKAgg')
import matplotlib.pyplot as plt
import matplotlib.transforms
import numpy as np
from sklearn.linear_model import LinearRegression
import pandas
# Load CSV and columns
df = pandas.read_csv(r'C:\Users\User\Desktop\dataset.csv')
print (df.head())
df = df[['TTL_value','packet_size']]
#X = df['Input_port']
#Y = df['Output_port']
X=np.array(df.packet_size)
Y=np.array(df.TTL_value)
#Split the data into training/testing sets
X_train = X[:-100]
X_test = X[-100:]
# Split the targets into training/testing sets
Y_train = Y[:-100]
Y_test = Y[-100:]
# Plot outputsregr = linear_model.LinearRegression()
regr=LinearRegression(fit_intercept=True)
regr.fit(X_test[:,np.newaxis],Y_test)
X_testfit=np.linspace(0,100000)
Y_testfit=np.linspace(255,255)
#Y_testfit=regr.predict(X_testfit[:,np.newaxis])
print ("Normal Pakctet size range is 7 to 65542")
plt.scatter(X_test, Y_test, color='black')
plt.plot(X_testfit, Y_testfit, color='red',linewidth=3)
plt.title('Test Data')
plt.xlabel('packet_size')
plt.ylabel('TTL_value')
plt.xticks((0,20000,40000,60000,80000,100000))
plt.yticks((0,50,100,150,200,250,300))
plt.show()
print ("The TTL value more than 255 is a malicious traffic")
我想显示带有y轴值的红线。红线是255。我尝试了很多次,但确实做不到。
答案 0 :(得分:0)
如果我对您的理解正确,那么您想在y = 255处的红色水平线上标注相应的y值(255)。在这种情况下,这是为您提供的示例工作解决方案。您只需要将plt.text
与所需的x坐标和y坐标以及要放入文本中的字符串一起使用。在这里,我使用y值作为字符串。 Y_testfit[0]*1.005
将文本稍微移到水平线上方,以避免与文本重叠。
您可以针对您的问题调整此解决方案。
import matplotlib.pyplot as plt
X_testfit=np.linspace(0,100000)
Y_testfit=np.linspace(255,255)
plt.plot(X_testfit, Y_testfit, '-r', lw=3)
plt.text(20000, Y_testfit[0]*1.005, 'y=%d' %Y_testfit[0], fontsize=20)