Home > Back-end >  Seaborn, how sum time in barplot?
Seaborn, how sum time in barplot?

Time:04-01

pandas output:

          name                     type                 value
0         Town_1                  serv_time           8 days 07:14:44
1         Town_2                  serv_time           0 days 16:46:35
2         Town_3                  serv_time           0 days 22:39:27
3         Town_4                  serv_time           0 days 02:36:56
4         Town_5                  serv_time           0 days 11:17:45
[2022-04-01 15:18:22][ERROR] - Neither the `x` nor `y` variable appears to be numeric.

and on python:

Plot(
        type='bar',
        data=new_data,
        x='value',
        hue='type',
        y='name',
        style='whitegrid',

    )


def __create_bar(self):
    sns.set_palette('Set2')
    sns.despine()

    plot = sns.barplot(ax=self.ax, x=self.x, y=self.y, hue=self.hue, data=self.data)
    self.__set_legends()
    if self.show:
        return plt.show()
    return self.__save(plot)

i am trying do barplot in seaborn, but i get error numeric. How use sum time value in barplot seaborn?

CodePudding user response:

You need to convert value series to a numeric data type to plot it.

For example, assuming that value has a timedelta dtype. You can create another column in your dataframe.

new_data['value_seconds'] = new_data.value.dt.total_seconds()

Plot(
        type='bar',
        data=new_data,
        x='value_seconds',
        hue='type',
        y='name',
        style='whitegrid',

    )

CodePudding user response:

To do a bar plot in seaborn one needs sufficient data.

There was insufficient data in the question for seaborn to do this. In particular the error message stating that x or y needs to be numeric !

Neither the `x` nor `y` variable appears to be numeric.

An example of what would work:

import pandas as pd
import seaborn as sns

sns.set_theme(style="whitegrid")
d = {'name':['town_1', 'town_2', 'town_3', 'town_4'],
    'value A':[8, 0, 0.5, 1],
    'value B':[2, 4, 6, 8]}
df = pd.DataFrame(d)


ax = sns.barplot(x='name', y='value A',data=df)

And the result:

     name  value A  value B
0  town_1      8.0        2
1  town_2      0.0        4
2  town_3      0.5        6
3  town_4      1.0        8

enter image description here

  • Related