簡體   English   中英

在 seaborn 中繪制不同長度的 arrays

[英]Plotting arrays with different lengths in seaborn

我有一個 dataframe ,我想從中制作一條 plot ,該數組包含以下內容

   Symbol  Avg.Sentiment  Weighted  Mentions                                          Sentiment
0     AMC           0.14      0.80       557  [-0.38, -0.48, -0.27, -0.42, 0.8, -0.8, 0.13, ...
2     GME           0.15      0.26       175  [-0.27, 0.13, -0.53, 0.65, -0.91, 0.66, 0.67, ...
1      BB           0.23      0.29       126  [-0.27, 0.34, 0.8, -0.14, -0.39, 0.4, 0.34, -0...
11    SPY          -0.06     -0.03        43  [0.32, -0.38, -0.54, 0.36, -0.18, 0.18, -0.33,...
4    SPCE           0.26      0.09        35  [0.65, 0.57, 0.74, 0.48, -0.54, -0.15, -0.3, -...
13     AH           0.06      0.02        33  [0.62, 0.66, -0.18, -0.62, 0.12, -0.42, -0.59,...
12   PLTR           0.16      0.05        29  [0.66, 0.36, 0.64, 0.59, -0.42, 0.65, 0.15, -0...
15   TSLA           0.13      0.03        24  [0.1, 0.38, 0.64, 0.42, -0.32, 0.32, 0.44, -0....

以此類推,'Sentiment'列表中的元素數量與提及次數相同,我想制作一條plot,其中Symbol為x軸,sentiment為y軸,我相信這個問題我遇到的是由於列表長度不同,我得到的實際錯誤讀數是

ValueError: setting an array element with a sequence.

我試圖用來創建條 plot 的代碼是這個

def symbolSentimentVisualization(dataset):
    sns.stripplot(x='Symbol',y='Sentiment',data=dataset.loc[:9])
    plt.show()

the other part of my issue I would guess has something to do with numpy trying to set multidimensional arrays with different lengths before being put into a seaborn plot, but not 100% on that, if the solution is to plot one row at a time and然后合並肯定會起作用的繪圖,但我不確定我應該調用什么來做到這一點,因為嘗試以下方法似乎也不起作用。

def symbolSentimentVisualization(dataset):
    sns.stripplot(x=dataset['Symbol'][0],y=dataset['Sentiment'][0],data=dataset.loc[:9])
    plt.show()

IIUC 先explode “情緒”,然后是 plot:

df = df.explode('Sentiment')
ax = sns.stripplot(x="Symbol", y="Sentiment", data=df)

樣本數據:

np.random.seed(5)
df = pd.DataFrame({
    'Symbol': ['AMC', 'GME', 'BB', 'SPY', 'SPCE'],
    'Mentions': [557, 175, 126, 43, 35]
})

df['Sentiment'] = df['Mentions'].apply(lambda x: (np.random.random(x) * 2) - 1)
  Symbol  Mentions                                          Sentiment
0    AMC       557  [-0.556013657820521, 0.7414646123547528, -0.58...
1    GME       175  [-0.5673003921341209, -0.6504850189478857, 0.1...
2     BB       126  [0.7771316020052821, 0.26579994709269994, -0.4...
3    SPY        43  [-0.5966607678089173, -0.4473484233894889, 0.7...
4   SPCE        35  [0.7934741289205556, 0.17613102678923398, 0.58...

結果圖:

圖形


帶有示例數據的完整工作示例:

import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

np.random.seed(5)
df = pd.DataFrame({
    'Symbol': ['AMC', 'GME', 'BB', 'SPY', 'SPCE'],
    'Mentions': [557, 175, 126, 43, 35]
})

df['Sentiment'] = df['Mentions'].apply(lambda x: (np.random.random(x) * 2) - 1)

df = df.explode('Sentiment')
ax = sns.stripplot(x="Symbol", y="Sentiment", data=df)
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM