I would avoid naming a list object list
. It confuses the namespace. But try something like
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(0, 10, 0.2)
y = [2.5, 3, 1.5, ... , 7, 9]
ax.plot(x, y)
plt.show()
It creates a list of point on the x-axis, which occur at multiples of 0.2
using np.arange
, at which matplotlib will plot the y values. Numpy is a library for easily creating and manipulating vectors, matrices, and arrays, especially when they are very large.
Edit:
fig.add_subplot(N_row,N_col,plot_number)
is the object oriented approach to plotting with matplotlib. It's useful if you want to add multiple subplots to the same figure. For example,
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
adds two subplots to the same figure fig
. They will be arranged one above the other in two rows. ax2
is the bottom subplot. Check out this relevant post for more info.
To change the actual x ticks and tick labels, use something like
ax.set_xticks(np.arange(0, 10, 0.5))
ax.set_xticklabels(np.arange(0, 10, 0.5))
# This second line is kind of redundant but it's useful if you want
# to format the ticks different than just plain floats.