英文:
Select the color of the bar in histogram plot based on its value
问题
我有成千上万条数据,我想绘制它们的直方图。我希望根据直方图的值来设置不同的颜色。我的值在 0-10 之间。所以,我想将柱状图的颜色从红色渐变到绿色。如果值接近零,颜色应为红色,如果接近10,颜色应为绿色,就像我附上的图像一样。在以下示例中,我希望将行 h 的颜色设置为接近绿色,而行 b 的颜色设置为接近红色。这是一个简单的示例,我有多个柱形和值。
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
rating = [8, 4, 5, 6]
objects = ('h', 'b', 'c', 'a')
y_pos = np.arange(len(objects)
# 定义颜色映射
colors = plt.cm.RdYlGn(np.linspace(0, 1, len(rating)))
plt.barh(y_pos, rating, align='center', alpha=0.5, color=colors)
plt.yticks(y_pos, objects)
plt.show()
英文:
I have thousands of data that I want to plot the histogram of them. I want to put the different colors based on the values of the histogram. My values are between 0-10. So, I want to put the color of the bar from red to green. And if it is close to zero, the color should be red and if it is close to 10, the color should be green. Like the image I attached. In the following example, I want to set the color of row h as close to green, and the b is close to red. Here is a simple example, I have multiple bars and values.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
rating = [8, 4, 5,6]
objects = ('h', 'b', 'c','a')
y_pos = np.arange(len(objects))
plt.barh(y_pos, rating, align='center', alpha=0.5)
plt.yticks(y_pos, objects)
plt.show()
答案1
得分: 3
Matplotlib 使用 colormap 与 norm 来根据数值应用颜色。Colormap 将数值从 0 到 1 映射到颜色,例如 0 对应绿色,0.5 对应黄色,1 对应红色。Norm 将数值从给定范围映射到 0 到 1 的范围,例如,将最小值映射为 0,最大值映射为 1。将 colormap 应用于给定数值的 norm,然后可以得到所需的颜色。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
rating = [8, 4, 5, 6]
objects = ('h', 'b', 'c', 'a')
y_pos = np.arange(len(objects))
cmap = plt.get_cmap('RdYlGn_r')
norm = plt.Normalize(vmin=min(rating), vmax=max(rating))
plt.barh(y_pos, rating, align='center', color=cmap(norm(np.array(rating)))
plt.yticks(y_pos, objects)
plt.show()
或者,也可以使用 seaborn 库来实现一个稍微简单的方法:
import seaborn as sns
rating = [8, 4, 5, 6]
objects = ['h', 'b', 'c', 'a']
ax = sns.barplot(x=rating, y=objects, hue=rating, palette='RdYlGn_r', dodge=False)
英文:
To apply colors depending on values, matplotlib uses a colormap combined with a norm. The colormap maps values between 0 and 1 to a color, for example 0 to green, 0.5 to yellow and 1 to red. A norm maps values from a given range to the range 0 to 1, for example, the minimum value to 0 and the maximum value to 1. Applying the colormap to the norm of the given values then gives the desired colors.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
rating = [8, 4, 5, 6]
objects = ('h', 'b', 'c', 'a')
y_pos = np.arange(len(objects))
cmap = plt.get_cmap('RdYlGn_r')
norm = plt.Normalize(vmin=min(rating), vmax=max(rating))
plt.barh(y_pos, rating, align='center', color=cmap(norm(np.array(rating))))
plt.yticks(y_pos, objects)
plt.show()
Alternatively, the seaborn library could be used for a little bit simpler approach:
import seaborn as sns
rating = [8, 4, 5, 6]
objects = ['h', 'b', 'c', 'a']
ax = sns.barplot(x=rating, y=objects, hue=rating, palette='RdYlGn_r', dodge=False)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论