python - Matplotlib savefig() on a zoomed in graph -
drawing huge graph networkx , matplotlib
i'm reasking linked question. think can better job explaining question. mathplotlib.show() called on large graph, default zoomed out, clustered output. desired endstate use mathplotlib.savefig() save plot use in report. however, savefig() output zoomed out, general. changing image size or dpi not fix this. makes zoomed out image bigger. there way zoom in graph , save without using ui? ui, can zoom in, spread nodes out, , center around node in question, not know how automatically.
relevant code:
nx.draw(g,pos,node_color=colorvalues, with_labels = false,node_size=values) fig.set_size_inches(11,8.5) if show ==0: plt.show() if show ==1: plt.savefig(name+" coremem.png",bbox_inches=0,orientation='landscape',pad_inches=0.1)
you use ax.set_xlim
, ax.set_ylim
set x
, y
ranges of plot. example,
import networkx nx import matplotlib.pyplot plt import numpy np filename = '/tmp/graph.png' g = nx.complete_graph(10) pos = nx.spring_layout(g) xy = np.row_stack([point key, point in pos.iteritems()]) x, y = np.median(xy, axis=0) fig, ax = plt.subplots() nx.draw(g, pos, with_labels=false, node_size=1) ax.set_xlim(x-0.25, x+0.25) ax.set_ylim(y-0.25, y+0.25) plt.savefig(filename, bbox_inches=0, orientation='landscape', pad_inches=0.1)
to find out original limits (before calling ax.set_xlim
, ax.set_ylim
), use
>>> ax.get_xlim() (-0.20000000000000001, 1.2000000000000002) ax.get_ylim() (-0.20000000000000001, 1.2000000000000002)
Comments
Post a Comment