I'm seeing a weird behavior with a Seaborn barplot. I am using a script that I verified that it works with one data frame. When I concatenate multiple data frames and use groupby
, the barplot is coming out white, ie, the color_palette
is no longer working.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.collections import PolyCollection as p
import seaborn as sns
sns.set(font_scale=1.5, style='white', context='paper')
def plot_consumers(count, df):
print(count.groupby(['periods'], as_index=False)[
'consumerId'].mean().describe())
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 3))
pal = sns.color_palette('Blues_d', n_colors=1)
sns.barplot(x='periods',
y='consumerId',
data=count.groupby(['periods'], as_index=False)[
'consumerId'].mean(),
ax=axes[0],
palette=pal)
sns.lineplot(x='periods',
y='distance',
data=df.groupby(['periods'], as_index=False)[
'distance'].mean(),
legend=False,
ax=axes[1])
# Axes config
axes[0].set(ylim=(-0.05, 100.05))
axes[0].set(ylabel='%')
axes[0].set(xlim=(-10, 310))
axes[0].xaxis.set_major_locator(ticker.MultipleLocator(100))
axes[0].xaxis.set_major_formatter(ticker.ScalarFormatter())
axes[1].set(ylabel='customer satisfaction')
axes[1].set(ylim=(-0.05, 1.05))
fig.tight_layout()
plt.show()
After I group the count
data frame, I get the following:
periods consumerId
count 300.000000 300.000000
mean 149.500000 21.540741
std 86.746758 0.175113
min 0.000000 19.666667
25% 74.750000 21.555556
50% 149.500000 21.555556
75% 224.250000 21.555556
max 299.000000 23.111111
I know that the bars are being plotted because I changed the style to dark
and I can see the bars in white.
If I change the barplot to a lineplot it also works.
根据@mwaskom的说法,问题是因为我试图在较短的空间中容纳太多的钢筋。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.