简体   繁体   English

如何在 Matplotlib 中找到定性颜色图的颜色限制?

[英]How do I find color limits for qualitative colormaps in Matplotlib?

I have a following code that generates a scatter plot:我有以下代码生成散点图 plot:

plt.scatter(df['column_A'], y= df['column_B'], 
            alpha = .6, c = df['column_C'], cmap = 'Accent')

It generates this chart where points are colored based on values from 'column_C'它生成此图表,其中的点根据“column_C”中的值着色

I am using qualitative colormap in cmap which automatically creates intervals on column_C and color the points accordingly.我在 cmap 中使用定性颜色图,它会自动在 column_C 上创建间隔并相应地为点着色。 I can visually say that the grey group ranges from around 10 to the maximum in data (around 12) but I would like to access the exact numbers.我可以直观地说灰色组的范围从大约 10 到数据中的最大值(大约 12),但我想访问确切的数字。 Is there a way to do that?有没有办法做到这一点?

Thanks a lot非常感谢

By default, the colors range from the smallest to the largest values of column_C .默认情况下,colors 的范围从column_C的最小值到最大值。 For the colormap with N colors, there will be N segments, thus N+1 borders which can be calculated with np.linspace() .对于具有N colors 的颜色图,将有N个段,因此可以使用np.linspace()计算N+1边界。

Here is some illustrating code.这是一些说明性代码。 Both a colorbar with default ticks and one with ticks at the color borders are added.添加了一个带有默认刻度的颜色条和一个在颜色边界处带有刻度的颜色条。

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

df = pd.DataFrame({'column_A': np.random.rand(100),
                   'column_B': np.random.rand(100),
                   'column_C': np.random.rand(100) * 20 - 8})
cmap = plt.get_cmap('Accent')
plt.scatter(df['column_A'], y=df['column_B'],
            alpha=.6, c=df['column_C'], cmap=cmap)
plt.colorbar()  # the default color bar
color_borders = np.linspace(df['column_C'].min(), df['column_C'].max(), cmap.N + 1)
plt.colorbar(ticks=color_borders)  # ticks at the color borders
plt.show()

在颜色栏中查找颜色边框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM