title: How to use the `pos` argument in `networkx` to create a flowchart-style Graph? (Python 3)
body: <p><strong>I am trying create a linear network graph using <code>Python</code></strong> (preferably with <code>matplotlib</code> and <code>networkx</code> although would be interested in <code>bokeh</code>) similar in concept to the one below.  </p>
  
  <p><a href="https://i.stack.imgur.com/qojHg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qojHg.png" alt="enter image description here"></a></p>
  
  <p><strong>How can this graph plot be constructed efficiently (<code>pos</code>?) in Python using <code>networkx</code>?</strong>  I want to use this for more complicated examples so I feel that hard coding the positions for this simple example won't be useful :( . Does <code>networkx</code> have a solution to this? </p>
  
  <blockquote>
    <p><a href="https://networkx.github.io/documentation/development/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html#networkx.drawing.nx_pylab.draw_networkx" rel="noreferrer">pos (dictionary, optional) – A dictionary with nodes as keys and
    positions as values. If not specified a spring layout positioning will
    be computed. See networkx.layout for functions that compute node
    positions.
    </a></p>
  </blockquote>
  
  <p>I haven't seen any tutorials on how this can be achieved in <code>networkx</code> which is why I believe this question will be a reliable resource for the community.  I've extensively gone through the <a href="https://networkx.github.io/documentation/development/tutorial/index.html" rel="noreferrer"><code>networkx</code> tutorials</a> and nothing like this is on there.  The layouts for <code>networkx</code> would make this type of network impossible to interpret without careful use of the <code>pos</code> argument... which I believe is my only option.  <strong>None of the precomputed layouts on the <a href="https://networkx.github.io/documentation/networkx-1.9/reference/drawing.html" rel="noreferrer">https://networkx.github.io/documentation/networkx-1.9/reference/drawing.html</a> documentation seem to handle this type of network structure well.</strong> </p>
  
  <p><strong>Simple Example:</strong></p>
  
  <p>(A) every outer key is the iteration in the graph moving from left to the right (e.g. iteration 0 represents samples, iteration 1 has groups 1 - 3, same with iteration 2, iteration 3 has Groups 1 - 2, etc.).  (B) The inner dictionary contains the current grouping at that particular iteration, and the weights for the previous groups merging that represent the current group (e.g. <code>iteration 3</code> has <code>Group 1</code> and <code>Group 2</code> and for <code>iteration 4</code> all of <code>iteration 3's</code> <code>Group 2</code> has gone into <code>iteration 4's</code> <code>Group 2</code> but <code>iteration 3's</code> <code>Group 1</code> has been split up.  The weights always sum to 1. </p>
  
  <p>My code for the connections w/ weights for the plot above:</p>
  
  <pre><code>D_iter_current_previous =    {
          1: {
              "Group 1":{"sample_0":0.5, "sample_1":0.5, "sample_2":0, "sample_3":0, "sample_4":0},
              "Group 2":{"sample_0":0, "sample_1":0, "sample_2":1, "sample_3":0, "sample_4":0},
              "Group 3":{"sample_0":0, "sample_1":0, "sample_2":0, "sample_3":0.5, "sample_4":0.5}
              },
          2: {
              "Group 1":{"Group 1":1, "Group 2":0, "Group 3":0},
              "Group 2":{"Group 1":0, "Group 2":1, "Group 3":0},
              "Group 3":{"Group 1":0, "Group 2":0, "Group 3":1}
              },
          3: {
              "Group 1":{"Group 1":0.25, "Group 2":0, "Group 3":0.75},
              "Group 2":{"Group 1":0.25, "Group 2":0.75, "Group 3":0}
              },
          4: {
              "Group 1":{"Group 1":1, "Group 2":0},
              "Group 2":{"Group 1":0.25, "Group 2":0.75}
              }
          }
  </code></pre>
  
  <p><strong>This is what happened when I made the Graph in <code>networkx</code></strong>:</p>
  
  <pre><code>import networkx
  import matplotlib.pyplot as plt
  
  # Create Directed Graph
  G = nx.DiGraph()
  
  # Iterate through all connections
  for iter_n, D_current_previous in D_iter_current_previous.items():
      for current_group, D_previous_weights in D_current_previous.items():
          for previous_group, weight in D_previous_weights.items():
              if weight &gt; 0:
                  # Define connections using `|__|` as a delimiter for the names
                  previous_node = "%d|__|%s"%(iter_n - 1, previous_group)
                  current_node = "%d|__|%s"%(iter_n, current_group)
                  connection = (previous_node, current_node)
                  G.add_edge(*connection, weight=weight)
  
  # Draw Graph with labels and width thickness
  nx.draw(G, with_labels=True, width=[G[u][v]['weight'] for u,v in G.edges()])
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/aiXnI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aiXnI.png" alt="enter image description here"></a></p>
  
  <p>Note: The only other way, I could think of to do this would be in <code>matplotlib</code> creating a scatter plot with every tick representing a iteration (5 including the initial samples) then connecting the points to each other with different weights.  This would be some pretty messy code especially trying to line up the edges of the markers w/ the connections...However, I'm not sure if this and <code>networkx</code> is the best way to do it or if there is a tool (e.g. <code>bokeh</code> or <code>plotly</code>) that is designed for this type of plotting.</p>