matplotlib Line plots


Example

Simple line plot

Line plot

import matplotlib.pyplot as plt

# Data
x = [14,23,23,25,34,43,55,56,63,64,65,67,76,82,85,87,87,95]
y = [34,45,34,23,43,76,26,18,24,74,23,56,23,23,34,56,32,23]

# Create the plot
plt.plot(x, y, 'r-')
# r- is a style code meaning red solid line

# Show the plot
plt.show()

Note that in general y is not a function of x and also that the values in x do not need to be sorted. Here's how a line plot with unsorted x-values looks like:

# shuffle the elements in x
np.random.shuffle(x)
plt.plot(x, y, 'r-')
plt.show()

enter image description here

Data plot

This is similar to a scatter plot, but uses the plot() function instead. The only difference in the code here is the style argument.

plt.plot(x, y, 'b^')
# Create blue up-facing triangles

Data plot

Data and line

The style argument can take symbols for both markers and line style:

plt.plot(x, y, 'go--')
# green circles and dashed line

Markers and line