[英]seaborn regplot removes colors of datapoints
我正在分析Iris 数据集并在花瓣宽度和花瓣长度之间绘制散点图。 为了制作情节,我使用了以下代码:
# 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()
之后我绘制了一条回归线,但绘制这条线后,颜色不太明显。 我试图改变回归线的颜色,但这没有帮助。 如何在不丢失不同物种颜色的情况下绘制回归线?
制作包含回归线的图的代码是:
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]))
您的问题是sns.regplot()
在具有不同颜色的点之上绘制相同颜色的所有点。
为避免这种情况,请尝试调用regplot(..., scatter=False)
以防止绘制单个数据点。 检查regplot
的文档。
如果你很高兴有多个回归线,你可以拆分你的数据并过度绘制......
iris = sns.load_dataset("iris")
fig, ax = plt.subplots()
colors = ['darkorange', 'royalblue', '#555555']
markers = ['.', '+', 'x']
for i, value in enumerate(iris.species.unique()):
ax = sns.regplot(x="petal_length", y="petal_width", ax=ax,
color=colors[i],
marker=markers[i],
data=iris[iris.species == value],
label=value)
ax.legend(loc='best')
display(fig)
plt.close('all')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.