How do you plot items in a list in python?
I need to plot the velocities of some objects(cars). Show Each velocity are being calculated through a routine and written in a file, roughly through this ( I have deleted some lines to simplify):
The result of this is a text file with list of velocities for each car. something like this:
Then I wanted to plot each velocity
This is what I found. The values are taken as a string and put them as yLabel. I got it working through this:
What I learnt is that, the set of velocity lists I built previously were treated as lines of data. I had to convert them to arrays to be able to plot them. However the brackets [] were getting into the way. By converting the line of data to string and removing the brackets through this (i.e. [1:-1]). It is working now, but I'm sure there is a better way of doing this. Any comments? 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¶
Can we plot list in Python?We will use the matplotlib library. It is a popular Python library for data visualization. Using this, we can easily plot a list in a few lines of code.
How do you plot multiple values in Python?Set the figure size and adjust the padding between and around the subplots.. Create random xs and ys data points using numpy.. Zip xs and ys. Iterate them together.. Make a scatter plot with each x and y values.. To display the figure, use show() method.. Does matplotlib work with lists?More plots
bar function, for plotting bar charts. The full list of plotting functions can be found in the the matplotlib.
Can you plot objects in Python?Plotting the data of a Series or DataFrame object can be accomplished by using the matplotlib. pyplot methods and functions. Keep in mind that in order to be flexible, the plot() method accepts a considerable number of arguments that can only be learned by practicing various plotting scenarios.
|