Py学习  »  Python

Python Seaborn Ridge绘图教程不起作用

ffgg • 3 年前 • 1662 次点击  

如果我复制粘贴上给出的示例 Seaborn website 要绘制“脊线图”,代码在两个不同点失败:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})

# Create the data
rs = np.random.RandomState(1979)
x = rs.randn(500)
g = np.tile(list("ABCDEFGHIJ"), 50)
df = pd.DataFrame(dict(x=x, g=g))
m = df.g.map(ord)
df["x"] += m

# Initialize the FacetGrid object
pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
g = sns.FacetGrid(df, row="g", hue="g", aspect=15, height=.5, palette=pal)

# Draw the densities in a few steps
g.map(sns.kdeplot, "x",
      bw_adjust=.5, clip_on=False,
      fill=True, alpha=1, linewidth=1.5)
g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw_adjust=.5)

# passing color=None to refline() uses the hue mapping
g.refline(y=0, linewidth=2, linestyle="-", color=None, clip_on=False)


# Define and use a simple function to label the plot in axes coordinates
def label(x, color, label):
    ax = plt.gca()
    ax.text(0, .2, label, fontweight="bold", color=color,
            ha="left", va="center", transform=ax.transAxes)


g.map(label, "x")

# Set the subplots to overlap
g.figure.subplots_adjust(hspace=-.25)

# Remove axes details that don't play well with overlap
g.set_titles("")
g.set(yticks=[], ylabel="")
g.despine(bottom=True, left=True)

线 g.refline(y=0, linewidth=2, linestyle="-", color=None, clip_on=False) 和线 g.figure.subplots_adjust(hspace=-.25) 出现以下错误时失败:

AttributeError: 'FacetGrid' object has no attribute 'figure'

我在 熊猫:1.2.4 seaborn:0.11.1

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132569
 
1662 次点击  
文章 [ 1 ]  |  最新文章 3 年前