Lecture Note
University
University of California San DiegoCourse
DSC 207R | Python for Data SciencePages
2
Academic year
2023
anon
Views
11
Basic Plotting in Matplotlib Part 1 We are learning this week how to use Matplotlib in our Jupiter notebooks to plot data. At the end of this article, you should be able to use Matplotlib to build bar charts, line charts,and histograms and identify the basic elements of a Matplotlib figure. Exploring CO2 Emissions for the United States Let's dive into our Jupiter notebook from the last video, where we got a feel for what's held in the World Development Indicators data set. Our goal now is to explore the CO2 emissionsfor the United States. To do that, we will set up two masks using the string method contains.The first mask will contain all rows for which the name contains CO2 emissions, and thesecond is for all rows whose country code is the USA. We will keep the results of that data ina temporary data frame called stage. After creating the data frame, let's check to see what's inside it. We now have the CO2 emissions, per capita, by year for the United States. Creating Bar Charts Let's now use Matplotlib to see how these emissions have changed over time. I only need two lines of code to accomplish this. I'll first grab the years, then the CO2 emissions, thensend those to a bar plot separately. The narrative is not quite flawless, though. A y-axis label is required, which is crucial in this case. This plot doesn't really work very well on its own without knowing what we plotted. Butonce more, if our goal is simply to investigate the data, then that's fine. Creating Line Plots We will employ a line plot to improve the visual appeal of this chart. This time, we'll include a title, x and y axes labels, and more. For the sake of clarity, we will also add an x-axis label.The line plot is what the plt.plot function requests. The terms "x label" and "y label" refer tothe axes' labels inside the plot itself. You can alter things like font size, color, and otheraspects using the various parameters that each of these functions accepts. We will similarlyset up the title. Now, we can plot the graph. However, the y-axis is starting at 15, which could be misleading. So, let's fix that by making a call to the axis and passing the ranges we wantplotted.
Using Histograms Next, let's examine the data using histograms. All of the CO2 emissions per capita numbers in our data set will be shown. Nevertheless, we have also added some code thatwill allow you to investigate numbers that are within one standard deviation. Conclusion Matplotlib is an effective tool for data visualization. You can make a wide range of plots, such as bar charts, line charts, and histograms, with just a few lines of code. Understandingthe basic elements of a Matplotlib figure will help you build instructive and aestheticallypleasing images that will aid in exploring and comprehending your data.
Basic Plotting in Matplotlib Part 1
Please or to post comments