python - 将networkx图保存为pdf时的不同边宽
问题描述
问题:我想以矢量化格式(例如 pdf)保存 networkx 图,但这不能保留预期的边缘宽度。
示例:考虑以下代码段:
import networkx
import matplotlib.pyplot as plt
import numpy as np
# Define graph
G = nx.Graph()
for i in range(10):
for j in range(10):
weight = 1
if i != j:
if np.random.rand() < 0.7:
weight = 0.01
else:
weight = 0.5*np.random.rand()
G.add_edge('A{}'.format(i), 'B{}'.format(j), weight=weight)
# Plot graph
plt.figure(figsize=(7, 7))
left_nodes, right_nodes = nx.bipartite.sets(G)
pos = dict()
pos.update((n, (1, i)) for i, n in enumerate(left_nodes))
pos.update((n, (2, i)) for i, n in enumerate(right_nodes))
weights = [G[u][v]['weight'] for u, v in G.edges()]
color_map = ['lightblue' if n.startswith('A') else 'orange' for n in G]
nx.draw(G,
pos=pos,
width=weights,
node_color=color_map,
node_size=1000,
with_labels = True)
plt.savefig('test.png', bbox_inches='tight');
这会将以下图像保存为test.png
:
相反,当我使用可缩放矢量格式(例如,将图像另存为test.pdf
)时,输出为:
这不会保留预期的边缘宽度。如何以矢量格式保存绘图,使其看起来像 .png 图像?
解决方案
推荐阅读
- c# - Create a Scriptable Object Instance through a Constructor
- java - Arraylist 查找连续重复元素的计数
- sql - 有没有办法在 sql 中将 2018 年 8 月 31 日星期五 12:12 PM 转换为这种格式 2018-08-31 12:12 PM?
- java - 如何在放心中获得大于指定值的值
- apache-kafka - Kafka Streams:如何在 Streamer 代码中获取轮询批量大小 - 高级消费者
- android - Android EditText over NativeActivity:长按弹出点击在某些设备上不起作用
- excel - 使用索引匹配查找区域中的第二个匹配列
- c# - 在 Visual Studio 2017 C# 中找不到 Microsoft.Office.Interop.Access 的参考
- hadoop - 从 Hive 加载的 SAS 被截断为 32,767 个字符
- javascript - 如何在画布拖放中修复对象位置