Plot 2-dimensional NumPy array using specific columns

后端 未结 2 782
你的背包
你的背包 2021-01-03 23:14

I have a 2D numpy array that\'s created like this:

data = np.empty((number_of_elements, 7))

Each row with 7 (or whatever) floats represents

2条回答
  •  渐次进展
    2021-01-03 23:59

    Setting up a basic matplotlib figure is easy:

    import matplotlib.pyplot as plt
    import numpy as np
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    

    Picking off the columns for x, y and color might look something like this:

    N = 100
    data = np.random.random((N, 7))
    x = data[:,0]
    y = data[:,1]
    points = data[:,2:4]
    # color is the length of each vector in `points`
    color = np.sqrt((points**2).sum(axis = 1))/np.sqrt(2.0)
    rgb = plt.get_cmap('jet')(color)
    

    The last line retrieves the jet colormap and maps each of the float values (between 0 and 1) in the array color to a 3-tuple RGB value. There is a list of colormaps to choose from here. There is also a way to define custom colormaps.

    Making a scatter plot is now straight-forward:

    ax.scatter(x, y, color = rgb)
    plt.show()
    # plt.savefig('/tmp/out.png')    # to save the figure to a file
    

    enter image description here

提交回复
热议问题