How to plot two lists in python
Please, help me to plot two lists on the same graph. The lines should be of different colors. Here is the code I tried: Show
asked Jul 17, 2017 at 23:50
Alberto AlvarezAlberto Alvarez 7172 gold badges9 silver badges16 bronze badges 5 You should use I don't know what is your X axis but obviously you should create another array/list to be your X value. Then use . Please refer to the pyplot documentation for more details.
Kye 3,9703 gold badges19 silver badges48 bronze badges answered Jul 18, 2017 at 0:39
It's very easy, Hope this sample code will help.
answered Apr 15, 2019 at 14:59
subratasubrata 1111 silver badge8 bronze badges Stealing Borrowing from another answer, this appears to work:
other answer: python/matplotlib - multicolor line Of course, replace the answered Jul 18, 2017 at 1:34
Introduction¶There are many scientific plotting packages. In this chapter we focus on matplotlib, chosen because it is the de facto plotting library and integrates very well with Python. This is just a short introduction to the Basic Usage – pyplot.plot¶Simple use of >>> from matplotlib import pyplot as plt >>> plt.plot([1,2,3,4]) [ If you run this code in the interactive Python interpreter, you should get a plot like this: Two things to note from this plot:
If you pass two lists to >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4]) Understandably, if you provide two lists their lengths must match: >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4, 5]) ValueError: x and y must have same first dimension To plot multiple curves simply call >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4], [0.1, 0.2, 0.3, 0.4], [1, 4, 9, 16]) Alternaltively, more plots may be added by repeatedly calling >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4]) >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 4, 9, 16]) Adding information to the plot axes is straightforward to do: >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4]) >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 4, 9, 16]) >>> plt.xlabel("Time (s)") >>> plt.ylabel("Scale (Bananas)") Also, adding an legend is rather simple: >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4], label='first plot') >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 4, 9, 16], label='second plot') >>> plt.legend() And adjusting axis ranges can be done by calling >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4]) >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 4, 9, 16]) >>> plt.xlabel("Time (s)") >>> plt.ylabel("Scale (Bananas)") >>> plt.xlim(0, 1) >>> plt.ylim(-5, 20) In addition to x and y data lists, >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4], 'rx') >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 4, 9, 16], 'b-.') >>> plt.xlabel("Time (s)") >>> plt.ylabel("Scale (Bananas)") The style strings, one per x–y pair, specify color and shape: ‘rx’ stands for red crosses, and ‘b-.’ stands for blue dash-point line. Check the documentation of
Finally, More plots¶While Bar charts can be plotted using >>> plt.bar(range(7), [1, 2, 3, 4, 3, 2, 1]) Note, however, that
contrary to One of the optional arguments to >>> plt.bar(numpy.arange(0., 1.4, .2), [1, 2, 3, 4, 3, 2, 1]) Specifying narrower bars gives us a much better result: >>> plt.bar(numpy.arange(0., 1.4, .2), [1, 2, 3, 4, 3, 2, 1], width=0.2) Sometimes you will want to compare a function to your measured data; for example when you just fitted a function. Of course this is possible with matplotlib. Let’s say we fitted an quadratic function to the first 10 prime numbers, and want to check how good our fit matches our data.
We made the scatter plot red by passing it the keyword argument Interactivity and saving to file¶If you tried out the previous examples using a Python/IPython console you probably got for each plot an interactive window. Through the four rightmost buttons in this window you can do a number of actions:
The three leftmost buttons will allow you to navigate between different plot views, after zooming/panning. As explained above, saving to file can be easily done from the interactive plot window. However, the need might arise to have your script write a plot directly as an image, and not bring up any interactive window. This is easily done by calling >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 2, 3, 4], 'rx') >>> plt.plot([0.1, 0.2, 0.3, 0.4], [1, 4, 9, 16], 'b-.') >>> plt.xlabel("Time (s)") >>> plt.ylabel("Scale (Bananas)") >>> plt.savefig('the_best_plot.pdf') Multiple figures¶With this groundwork out of the way, we can move on to some more advanced matplotlib use. It is also possible to use it in an object-oriented manner, which allows for more separation between several plots and figures. Let’s say we have two sets of data we want to plot next to eachother, rather than in the same figure. Matplotlib has several
layers of organisation: first, there’s an
This example also neatly highlights one of Matplotlib’s shortcomings: the API is highly inconsistent. Where we could do Now, we want to make multiple plots next to each other. We do that by calling
The Exercises¶
How do you make a scatter plot with two lists in Python?Scatter plots of (x,y) point pairs are created with Matplotlib's ax. scatter() method. The required positional arguments supplied to ax. scatter() are two lists or arrays.
...
Scatter Plots.. How do you plot multiple data in Python?You can plot multiple lines from the data provided by a Dataframe in python using matplotlib. You can do it by specifying different columns of the dataframe as the x and y-axis parameters in the matplotlib. pyplot. plot() function.
How do you plot two arrays in the same plot in Python?“how to plot multiple arrays in python” Code Answer. Call plt. plot() as many times as needed to add additional lines to plot.. import matplotlib. pylot as plt.. x_coordinates = [1, 2, 3]. y1_coordinates = [1, 2, 3]. y2_coordinates = [3, 4, 5]. plt. plot(x_coordinates, y1_coordinates) # plot first line.. How do you plot a list in Python?Following steps were followed:. Define the x-axis and corresponding y-axis values as lists.. Plot them on canvas using . plot() function.. Give a name to x-axis and y-axis using . xlabel() and . ylabel() functions.. Give a title to your plot using . title() function.. Finally, to view your plot, we use . show() function.. |