随x和色相变化颜色

随x和色相变化颜色

本文介绍了seaborn barplot:随x和色相变化颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集包含有关决策支持模型的短期和长期影响的信息.我想将其绘制在具有4条的条形图中:

  • 短期内建立模型
  • 模型,长期
  • 模拟,短期
  • 长期建模

下面是一些示例代码:

  df = pd.DataFrame(columns = ["model","time","value"])df ["model"] = ["on"] * 2 + ["off"] * 2df ["time"] = ["short","long"] * 2df["值"] = [1, 10, 2, 4]sns.barplot(data = df,x ="model",hue ="time",y ="value")plt.show()

看起来像这样:

还有许多其他相关图形,它们已经建立了颜色约定.型号开/关以颜色的色调编码.长期与短期以颜色的饱和度进行编码.所以让我们假设我已经给出了带有颜色值的变量.如何为条形图中的每个条形分配单独的颜色?

seaborn.barplot 的

My dataset contains information on the short-term and long-term effects of a decision support model. I would like to plot this in a barplot, with 4 bars:

  • model on, short term
  • model on, long term
  • model off, short term
  • model on, long term

here is some sample code:

df = pd.DataFrame(columns=["model", "time", "value"])
df["model"] = ["on"]*2 + ["off"]*2
df["time"] = ["short", "long"] * 2
df["value"] = [1, 10, 2, 4]

sns.barplot(data=df, x="model", hue="time", y="value")
plt.show()

it looks like this:

There are many other related figures and they have established color conventions. Model on/off is encoded in the hue of the color. Longterm vs shortterm is encoded in the saturation of the color. So let's assume that I have given variables with color values. How can I assign each individual bar in the barplot an individual color?

The docs for seaborn.barplot only show color, which specifies one color for all elements and palette which only gives different hue values different colors.

解决方案

The existing answer shows a nice way on how to arrange barplots with pyplot.

Unfortunately, my code relies heavily on other seaborn functionality, such as error bars, etc. So I would prefer to be able to keep the seaborn barplot functionality and just specify my own colors.

It is possible to iterate over the bars in a seaborn barplot as matplotlib patches. That allows setting a color, hatch, etc: Is it possible to add hatches to each individual bar in seaborn.barplot?

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

df = pd.DataFrame(columns=["model", "time", "value"])
df["model"] = ["on"]*2 + ["off"]*2
df["time"] = ["short", "long"] * 2
df["value"] = [1, 10, 2, 4]

fig, ax = plt.subplots()
bar = sns.barplot(data=df, x="model", hue="time", y="value", edgecolor="white")


colors = ["red", "green", "blue", "black"]
# Loop over the bars
for i,thisbar in enumerate(bar.patches):
    # Set a different hatch for each bar
    thisbar.set_color(colors[i])
    thisbar.set_edgecolor("white")

However, if you do this, it will not update the legend. You can use the following code to create a custom legend. It is complicated, because I need multiple color patches for every legend entry. This is apparently quite complicated to do: Python Matplotlib Multi-color Legend Entry

# add custom legend
ax.get_legend().remove()
legend_pos = np.array([1, 10])
patch_size = np.array([0.05, 0.3])
patch_offset = np.array([0.06, 0])

r2 = mpatches.Rectangle(legend_pos, *patch_size, fill=True, color='red')
r3 = mpatches.Rectangle(legend_pos + patch_offset, *patch_size, fill=True, color='blue')
ax.add_patch(r2)
ax.add_patch(r3)
ax.annotate('Foo', legend_pos + 3* patch_offset - [0, 0.1], fontsize='x-large')

plt.show()

这篇关于seaborn barplot:随x和色相变化颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:35