使用http://seaborn.pydata.org/ generated/seaborn.violinplot.html 上的示例:
import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x="day", y="total_bill", data=tips)
Run Code Online (Sandbox Code Playgroud)

(来源:pydata.org)
如何在每把小提琴的顶部绘制两条小水平线(例如指示分布的 2.5 百分位数和 97.5 百分位数的误差线上限?
我正在尝试使用 seaborn 的coefplot函数,但它没有显示输出。相反,我收到一个错误:
AttributeError:模块“seaborn”没有属性“coefplot”。
myresultsmyresult .env_corr(env_vars)
def env_corr(self, env_vars, coeff_plot=False, qq_plot=False):
"""
Determine correlations with environmental/non-discretionary variables
using a logit regression. Tobit will be implemented when available
upstream in statsmodels.
Takes:
env_vars: A pandas dataframe of environmental variables
Returns:
corr_mod: the statsmodels' model instance containing the inputs
and results from the logit model.
Note that there can be no spaces in the variables' names.
"""
import matplotlib.pyplot as plt
from statsmodels.regression.linear_model import OLS
from statsmodels.graphics.gofplots import qqplot
import …Run Code Online (Sandbox Code Playgroud) 我需要创建一个如下所示的图表,一系列堆叠的条形图。有些值为正值,有些值为负值。我希望图表的显示方式就像 Excel 中的堆叠条形图一样,它将正值向上堆叠,将负值向下堆叠。有没有一种简单的方法可以对 N 个类别执行此操作?
这是一些示例数据:
df = pd.DataFrame(index=['06-11-2018', '06-12-2018', '06-13-2018', '06-14-2018', '06-15-2018'],
data={'A': [-378, -2347, 509, 987, 513],
'B': [-527, -2599, 765, 533, 670],
'C': [-2343, -2273, 2093, 2197, 1990],
'D': [-1845, -1853, 3325, 1306, 2160]})
Run Code Online (Sandbox Code Playgroud)
如何将点放在seaborn条形图上?以下代码生成条形图。
import matplotlib.pyplot as plt
import seaborn
import matplotlib
matplotlib.use('TkAgg')
seaborn.set_context('talk')
data_df = pandas.DataFrame([3, 1, 2, 4], index=['a', 'b', 'c', 'd']).transpose()
points_df = pandas.DataFrame([3.5, 0.5, 1.75, 4.25], index=['a', 'b', 'c', 'd']).transpose()
plt.figure()
seaborn.barplot(data=data_df)
plt.show()
Run Code Online (Sandbox Code Playgroud)
现在如何将数据添加到points_df该图表上作为红点?
我正在绘制小面网格,以便我的 x 轴刻度是日期 (dd-mm-yy),但它看起来不太好。有没有办法改变网格的大小(如figsize)?
g = sns.FacetGrid(c53_grid, col='question',col_wrap = 4,
margin_titles=True)
g = g.map(plt.plot, "submitted_at", "int_value_0")
g = g.map(plot_mean, 'int_value_0', ls=":", c=".5")
g.set_titles("{col_name}")
g.set_xticklabels(rotation = 45)
Run Code Online (Sandbox Code Playgroud) 我创建了一组遵循特定配色方案的雨云图分布(来自seaborn的Set2)。我想让我的计数图与列出的组的颜色相匹配(例如:饮食组的男性和女性计数为绿色,mod-pa 的 m:f 计数为粉红色等)。但是我无法将调色板与 x 变量和色调对齐。看来 countplot 只会根据色调着色。
我尝试过使用 set_colors 来操作哪些栏来改变颜色,我也尝试过根据如下条件映射颜色,但似乎没有任何效果。
ax = sns.countplot(x="Group", hue="Sex", data=df)
ax[0].set_color('r')
TypeError: 'AxesSubplot' object does not support indexing
value=(df['Group']=='DIET') & (df['Sex']=='Female')
df['color']= np.where( value==True , "#9b59b6", "#3498db")
ax = sns.countplot(x="Group", hue="Sex", data=df, color=df['color'])
TypeError: 'Series' objects are mutable, thus they cannot be hashed
Run Code Online (Sandbox Code Playgroud)
完整代码
import pandas as pd
import numpy as np
import seaborn as sns
df = pd.DataFrame({"Sex" : np.random.choice(["Male", "Female"], size=1310, p=[.65, .35]),
"Group" : np.random.choice(["DIET", "MOD-PA", "HIGH-PA"],size=1310)})
# Unique …Run Code Online (Sandbox Code Playgroud) 我的数据框中的列名称很长,因此当我制作配对图时,标签彼此重叠。我想将我的标签旋转 90 度,这样它们就不会发生碰撞。我尝试在线查找和文档,但找不到解决方案。这是我写的内容和错误消息:
plt.figure(figsize=(10,10))
g = sn.pairplot(df, kind="scatter")
g.set_xticklabels(g.get_xticklabels(), rotation=90)
g.set_yticklabels(g.get_yticklabels(), rotation=90)
Run Code Online (Sandbox Code Playgroud)
plt.figure(figsize=(10,10))
g = sn.pairplot(df, kind="scatter")
g.set_xticklabels(g.get_xticklabels(), rotation=90)
g.set_yticklabels(g.get_yticklabels(), rotation=90)
Run Code Online (Sandbox Code Playgroud)
如何在 Seaborn PairGrid 中旋转标签(x 和 y)?
注意:对不起,我的wifi无法上传图片以供参考。
我正在处理一群患者的肿瘤图像表达数据,对于每个患者,我都有一个提取的肿瘤图像特征的列表。我使用层次聚合聚类对患者和特征进行了聚类,并使用Seaborn 的 .clustermap对其进行了绘制。这是我到目前为止所拥有的:

现在,每个患者都有一堆与之相关的分类信息,这些是癌症亚型(A、B、C、D)、T 分期(1、2、3、4)、N 分期(0、1、2、3) )、M stage(0,1) 以及它们所属的 HAC(1,2,3,...) 集群。此外,每个图像特征也属于不同的类别。我想在每个轴上显示这个分类信息(我知道 {row, col}_colors。本质上我正在尝试重新创建下面的图,我想知道在 Python 中使用 matplotlib/seaborn 是否可以实现这一点。

另外,你认为这个图的作者是在 2014 年用什么来生成它的。R?
我的代码带有一些随机数据:
# Random dummy data
np_zfeatures = np.random.random((420, 1218)) # example matrix of z-scored features [patients, features]
patient_T_stage = np.random.randint(low=1, high=5, size=(420,))
patient_N_stage = np.random.randint(low=0, high=4, size=(420,))
patient_M_stage = np.random.randint(low=0, high=2, size=(420,))
patient_O_stage = np.random.randint(low=0, high=5, size=(420,))
patient_subtype = np.random.randint(low=0, high=5, size=(420,))
feature_class = np.random.randint(low=0, high=5, size=(1218,)) # There's 5 categories of features (first order, shape, …Run Code Online (Sandbox Code Playgroud) 我正在尝试与seaborn 一起绘制line plot。
使用:
sns.relplot(Data, x="InvoiceYearMonth", y="price", hue="company")
Run Code Online (Sandbox Code Playgroud)
给我一个错误:
TypeError: relplot() got multiple values for argument 'x'
Run Code Online (Sandbox Code Playgroud)
可能出什么问题了?
这是我的数据样本供您参考:
InvoiceYearMonth company price
0 202001 companyA 1509.40
1 202001 companyB 469.00
2 202001 companyC 358.81
3 202002 companyD 870.00
4 202002 companyE 465.58
5 202002 companyF 563.00
6 202003 companyG 1140.00
Run Code Online (Sandbox Code Playgroud) 我有这个数据框:
df = {'DATE': [43390, 43599, 43605, 43329, 43440],
'STORE': [1, 1, 1, 2, 2],
'LYLTY_CARD_NBR': [1000, 1307, 1343, 2373, 2426],
'TXN_ID': [1, 348, 383, 974, 1038],
'PROD_QTY': [2, 3, 2, 5, 3],
'TOT_SALES': [6.0, 6.3, 2.9, 15.0, 13.8]}
transac = pd.DataFrame(df)
DATE STORE LYLTY_CARD_NBR TXN_ID PROD_QTY TOT_SALES
0 43390 1 1000 1 2 6.0
1 43599 1 1307 348 3 6.3
2 43605 1 1343 383 2 2.9
3 43329 2 2373 974 5 15.0
4 43440 2 2426 …Run Code Online (Sandbox Code Playgroud) python ×10
seaborn ×10
matplotlib ×5
pandas ×2
bokeh ×1
boxplot ×1
charts ×1
colors ×1
data-science ×1
dataframe ×1
facet-grid ×1
label ×1
plot ×1
python-3.x ×1
r ×1
violin-plot ×1