Can we plot list in python?
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. Show 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 plot a list of points in Python?Practical Data Science using Python. Set the figure size and adjust the padding between and around the subplots.. Create lists of x and y data points.. Plot x and y data points with red color and starred marker.. Set some axis properties.. Iterate x and y to show the coordinates on the plot.. How do you plot two lists in Python?MatPlotLib with Python
Set the figure size and adjust the padding between and around the subplots. Create y1, x1, y2 and x2 data points using numpy with different array lengths. Plot x1, y1 and x2, y2 data points using plot() method. To display the figure, use show() method.
Can you plot arrays in Python?Plot 2-D Arrays in Python
To plot a 2-dimensional array, refer to the following code. The variable y holds the 2-D array. We iterate over each array of the 2-D array, plot it with some random color and a unique label. Once the plotting is done, we reposition the legend box and show the plot.
How do you plot multiple data in Python?Use matplotlib.. x1 = [1, 2, 3] Data for the first line.. y1 = [4, 5, 6]. x2 = [1, 3, 5] Data for the second line.. y2 = [6, 5, 4]. plt. legend(["Dataset 1", "Dataset 2"]) Create a legend for the graph.. |