Creating plots, charts, and graphs is the very purpose of a plotting package, such as Matplotlib. You can plot all the standard charts you can think of with Matplotlib and Python. These charts can help you show how data changes over time. For example, you might want to plot the temperatures of your home city over several months or years.
By plotting these temperatures, you can see how they change. For example, as a city grows, you might see different average temperatures than when it was smaller. Or maybe you won’t. Either way, you will still take hundreds of temperature readings and make a drawing that helps you understand the data.
Let’s start charting!
Finding a Dataset
Many governments worldwide provide free datasets you can use. You can find more than twenty-eight thousand datasets from the USA on [data.gov](https://catalog.data.gov/dataset/?res_format=CSV).
You can get the datasets in one or more of the following formats:
CSV
RDF
JSON
XML
Python has built-in libraries for CSV, JSON, and XML, so you should be able to find a dataset that you can read easily with Python. You can right-click on the links provided and download the files. If you have trouble downloading from the website, right-click the file and copy the URL. Then you can use Python to download the file.
The example above uses the `requests` package, which is **not** included with Python. You can install it by opening your terminal and running this command:
```
python -m pip install requests
```
Once requests is installed, you can run the code above. Note that the URL used in this example downloads crime statistics as a JSON file over 400 megabytes in size.
You can load the data into Python using Python’s `json` module. Alternatively, if you downloaded a CSV file, you would use the `csv` module instead.
For the purposes of this chapter, if you use a government dataset, use the CSV file for now, as it is a little easier to work with. The above example is useful when you need to download an XML or JSON file, though.
Now let’s use the data you just downloaded to create a line chart!
Creating a Line Chart
A line chart is best for showing trends or changes over time with continuous data. You might use a line chart to show product popularity over time or how temperatures change over the years. You can also use line charts to compare multiple datasets on the same chart.
For this example, you will use the [Electric Vehicle Population Data for the State of Washington](https://catalog.data.gov/dataset/electric-vehicle-population-data) dataset from the Data.gov website. You will be using the CSV version of the file. Download the file and put it in the same folder where you will write your Python code.
Next, open up your favorite Python IDE or text editor and create a new file called line_plot_electric_vehicles.py. Then enter the following code:
Just in case you cannot find the CSV file for this dataset, the top ten electric vehicle models at the time of writing this chapter were the following:
What that means is that you can set the data variable in main() to the following instead:
data = [55187, 37340, 13768, 7845, 7813, 6684, 5893, 5731, 5267, 4952]
But it’s good to parse a CSV file to get your dataset rather than hard-coding data in your code. On the other hand, datasets can disappear from the Internet, so having the data here keeps the code working.
If you don’t want to mess with the CSV file, you can create a different Python file called line_plot_electric_vehicles2.py with the following code:
You are using Matplotlib’s pyplot library to create your charts. In fact, this is the library you will use to create all your plots in this book. To create the line chart, you pass a list of integers to the plot() function.
There are many other functions you can call. In your example, you use title() to set the title of the plot. Then you call show(), which draws the chart and displays it to the user. You will learn about many of the other functions you can use in later chapters.
No matter which version of the code you run, when you run this code, it will show the following chart:
The chart shows a trend line that shows the popularity of electric automobile models. The data isn’t labeled, but if you look at the raw data, you can see that 40% of the models are made by Tesla, and two of the top three are also from Tesla.
Spend some time analyzing your dataset and see if you can find other ways to display the data. You might want to grab another dataset, such as car prices over time or average lifespans over time, since those types of datasets are even better for a line chart.
Wrapping Up
Matplotlib can do all kinds of charts and plots, much more than what is demonstrated here. You can also change colors, do live charts, and much more. Give matplotlib a try and see what you can do!
Hire an AI Team Member
Scale your business with Marblism today
Or hire Viktor to grow your business and answer your users’ questions.






