【问题标题】:seaborn regplot removes colors of datapointsseaborn regplot 删除数据点的颜色
【发布时间】:2017-08-30 08:22:54
【问题描述】:

我正在分析Iris dataset,并在花瓣宽度和花瓣长度之间绘制了一个散点图。为了制作情节,我使用了这段代码:

# 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]))

【问题讨论】:

    标签: python matplotlib regression seaborn


    【解决方案1】:

    您的问题是sns.regplot() 将所有点绘制为相同颜色,在不同颜色的点之上。

    为避免这种情况,请尝试调用regplot(..., scatter=False) 以防止绘制单个数据点。 Check the documentation for regplot.

    【讨论】:

    • 它没有按预期工作。 regplot 只考虑一种类型的数据点。能否请您显示完整的方法,我可能会遗漏一些东西。
    • @jaysabir 我不明白你的评论。请发布一个新问题,并完整描述您的问题,包括代码和数据
    • 我想用色调绘制一个regplot。就像这个问题一样。
    • 我同意@DizietAsahi,为了节省您的时间,请查看stackoverflow.com/questions/47407173/…
    【解决方案2】:

    如果您乐于拥有多条回归线,您可以拆分数据并重绘...

    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')
    

    【讨论】:

      猜你喜欢
      • 2015-08-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      相关资源
      最近更新 更多