简体   繁体   English

如何在 matplotlib 补丁集合中设置限制

[英]How to set limits in a matplotlib patchcollection

I am using the following piece of code to display a network where the edges are colored according to their weight:我正在使用以下代码来显示一个网络,其中边缘根据其权重着色:

import matplotlib as mpl
import matplotlib.pyplot as plt
import networkx as nx

seed = 13648  # Seed random number generators for reproducibility
G = nx.random_k_out_graph(10, 3, 0.5, seed=seed)
pos = nx.spring_layout(G, seed=seed)

node_sizes = [3 + 10 * i for i in range(len(G))]
M = G.number_of_edges()
edge_colors = range(2, M + 2)
edge_alphas = [(5 + i) / (M + 4) for i in range(M)]
cmap = plt.cm.plasma

nodes = nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color="indigo")
edges = nx.draw_networkx_edges(
    G,
    pos,
    node_size=node_sizes,
    arrowstyle="->",
    arrowsize=10,
    edge_color=edge_colors,
    edge_cmap=cmap,
    width=2,
)
# set alpha value for each edge
for i in range(M):
    edges[i].set_alpha(edge_alphas[i])

pc = mpl.collections.PatchCollection(edges, cmap=cmap)
pc.set_array(edge_colors)
plt.colorbar(pc)

ax = plt.gca()
ax.set_axis_off()
plt.show()

It yields the following graph:它产生以下图表:

具有默认颜色图限制设置的图形

I want to manually change the limits of the colormap.我想手动更改颜色图的限制。 To do so I use:为此,我使用:

pc.set_clim(vmin=0, vmax=60)

It yields the following graph:它产生以下图表:

在此处输入图像描述

The problem is, that the colorbar does change according to the limits I set, but the color of the edges is exactly the same, which means they are displaying incorrect values.问题是,颜色条确实会根据我设置的限制发生变化,但边缘的颜色完全相同,这意味着它们显示的值不正确。 How can I fix this?我怎样才能解决这个问题?

Ok I was able to solve it, you can set limits in draw_networkx_edges as好的,我能够解决它,您可以在 draw_networkx_edges 中设置限制为

edges = nx.draw_networkx_edges(
    G,
    pos,
    node_size=node_sizes,
    arrowstyle="->",
    arrowsize=10,
    edge_color=edge_colors,
    edge_cmap=cmap,
    width=2,
    edge_vmin=0, edge_vmax=60
)

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

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