Matplotlib: How to mirror an image with respect to both axes?

笑着哭i 提交于 2019-12-08 14:06:27

问题


I am working with NetworkX in order to evaluate the dynamics that occur in a network. Then I visually represent the result. I work with the following:

#Step 1: Creating the original network
import networkx as nx
import matplotlib.pyplotas plt
N=100
G=nx.grid_2d_graph(N,N) #2D regular graph of 10000 nodes
pos = dict( (n, n) for n in G.nodes() ) #Dict of positions
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)
pos = {y:x for x,y in labels.iteritems()} #Renamed positions. Position (0,0) is in the upper left corner now.
H=G.copy()

#Step 2: Dynamics modeling block

#Step 3: Plotting the resulting network
G2=dict((k, v) for k, v in status_nodes_model.items() if v < 1) #All nodes that must be removed from the regular grid
H.remove_nodes_from(G2)
nx.draw_networkx(H, pos=pos, with_labels=False, node_size = 10)
        plt.xlim(-20,120,10)
        plt.xticks(numpy.arange(-20, 130, 20.0))
        plt.ylim(-20,120,10) 
        plt.yticks(numpy.arange(-20, 130, 20.0))
        plt.axis('on')
        plt.plot((0, 100), (0, 0), '0.60') #Line1 - visual boundary for failure stages
        plt.plot((0, 0), (0, 100), '0.60') #Line2 - visual boundary for failure stages
        plt.plot((0, 100), (100, 100), '0.60') #Line3 - visual boundary for failure stages
        plt.plot((100, 100), (0, 100), '0.60') #Line4 - visual boundary for failure stages
        title_string=('Lattice Network, Stage 2') 
        subtitle_string=('Impact & dynamics | Active nodes: '+str(act_nodes_model)+' | Failed nodes: '+str(failed_nodes_haz+failed_nodes_model)+' | Total failure percentage: '+str(numpy.around(100*(failed_nodes_haz+failed_nodes_model)/10000,2))+'%')
        plt.suptitle(title_string, y=0.99, fontsize=17)
        plt.title(subtitle_string, fontsize=8)

What I visually get is an UNWANTED MIRRORING. Please see below:

My question: is there a way of mirroring the output image both horizontally and vertically, so that the image to the right is obtained, but with readable titles and labels?


回答1:


You just need to change the values in pos.

my_pos = {}
for key in pos.keys():
     x,y = pos[key]
     my_pos[key] = (-x,-y)#or whatever function you want
nx.draw_networkx(H, pos=my_pos, with_labels=False, node_size = 10)


来源:https://stackoverflow.com/questions/33826967/matplotlib-how-to-mirror-an-image-with-respect-to-both-axes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!