Plot numpy array of [x,3] on the same graph

Viewed 654

How to plot a NumPy array such that each horizontal line output[0],output[1],output[2]....... from the numpy array below corresponds to 3 dots output[0][0], output[0][1], output[0][2] on x-axis value 1, the next points is output[1][0], output[1][1], output[1][2] corresponds to x-axis value 2.

And the similar positions in the output[z][0] are connected with a line such that output[0][0] , output[1][0], output[2][0], output[3][0], .....are all connected with a line.

array([[112.00686535, 122.94385176, 32.6615162 ],
       [122.02063079, 142.95451678, 42.66445891],
       [132.04269772, 112.98105083, 52.6898423 ],
       ...,

The above numpy array example has a shape of [2000,3]

The output may look like

enter image description here

For example, x-axis value 2 represents output[2] = output[2][0], output[2][1], output[2][2]

2 Answers

Perhaps something like this, with Pandas?

import pandas as pd
import numpy as np

xs = np.array([[112.00686535, 122.94385176, 32.6615162 ],
               [122.02063079, 142.95451678, 42.66445891],
               [132.04269772, 112.98105083, 52.6898423 ]])
df = pd.DataFrame(xs, index=np.arange(1, len(xs)+1))
df.plot(xticks=df.index)

enter image description here

I figured out plotly gives a nice, interactive graph.

import plotly.graph_objects as go

# Create random data with numpy
import numpy as np
np.random.seed(1)

N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5

# Create traces
fig = go.Figure()
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
                    mode='lines',
                    name='lines'))

fig.add_trace(go.Scatter(x=random_x, y=random_y1,
                    mode='lines+markers',
                    name='lines+markers'))

fig.add_trace(go.Scatter(x=random_x, y=random_y2,
                    mode='markers', name='markers'))

fig.show()

enter image description here

For an output with shape [x,3], we can pass

y=output[:,0] , y=output[:,1], y=output[:,2] 

to each line individually

i.e.

fig.add_trace(go.Scatter(x=random_x, y=output[:,0],
                    mode='lines',
                    name='lines'))
Related