本文介绍了`ListedColormap` 列表导致所有黑条 - matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出一个通过该值控制暗度(或明度)的图,因此例如:值20将是较深的颜色,值5将是较浅的颜色,但是我想要相同的值数字具有相同的颜色(并且易于查看......),到目前为止我尝试了 ListedColormap(如前所述 )只能让我了解所有黑条,这是代码:

I would like to output a plot that which controls darkness (or lightness) by the value, so example: value of 20 would be a darker color and value of 5 would be a lighter color, but I would as want the same numbers to have the same colors (and easy to see...), so far I tried ListedColormap (as mentioned here) which only got me as far as all black bars, here is the code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
df = pd.DataFrame({'values': [0, 0, 0, 0, 0, 17, 16, 16, 15, 15, 15, 14, 13, 13, 13]})
colors = ListedColormap([str((i*4) / 100) for i in df['values'].replace(0, 1)])
df.plot(kind='barh', colormap=colors)
plt.show()

推荐答案

您需要提供颜色列表.在这里,使用 matplotlib:

You will need to supply a list of colors. Here, using matplotlib:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'values': [0, 0, 0, 0, 0, 17, 16, 16, 15, 15, 15, 14, 13, 13, 13]})
colors = [str((i*4) / 100) for i in df['values'].replace(0, 1)]
plt.barh(df.index, df['values'].values, color=colors)
plt.show()

这篇关于`ListedColormap` 列表导致所有黑条 - matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 08:05