我正在尝试将绘图转换为Seaborn
,但是我遇到了多个barplot的问题。数据是列表的列表,如下所示:
raw_data = [[47.66773437098896, 47.585408826566024, 45.426437828641106, 44.955787935926836],
[47.700582993718115, 47.59796443553682, 45.38896827262796, 44.80916093973529],
[47.66563311651776, 47.476571906259835, 45.21460968763448, 44.78683755963528],
[47.248523637295705, 47.42573841363118, 45.52890109500238, 45.10243082784969],
[47.14532745960979, 47.46958795222966, 45.4804195003332, 44.97715435208194],
[46.61620129160194, 47.316775886868584, 45.053032014046366, 44.527497508033704]]
我的简单seaborn脚本如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def plot_sns(raw_data):
data = np.array(raw_data)
x = np.arange(len(raw_data))
width = 0.2 # width of bar
sns.axes_style('white')
sns.set_style('white')
ax = sns.barplot(x, data[:,0])
我想并排添加更多条形图(每组4条形图)。在Matplotlib中,我通过在右边间隔另一个条形来实现此目的,但是在Seaborn中,它不起作用。我看到的所有示例都使用了Pandas数据框。
答案 0 :(得分:1)
seaborn.barplot
还可以绘制字典,允许对数据进行分组。要对其进行正确分组,您需要添加 4 个类别,因为您希望并排放置 4 个条形图。您还需要相应地重新排列数据:
import seaborn as sn
raw_data = {
# cat: A B C D
'x': ['Group 1', 'Group 1', 'Group 1', 'Group 1',
'Group 2', 'Group 2', 'Group 2', 'Group 2',
'Group 3', 'Group 3', 'Group 3', 'Group 3',
'Group 4', 'Group 4', 'Group 4', 'Group 4',
'Group 5', 'Group 5', 'Group 5', 'Group 5',
'Group 6', 'Group 6', 'Group 6', 'Group 6'],
'y': [47.66773437098896, 47.585408826566024, 45.426437828641106, 44.955787935926836,
47.700582993718115, 47.59796443553682, 45.38896827262796, 44.80916093973529,
47.66563311651776, 47.476571906259835, 45.21460968763448, 44.78683755963528,
47.248523637295705, 47.42573841363118, 45.52890109500238, 45.10243082784969,
47.14532745960979, 47.46958795222966, 45.4804195003332, 44.97715435208194,
46.61620129160194, 47.316775886868584, 45.053032014046366, 44.527497508033704],
'category': ['A', 'B', 'C', 'D','A', 'B', 'C', 'D','A', 'B', 'C', 'D','A', 'B', 'C', 'D','A', 'B', 'C', 'D','A', 'B', 'C', 'D']
}
sb.barplot(x='x', y='y', hue='category', data=raw_data)
x
和 category
字段为每个值分配一个组和类别,以便 barplot
可以对这些值进行分组。