最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - reverse order of bars in seaborn stacked barplot - Stack Overflow

programmeradmin5浏览0评论

i have a plot like this:

now I want to inverse the order of the stacked bars without changing the color-value-mapping: low value->red, high_value-> green. in short: lowest bar is red with value 1 and the top par is green with value 10.

basically it should look like this (but without the inverted yaxis...)

here is some sample code to generate the data and the plot...

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

#dummy data
relative_amount_bad_scores = np.random.rand(1000)
aggregated_score = np.random.choice(
    np.arange(1, 11), 1000, p=[0.05, 0.05, 0.1, 0.1, 0.15, 0.15, 0.15, 0.1, 0.1, 0.05]
)
df_dummy = pd.DataFrame({
    "relative_amount_of_bad_scores": relative_amount_bad_scores,
    "aggregated_score": aggregated_score
})


# Plot
palette = sns.color_palette("RdYlGn", as_cmap=True)
fig, ax = plt.subplots(figsize=(15, 15))
sns.histplot(
    data=df_dummy.sort_values('aggregated_score', ascending = True),
    x="relative_amount_of_bad_scores",
    hue="aggregated_score",
    multiple="fill",
    ax=ax,
    binwidth=0.1,
    palette = palette
)
发布评论

评论列表(0)

  1. 暂无评论