Plotting list of datas

Hi all,

I have a question on how to plot from list of datas.

Basically I have 2 lists X and Y, and each term represent some points in the plane (x, y).

And I want to plot directly from those list but I don’t get the right result.

So if I do

for inde in range(X)
    plt.plot(X[inde], Y[inde], 'b')

it is ok. But if If do plt.plot(X, Y, 'b') the result is different, and not the one wanted. but this second solution seems to be the simplest to me.

How to make something in the same spirit works ?

I also tried plt.plot(*X, *Y, 'b') but with no success.

I’m sure matplotlib has a scatter plot somewhere, or some way of turning off the connecting lines. If not, seaborn will.

[Edit] both do. Here you go:

https://matplotlib.org/stable/gallery/shapes_and_collections/scatter.html

https://seaborn.pydata.org/examples/scatterplot_sizes.html

Thanks for your answer !

But in my case, I have a list of arrays, so each subplot is a line, not a single dot.

Hi,

Assuming you are using the same x-axis reference for all of the arrays, you can try something like the following script:

from matplotlib import pyplot as pl

# x-axis reference
x1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Measured data
y_data = [[1, 4, 9, 16, 27, 30, 33, 37, 40, 38],
          [3, 3, 6, 7, 23, 26, 31, 35, 37, 45],
          [2, 7, 12, 18, 25, 30, 32, 40, 43, 41]]

# Distinct data marker - there are others
plot_marker = ['go', 'r', 'b']

for index, data in enumerate(y_data):

    pl.plot(x1, data, plot_marker[index])

pl.xlabel('X-Value')
pl.ylabel('Y-Value')
pl.title('Plot of X vs. Data Arrays')

pl.xlim(0.0, 12.0)
pl.ylim(0.0, 50.)

pl.show()

You can use different markers of course.

Ok.

So I need to pass through a for loop to do it.

No, you don’t have to but it sure makes it a lot easier.

For example, if we were to do it explicitly versus the implied compact method above, we would set it up as per the following script:

from matplotlib import pyplot as pl

# Reference axis
x1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

y1 = [1, 4, 9, 16, 27, 30, 33, 37, 40, 38]
pl.plot(x1, y1, 'go')

y2 = [3, 3, 6, 7, 23, 26, 31, 35, 37, 45]
pl.plot(x1, y2, 'r')

y3 = [2, 7, 12, 18, 25, 30, 32, 40, 43, 41]
pl.plot(x1, y3, 'b')

pl.xlabel('X-Value')
pl.ylabel('Y-Value')
pl.title('Plot of X vs. Data Arrays')

pl.xlim(0.0, 12.0)
pl.ylim(0.0, 50.)

pl.show()

It all comes down to preference.

Yes sure.

But I cannot have plt.plot(X, Y, c) with X, Y and c lists ? This is my question.

Are you referring to a 3D plot?

Something like this:

import matplotlib.pyplot as plt
import numpy as np

ax = plt.figure().add_subplot(projection='3d')

# Prepare arrays x, y, z
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()