import networkx as nx


def nxMarkovGraph(self, all_vars=False):
    """Get networkx Graph object of the Markov graph of the model

    Example:
    >>> G = nxMarkovGraph(model)
    >>> nx.draw(G)
    """
    G = nx.Graph()
    G.add_nodes_from( [v.label for v in self.X if (all_vars or v.states > 1)] )
    for f in self.factors:
      for v1 in f.vars:
        for v2 in f.vars:
          if (v1 != v2) and (all_vars or (v1.states > 1 and v2.states > 1)):
            G.add_edge(v1.label,v2.label)
    return G
    """ Plotting examples:
    fig,ax=plt.subplots(1,2)
    pos = nx.spring_layout(G) # so we can use same positions multiple times...
    # use nodelist=[nodes-to-draw] to only show nodes in model
    nx.draw(G,with_labels=True,labels={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6'},
              node_color=[.3,.3,.3,.7,.7,.7,.7],vmin=0.0,vmax=1.0,pos=pos,ax=ax[0])
    nx.draw(G,with_labels=True,labels={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6'},
              node_color=[.3,.3,.3,.7,.7,.7,.7],vmin=0.0,vmax=1.0,pos=pos,ax=ax[1])
    """