Python Tutorial: Plotting with pandas

Learn to plot Apple stock data with pandas and matplotlib. Use pandas Series and DataFrames directly, apply logarithmic scales to compare different magnitudes, customize colors and legends, and save plots with savefig.
Chapters
Data visualization is a primary tool in a working Data Scientist toolbox. Let's see how to do it with Pandas. For convenience, we import Pandas as PD and matplotlib Pyplot as plt. We load the Apple stock data into a dataframe using readcsv. Notice we use the options parsedate equals true and Index call equals date to force a DateTime 64 index. Again, we'll use these indexes a lot with time series shortly. Also observe entries in the volume column are significantly larger in magnitude than other columns. Now we assign closer by indexing Apple close, yielding a series and applying the values method yielding a numpy array. Remember, the command plot can plot numpy arrays or lists, and the command show must be executed to make the plot visible. This is the resulting plot of the stock close prices. Notice the horizontal axis of the plot corresponds to date indices of the array. We can actually plot Pandas series directly. We assign close series from Apple as a series and call Plot with closed series as an argument. The result is a similar plot, but a bit nicer. The plot function automatically uses the series datetime index to label the horizontal axis. An even nicer alternative is to use the Pandas series plot method, I.e. apply close series plot. The result is as before, but with even more formatting and the axis labels and the name on the axis date inferred from the index name. In fact, Pandas dataframes have a plot method just like Pandas series. Calling Apple plot plots all of the columns of the data frame apple on the same axis. Pandas plots each numerical column against the index and uses the column labels in the legend. However, on this scale we can't see all five line plots because one is so much larger than all the others. We can produce a similar plot using plot plot from the matplotlib using the data frame as argument. This implicitly draws all of the numeric columns of Apple against the index. The figure resembles the one plotted using the data frame method plot, but there is no legend and no title on the date axis. Again, the volume column dominates the other five curves and they cannot be seen on the same scale. To remedy that problem, draw the plot again and call yscale log. This matplotlib function sets a logarithmic scale on the vertical axis. The legend still appears automatically, but now we can distinguish volumes on the order of 10 to the 7 from other price values on the order of 10 to the 2. Any matplotlib options can be used to customize a series or dataframe figure, for instance, we can abstract the open and close series and plot them separately, specifying the colors, line styles and legend labels. We zoom the axis in to the year 2001 with the vertical scale from 0 to 100, and we explicitly place a legend. To find out more about matplotlib customization, see our course on data visualization in Python. Notice again the horizontal date ticks are labeled for us cleanly. Finally, having drawn a figure, it's useful to be able to save it for future use. To obtain the preceding plot, we slice four columns and the rows corresponding to 2001 through 2004 inclusive from the Apple dataframe. We'll learn more about time series splicing later. We generate a plot and apply the Save FIG method to preserve the plot. Save figure can infer the file format, for instance png, jpeg, PDF and others from the suffix of the file name. Now it's your turn to make some fancy plots using pandas in the exercises.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







