Matplotlib 中颜色图的设置范围

发布于 2024-12-07 06:39:11 字数 321 浏览 0 评论 0原文

我正在使用 matplotlib 绘制一个简单的图形:

cm=plt.get_cmap('Blues')

nx.draw_circular(G,
        node_color='White',
        edge_color=range(G.number_of_edges()),
        edge_cmap=cm,
        node_size=900,
        width=4
        )

我想在颜色图“蓝色”上设置一个范围,以删除绘图中不可见的白色。

请帮忙!

抱歉英语不好。

I'm using matplotlib to plot a simple graph:

cm=plt.get_cmap('Blues')

nx.draw_circular(G,
        node_color='White',
        edge_color=range(G.number_of_edges()),
        edge_cmap=cm,
        node_size=900,
        width=4
        )

I want to set a range on the colormap 'Blues' in such a way to delete the white color which is not visible in the draw.

Please help!

Sorry for bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-12-14 06:39:11

范围(或规范化)实际上并不是颜色图,但通常作为使用颜色图绘制的函数中的一个功能来实现。例如, imshow 使用 vmin< /code> 和 vmax,因此您可以尝试将它们用作 draw_circular 的关键字(我找不到文档),或者也许 norm

除此之外,您可以使用您想要的精确颜色排列来制作自己的颜色图。有很多关于如何制作自定义颜色图的示例,以及一些不同的方法可用。此处(ab, c, d) 是一些可能对您有用的示例。

The range (or normilization) is not really a feature of the colormap, but is often implemented as a feature in the functions that plot using colormaps. For example, imshow uses vmin and vmax, so you might try using these as keywords with draw_circular (I can't find the documentation), or maybe norm.

Other than this, you can make your own colormap with exact color arrangement that you want. There are plenty of examples on how to make custom colormaps, and a few different approaches available. Here (a, b, c, d) are a few examples that might be useful to you.

青春如此纠结 2024-12-14 06:39:11

我在尝试使用不同的颜色图绘制数据时遇到了这个问题:

Blues vs Reds

很难判断哪个白色点属于哪个分布。我通过砍掉颜色图谱的较白部分解决了这个问题:

蓝队 vs 红队,删除白色

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap


def chop_cmap_frac(cmap: LinearSegmentedColormap, frac: float) -> LinearSegmentedColormap:
    """Chops off the beginning `frac` fraction of a colormap."""
    cmap_as_array = cmap(np.arange(256))
    cmap_as_array = cmap_as_array[int(frac * len(cmap_as_array)):]
    return LinearSegmentedColormap.from_list(cmap.name + f"_frac{frac}", cmap_as_array)


cmap1 = plt.get_cmap('Reds')
cmap2 = plt.get_cmap('Blues')

cmap1 = chop_cmap_frac(cmap1, 0.4)
cmap2 = chop_cmap_frac(cmap2, 0.4)
np.random.seed(42)
n = 50
xs = np.random.normal(size=n)
ys = np.random.normal(size=n)
vals = np.random.uniform(size=n)

plt.scatter(xs, ys, c=vals, cmap=cmap1)
plt.scatter(ys, xs, c=vals, cmap=cmap2)
plt.gca().set_facecolor('black')
plt.colorbar()
plt.show()

I ran into this problem trying to plot data with different colormaps:

Blues vs Reds

It's hard to which of the whitish dots belong to which distribution. I solved this problem by chopping off the whiter parts of the colormap spectrum:

Blues vs Reds with white removed

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap


def chop_cmap_frac(cmap: LinearSegmentedColormap, frac: float) -> LinearSegmentedColormap:
    """Chops off the beginning `frac` fraction of a colormap."""
    cmap_as_array = cmap(np.arange(256))
    cmap_as_array = cmap_as_array[int(frac * len(cmap_as_array)):]
    return LinearSegmentedColormap.from_list(cmap.name + f"_frac{frac}", cmap_as_array)


cmap1 = plt.get_cmap('Reds')
cmap2 = plt.get_cmap('Blues')

cmap1 = chop_cmap_frac(cmap1, 0.4)
cmap2 = chop_cmap_frac(cmap2, 0.4)
np.random.seed(42)
n = 50
xs = np.random.normal(size=n)
ys = np.random.normal(size=n)
vals = np.random.uniform(size=n)

plt.scatter(xs, ys, c=vals, cmap=cmap1)
plt.scatter(ys, xs, c=vals, cmap=cmap2)
plt.gca().set_facecolor('black')
plt.colorbar()
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文