我正在繪制一堆線,使用 seaborn 根據變數對它們進行著色,然后我做了一些峰值查找來標記特定的峰值。出于某種原因, sns.lineplot 完全按預期作業,但是,當嘗試使用具有幾乎相同引數的 sns.scatterplot 時,它會引發關于我的顏色不受支持的值錯誤。
我認為相關的軟體包版本:python:3.8.10 matplotlib:3.4.3 seaborn:0.11.2 pandas:1.3.0 numpy:1.21.0
最小可重現示例:
# imports
import pandas as pd
import numpy as np
import seaborn as sns
from scipy import signal
import matplotlib.pyplot as plt
# generate some data
x=np.arange(0,10,0.1)
y=np.sin(x)
df_test1 = pd.DataFrame({'x': x,
'y':y,
'color': ['Penguin']*50 ['Octopus']*50})
# find peaks from the data that we want to mark on our graph
df_test2 = pd.DataFrame()
for c in df_test1['color'].unique():
data = pd.DataFrame()
pks = signal.find_peaks(df_test1.loc[df_test1['color']==c, 'y'],
height=0.2, width=2)
data['x'] = df_test1.loc[df_test1['color']==c,'x'].iloc[pks[0]]
data['Height'] = pks[1]['peak_heights']
data['color'] = c
df_test2 = pd.concat([df_test2, data], ignore_index=True)
# printing the peaks dataframe just to confirm there are things to plot.
print(df_test2)
fig, axes = plt.subplots(1, 2, figsize=(8,8), sharey='row', sharex=True)
# plotting the line I want - this works
sns.lineplot(data=df_test1, x='x', y='y', hue='color', estimator=None, legend=False, ax=axes[0])
sns.lineplot(data=df_test1, x='x', y='y', hue='color', estimator=None, legend=False, ax=axes[1])
# labeling the peaks - This is where I get my error
sns.scatterplot(data=df_test2, x='x', y='Height', hue='color', style='color', legend=False, ax=axes[0])
# the work around I found is comment out the line above, and run the line below:
sns.lineplot(data=df_test2, x='x', y='Height', hue='color', estimator=None, legend=False, ax=axes[1],
markers=True, marker='X', linestyle='',)
fig.tight_layout()
plt.show()
在制作一個最小的可重現示例時,我找到了一種解決方法,但我想知道這是一個海生錯誤還是我誤解的東西。我也花了很多時間尋找解決方案,所以我希望這篇文章可以幫助同一條船上的其他人。
編輯:洗掉了估計器引數(根據特倫頓),仍然是相同的錯誤。不過,我會嘗試更新我的 seaborn 和 matplotlib。
Edit2:更新 seaborn 和 matplotlib 有效,所以我的猜測是有一個錯誤已得到糾正。
uj5u.com熱心網友回復:
更新 seaborn 和 matplotlib 有效,所以我的猜測是有一個錯誤已得到糾正。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/532758.html
