Line graphs are helpful for visualizing how a variable changes over time.
We can create a simple line graph using plt.plot()
and display it using plt.show()
plt.plot(data1.x_values, data1.y_values)
plt.show()
To create multiple lines, we add the second plt.plot()
before plt.show()
plt.plot(data1.x_values, data1.y_values)
plt.plot(data2.x_values, data2.y_values)
plt.show()
We we share graph, we must add lables
plt.xlable(x_values)
plt.ylable(y_values)
plt.title(value)
Note:
plt.show()
For example
plt.xlable("Months")
plt.ylable("USD")
plt.title("Sales")
plt.show()
If we have multiple lines in the same graph, we can label them by using the command plt.legend()
lable
in each plt.plot()
plt.legend()
For example
plt.plot(vietnam.months, vietnam.sales, lable="Vietnam")
plt.plot(thailand.months, thailand.sales, lable="Thailand")
plt.plot(laos.months, laos.sales, lable="Laos")
plt.legend()
If we only want to add quick text, we can use plt.text()
.
This function takes 3 arguments
plt.text(xcoord, ycoord, "Text Message")
For example, if we want to note at the coordinate (3,4). We can use
plt.text(3, 4, "Sales increase")
plt.title("Sales", fontsize=25)
plt.legend(color = "blue")
Check Web colors on Wikipedia.
We can change line color using keyword color
plt.plot(x, y1, color="tomato")
plt.plot(x, y2, color="organge")
We can change line width using keyword linewidth
plt.plot(x, y1, linewidth=1)
plt.plot(x, y2, linewidth=2)
We can change line width using keyword linestyle
For example, we can change linestyle to dotted (':'), dashed('–'), or no line ('')
plt.plot(x, y1, linestyle='-')
plt.plot(x, y2, linestyle='--')
plt.plot(x, y3, linestyle='-.')
plt.plot(x, y4, linestyle=':')
We can add markers width using keyword marker
For example, we can change the marker to circle (‘o’), diamond(’d’), star ('*'), or square (’s’).
plt.plot(x, y1, marker='x')
plt.plot(x, y2, marker='s')
plt.plot(x, y3, marker='o')
plt.plot(x, y4, marker='d')
plt.plot(x, y5, marker='*')
plt.plot(x, y6, marker='h')
Changing the plotting style is a fast way to change the entire look of your plot without having to update individual colors or line styles. Some popular styles include:
View all styles by typing print(plt.style.available) in the console.
plt.style.use('fivethirtyeight')
plt.style.use('ggplot')
plt.style.use('seaborn')
plt.style.use('default')
To zoom, we can use plt.axis()
with a list of input containing:
For example, to display a plot from x=0 to x=2 and from y=3 to y=5, we write
plt.axis([0, 2, 3, 5])
We can create subplots using .subplot()
The command plt.subplot() needs three arguments:
# Data sets
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
# First Subplot
plt.subplot(1, 2, 1) # a figure that has 1 row with 2 columns
plt.plot(x, y, color='blue')
plt.title('First Subplot')
# Second Subplot
plt.subplot(1, 2, 2)
plt.plot(x, y, color='red')
plt.title('Second Subplot')
# Display both subplots
plt.show()
We can customize the spacing between subplots using plt.subplots_adjust()
.subplots_adjust()
has some keyword arguments that can move your plots within the figure:
For example, to adjust both the top and the hspace:
plt.subplots_adjust(top=0.98, hspace=0.3)
Example 1
Create a figure that has two rows of subplots
Increase the spacing between horizontal subplots to 0.3 and the bottom margin to 0.25.
from matplotlib import pyplot as plt
x = range(7)
straight_line = [0, 1, 2, 3, 4, 5, 6]
parabola = [0, 1, 4, 9, 16, 25, 36]
cubic = [0, 1, 8, 27, 64, 125, 216]
# Subplot 1
plt.subplot(2, 1, 1)
plt.plot(x, straight_line)
# Subplot 2
plt.subplot(2, 2, 3)
plt.plot(x, parabola)
# Subplot 3
plt.subplot(2, 2, 4)
plt.plot(x, cubic)
plt.subplots_adjust(wspace=0.3, bottom=0.25)
plt.show()
Example 2
from matplotlib import pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
visits_per_month = [9695, 7909, 10831, 12942, 12495, 16794, 14161, 12762, 12777, 12439, 10309, 8724]
# numbers of key limes sold each month
key_limes_per_month = [92.0, 109.0, 124.0, 70.0, 101.0, 79.0, 106.0, 101.0, 103.0, 90.0, 102.0, 106.0]
# Create a figure of width 12 and height 8
plt.figure(figsize=(12,8))
# Create a left subplot
ax1 = plt.subplot(1,2,1)
x_values = range(len(months))
plt.plot(x_values, visits_per_month, marker='o')
plt.xlabel("month")
plt.ylabel("visitors")
ax1.set_xticks(x_values)
ax1.set_xticklabels(months)
plt.title("Visitors per month")
# Create a right subplot
ax2 = plt.subplot(1,2,2)
plt.plot(x_values, key_limes_per_month, marker='x', color="orange")
plt.xlabel("month")
plt.ylabel("limes sold")
ax2.set_xticks(x_values)
ax2.set_xticklabels(months)
plt.title("Limes sold per month")
plt.show()
# Save figure
plt.savefig("visit_sublime.png")
We can change the x-tick and y-tick marks with ax.set_xticks()
and ax.set_yticks()
.
These functions accept an array of values representing tick mark positions.