Seaborn barplot from lists instead of dataframes
Asked Answered
C

1

6

I'm being trying to convert my plots to Seaborn and I'm having an issue with a multiple barplot that I have. The data is a list of lists, like so:

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]]

My simple seaborn script is as follows:

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])

This yields the following plot: enter image description here

I would like to add more bars to the graph, side-by-side (4 bars per group). In Matplotlib, I do that by plotting another bar spaced to the right, but in Seaborn it didn't work. All the examples I saw used Pandas dataframes.

Crossexamine answered 30/11, 2018 at 16:54 Comment(5)
Would you get the desired output via pd.DataFrame(raw_data).plot.bar() ?Chevrette
Can I only use Seaborn with Pandas?Crossexamine
Seen from the code in the question you may use seaborn without pandas. But my previous comment was rather meant to ask why to use seaborn at all? Wouldn't you get the desired output with pandas alone?Chevrette
The above snippet works as intended. Since I have other plots in mind that will look very good with Seaborn, I'm want to perform the above using Seaborn.Crossexamine
The reason to use seaborn for the kind of bar plot you want would be to be able to easily plot a long-form dataframe. Here you have a wide-form numpy array. Of course you can convert it to a long-form dataframe with pandas. But there really is no reason to do so.Chevrette
R
2

seaborn.barplot can also plot dictionaries, which allows data to be grouped. To group it correctly, you need to add 4 categories as you want to have 4 bars side-by-side. You also need to rearrange the data accordingly:

import seaborn as sns

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']
           }

sns.barplot(x='x', y='y', hue='category', data=raw_data)

The x and category fields assign a group and category for each value so that the barplot can group the values.

enter image description here

Ringe answered 7/5, 2021 at 20:15 Comment(1)
Perhaps, 'category': ['A', 'B', 'C', 'D'] * n_groups might be more concise.Service

© 2022 - 2024 — McMap. All rights reserved.