Drawing 3D points on a 2D plot reading values from a file
Question: I have a file containing sets of points with 3 coordinates, separated by tabs.
Like this (spaces added for readability, not present in the original file):

    
    
    x0 \t y0 \t z0
    x0 \t y1 \t z1
    x1 \t y0 \t z0
    x1 \t y1 \t z1
    x1 \t y2 \t z2
    x2 \t y0 \t z0
    ...
    

I'd like to plot them as separate lines on a single 2D plot, like:

    
    
    line for all points with x=x0, label=x0
    line for all points with x=x1, label=x1
    line for all points with x=x2, label=x2
    

Painting those lines with a different color.

I know numpy has a cool function for reading columns, like this:

    
    
    my_data = np.genfromtxt(input_file, delimiter='\t', skiprows=0)
    Y = my_data[:, 1]
    Z = my_data[:, 2]
    

Is there a similarly quick and clean way of picking column values based on the
value of another column?

If there's not quick function to do that (compose a column based on the x
value next to it) I can parse the file and build the data structure step by
step.

Then I would do something like this, using Matplotlib:

    
    
    ax = plt.axes()
    ax.set_xlabel('Y')
    ax.set_ylabel('Z')
    
    # for each value of X
        # pick Y and Z values
        plt.plot(Y, Z, linestyle='--', marker='o', color='b', label='x_val')
    

But I'm sure there's a more Pythonic way of doing that. Maybe some trick with
list comprehension?

EDIT: here's the full working code (thanks to the people who answered). I just
need a way to make it show without cutting the legend

    
    
    import os
    import numpy as np
    import matplotlib.pyplot as plt
    
    input_file = os.path.normpath('C:/Users/sturaroa/Documents/my_file.tsv')
    
    # read values from file, by column
    my_data = np.genfromtxt(input_file, delimiter='\t', skiprows=0)
    X = my_data[:, 0]  # 1st column
    Y = my_data[:, 1]  # 2nd column
    Z = my_data[:, 2]  # 3rd column
    
    # read the unique values in X and use them as keys in a dictionary of line properties
    d = {val: {'label': 'x {}'.format(val), 'linestyle': '--', 'marker': 'o'} for val in set(X)}
    
    # draw a different line for each of the unique values in X
    for val, kwargs in d.items():
        mask = X == val
        y, z = Y[mask], Z[mask]
        plt.plot(y, z, **kwargs)
    
    # label the axes of the plot
    ax = plt.axes()
    ax.set_xlabel('Y')
    ax.set_ylabel('Z')
    
    # get the labels of all the lines in the graph
    handles, labels = ax.get_legend_handles_labels()
    
    # create a legend growing it from the middle and put it on the right side of the graph
    lgd = ax.legend(handles, labels, loc='center left', bbox_to_anchor=(1.0, 0.5))
    
    # save the figure so that the legend fits inside it
    plt.savefig('my_file.pdf', bbox_extra_artists=(lgd,), bbox_inches='tight')
    
    plt.show()
    


Answer: Assume you have a dictionary of `value:kwargs` pairs, where `value` is the
value that `X` must take to be in that curve and `kwargs` is a dict that holds
arguments to be passed to the plotting function.

The code below will use the `value` to construct a mask which can be used to
index and choose the appropriate points.

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    my_data = np.genfromtxt('data.txt', delimiter=' ', skiprows=0)
    X = my_data[:, 0]
    Y = my_data[:, 1]
    Z = my_data[:, 2]
    
    d = {
        0: {'label': 'x0'},
        1: {'label': 'x1'}
    }
    
    for val, kwargs in d.items():
        mask = X == val
        y, z = Y[mask], Z[mask]
    
        plt.plot(y, z, **kwargs)
    
    plt.legend()
    
    plt.show()
    

![enter image description here](http://i.stack.imgur.com/lquVt.png)

