Построение нескольких гистограмм - PullRequest
1 голос
/ 09 апреля 2020

Я хочу создать гистограмму с акцентом на два города. Мой набор данных похож на этот.

    city      rate     Bedrooms
    Houston 132.768382      0
    Dallas  151.981043      1
    Dallas  112.897727      3
    Houston 132.332665      1
    Houston 232.611185      2
    Dallas  93.530662       4

Я разбил их на рамки данных только из Далласа и Хьюстона. Как

dal.groupby('bedrooms')['rate'].mean().plot(kind='bar')

&

hou.groupby('bedrooms')['rate'].mean().plot(kind='bar') 

Как бы я go о создании гистограммы, которая перечисляет среднюю скорость списков на основе типа спальни. Нечто похожее на это изображение ниже, что я нашел здесь Python matplotlib несколько баров . С этикетками, являющимися городами.

Буду признателен за любую помощь!

enter image description here

Ответы [ 4 ]

3 голосов
/ 09 апреля 2020

Seaborn - ваш друг в этом случае, сначала создайте сгруппированный массив данных со средним значением rate для каждого города и спален и подготовьте график с использованием seaborn

import seaborn as sns

dal_group = dal.groupby(['city' , 'Bedrooms']).agg({'rate': 'mean'}).reset_index()
sns.barplot(data=dal_group, x='Bedrooms', y='rate', hue='city')

с данными, приведенными выше, он произведет это участок:

enter image description here

1 голос
/ 09 апреля 2020

Существует простое решение, используя только одну строку pandas (если вы сначала переставляете данные) или используя plotly

Данные

import pandas as pd
df =  pd.DataFrame({'city': {0: 'Houston',
  1: 'Dallas',
  2: 'Dallas',
  3: 'Houston',
  4: 'Houston',
  5: 'Dallas'},
 'rate': {0: 132.768382,
  1: 151.981043,
  2: 112.897727,
  3: 132.332665,
  4: 232.611185,
  5: 93.530662},
 'Bedrooms': {0: 0, 1: 1, 2: 3, 3: 1, 4: 2, 5: 4}})

# groupby
df = df.groupby(["city", "Bedrooms"])["rate"].mean().reset_index()

Pandas - Matplotlib

С помощью pivot_table мы можем переставить наши данные

pv = pd.pivot_table(df, 
               index="Bedrooms",
               columns="city",
               values="rate")
city          Dallas     Houston
Bedrooms                        
0                NaN  132.768382
1         151.981043  132.332665
2                NaN  232.611185
3         112.897727         NaN
4          93.530662         NaN

И затем построить только одну строку.

pv.plot(kind="bar");

enter image description here

Использование Plotly

import plotly.express as px

px.bar(df, x="Bedrooms", y="rate", color="city",barmode='group')

enter image description here

1 голос
/ 09 апреля 2020

Вот базовый c способ сделать это в matplotlib:

import numpy as np
import matplotlib.pyplot as plt


data_dallas = dal.groupby('bedrooms')['rate'].mean()
data_houston = hou.groupby('bedrooms')['rate'].mean()

fig, ax = plt.subplots()

x = np.arange(5)  # if the max. number of bedrooms is 4
width = 0.35      # width of one bar

dal_bars = ax.bar(x, data_dallas, width)
hou_bars = ax.bar(x + width, data_houston, width)

ax.set_xticks(x + width / 2)
ax.set_xticklabels(x)
ax.legend((dal_bars[0], hou_bars[0]), ('Dallas', 'Houston'))

plt.show()
1 голос
/ 09 апреля 2020

Подробнее можно прочитать здесь: https://pythonspot.com/matplotlib-bar-chart/


import numpy as np
import matplotlib.pyplot as plt

# data to plot
n_groups = # of data points for each
mean_rates_houston = [average rates of bedrooms for Houston]
mean_rates_dallas = [average rates of bedrooms for Dalls]

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8

rects1 = plt.bar(index, mean_rates_dallas, bar_width,
alpha=opacity,
color='b',
label='Dallas')

rects2 = plt.bar(index + bar_width, mean_rates_houston, bar_width,
alpha=opacity,
color='g',
label='Houston')

plt.xlabel('City')
plt.ylabel('Rates')
plt.title('Bedroom Rates per City')

# whatever the number of bedrooms in your dataset might be: change plt.xticks
plt.xticks(index + bar_width, ('0', '1', '2', '3')) 

plt.legend()
plt.tight_layout()
plt.show()
...