oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

What is matplotlib? #476

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

Matplotlib is a Python library used to create charts and graphs. The concepts you will learn include:

oldoc63 commented 1 year ago

Basic Line Plot

Line graphs are helpful for visualizing how a variable changes over time. Some possible data that would be displayed with a line graph:

oldoc63 commented 1 year ago

Using matplotlib methods, the following code will create a simple line graph using .plot() and displaying it using .show():

x_values = [0, 1, 2, 3, 4]
y_values = [0, 9, 4, 1, 16]
plt.plot(x_values, y_values)
plt.show()
oldoc63 commented 1 year ago
oldoc63 commented 1 year ago
  1. We are going to make a simple graph representing someone's spending on lunch over the past week. First, define two lists, days and money_spent.
  2. Plot days on x-axis and money_spent on the y-axis using plt.plot().
  3. Show the plot using plt.show().
oldoc63 commented 1 year ago

We can also have multiple line plots displayed on the same set of axes. This can be very useful if we want to compare two datasets with the same scale and axis categories. Matplotlib will automatically place the two lines on the same axes and give them different colors if you call plt.plot() twice.

Let's look at the graph we made in the last exercise to track lunch spending, where days is on the x-axis and spending (money_spent) is on the y-axis and add a friend's spending for comparison:

oldoc63 commented 1 year ago

We then get two lines on the same plot. By default, the first line is always blue, and the second line is always orange.

oldoc63 commented 1 year ago
  1. We have defined lists called time, revenue, and costs. Plot revenue vs time. Plot cost vs time on the same plot. Show the plot using plt.show().