Creating a cluster histogram for two columns in bokeh

I have a data frame that looks like this:

       type    price1     price2
0        A     5450.0     31980.0
1        B     5450.0     20000.0
2        C     15998.0    18100.0

What I want is a cluster histogram that displays a β€œtype” versus a β€œprice”. The ultimate goal is a chart that has two bars for each type, one bar for "price1" and the other for "price2". Both columns are in the same unit ($). Using Bokeh, I can group by type, but I don't seem to group the unit β€œprice”. I have this code:

import pandas as pd
import numpy as np
from bokeh.charts import Bar, output_file, show
from bokeh.palettes import Category20 as palette
from bokeh.models import HoverTool, PanTool
p = Bar(
        df,
        plot_width=1300,
        plot_height=900,
        label='type',
        values='price2',
        bar_width=0.4,
        legend='top_right',
        agg='median',
        tools=[HoverTool(), PanTool()],
        palette=palette[20])

But for each type, only one column is assigned to me. enter image description here

How can I change my code to get two columns for each type?

+4
source share
2

"" . , pd.melt(). "Seaborn" . .

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

your_df = pd.DataFrame(data={'type': ['A','B','C'],
      'price1':[5450, 5450, 15998],
      'price2' : [3216, 20000, 15000]})

long_df = pd.melt(your_df,id_vars = ['type'],value_vars =['price1','price2'])
print long_df

my_plot = sns.barplot(x="type", y="value",hue = "variable", data=long_df)
sns.plt.show()

enter image description here : Pandas

, , , renzop:

p = Bar(long_df,
    plot_width=1000,
    plot_height=800,
    label='type',
    values='value',
    bar_width=0.4,
    group='variable',
    legend='top_right')

show(p)
+2

, , grouped Bar plot.

, bokeh ( Pandas) .

df2 = pd.DataFrame(data={'type': ['A','B','C', 'A', 'B', 'C'],
          'price':[5450, 5450, 15998, 3216, 20000, 15000],
          'price_type':['price1', 'price1', 'price1', 'price2', 'price2', 'price2']})

p = Bar(
        df2,
        plot_width=1300,
        plot_height=900,
        label='type',
        values='price',
        bar_width=0.4,
        group='price_type',
        legend='top_right')
  show(p)

result

+4

Source: https://habr.com/ru/post/1667611/


All Articles