2017-08-30 113 views
0

我正在分析Iris dataset並在花瓣寬度和花瓣長度之間做了散點圖。爲了使情節我用這個代碼:seaborn regplot刪除數據點的顏色

# First, we'll import pandas, a data processing and CSV file I/O library 
import pandas as pd 
# We'll also import seaborn, a Python graphing library 
import warnings # current version of seaborn generates a bunch of warnings that we'll ignore 
warnings.filterwarnings("ignore") 
import seaborn as sns 
import matplotlib.pyplot as plt 
import numpy 
sns.set(style="dark", color_codes=True) 

# Next, we'll load the Iris flower dataset, which is in the "../input/" directory 
iris = pd.read_csv("Iris.csv") # the iris dataset is now a Pandas DataFrame 

# Let's see what's in the iris data - Jupyter notebooks print the result of the last thing you do 
print(iris.head(10)) 

# Press shift+enter to execute this cell 
sns.FacetGrid(iris, hue="Species", size=10) \ 
    .map(plt.scatter, "PetalLengthCm", "PetalWidthCm") \ 
    .add_legend() 

enter image description here

後來我繪製的迴歸線,但繪製這條線後,顏色不清晰可見。我試圖改變回歸線的顏色,但這沒有幫助。我怎樣才能繪製迴歸線而不失去不同物種的顏色?

,使包括迴歸線情節的代碼是:

sns.FacetGrid(iris, hue="Species", size=10) \ 
    .map(plt.scatter, "PetalLengthCm", "PetalWidthCm") \ 
    .add_legend() 
sns.regplot(x="PetalLengthCm", y="PetalWidthCm", data=iris) 

petal_length_array = iris["PetalLengthCm"] 
petal_width_array = iris["PetalWidthCm"] 

r_petal = numpy.corrcoef(petal_length_array, petal_width_array) # bereken de correlatie 

print ("Correlation is : " + str(r_petal[0][1])) 

enter image description here

回答

2

您的問題是sns.regplot()繪製所有點相同的顏色,在積分榜首的不同顏色。

要避免這種情況,請嘗試撥打regplot(..., scatter=False)以防止繪製單個數據點。 Check the documentation for regplot.