Matplotlib:如何镜像两个轴的图像?

时间:2015-11-20 12:38:25

标签: python image matplotlib networkx mirroring

我正在使用NetworkX来评估网络中发生的动态。然后我直观地表示结果。我使用以下内容:

#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)

我在视觉上得到的是一个不受欢迎的镜像。请看下面: enter image description here

我的问题:是否有办法镜像输出图像水平和垂直,以便获得右边的图像,但标题和标签可读?

1 个答案:

答案 0 :(得分:1)

您只需更改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)