May-20-2018, 10:09 AM
In matlab each time you plot it reuses the last axes, removing what was there. If you want to keep the previous plot you need to use the command "hold on" to avoid deleting the axes (and "hold off" when you are done) or use low level commands like "line" to plot.
The matplotlib original design follows this mode, so you need to use "plt.hold(True)" to keep the previous things in the axis. But this behaviour is deprecated since version 2 and now the default is to do not delete the content of the axes.
So either you have a really old version of matplotlib installed or you have a configuration file with a "axes.hold: False"
I do not have your input data to check if this code works, but I think there are 2 ways to fix this:
Just to add a small thing, you can clean up your loop avoiding to iterate "by index" that is quite inefficient in python (and I suspect you are missing the initial point):
The matplotlib original design follows this mode, so you need to use "plt.hold(True)" to keep the previous things in the axis. But this behaviour is deprecated since version 2 and now the default is to do not delete the content of the axes.
So either you have a really old version of matplotlib installed or you have a configuration file with a "axes.hold: False"
I do not have your input data to check if this code works, but I think there are 2 ways to fix this:
- The "Please don't do this, is deprecated" way:
plt.imshow(mpimg.imread('map.png'), extent=(149.105, 149.130, -35.29, -35.27)) plt.hold(True) plt.scatter(latitude, longtitude) plt.hold(False) plt.show()
- The "keep control of your axes" way:
fig = plt.figure() ax = fig.add_subplot(1, 1, 1) img = mpimg.imread('map.png') xlim = (149.105, 149.130) ylim = (-35.29, -35.27) # Show the background ax.imshow(img, extent=xlim + ylim) # Plot the lat-lon ax.scatter(latitude, longtitude) # Keep the axes in the right ranges ax.set_xlim(xlim) ax.set_ylim(ylim) plt.show()
Just to add a small thing, you can clean up your loop avoiding to iterate "by index" that is quite inefficient in python (and I suspect you are missing the initial point):
latitude = [] longtitude = [] for point in altas_data: latitude.append(float(point[1])) longitude.append(float(point[2]))Or depending of the format of altas_data you can even do it as:
import numpy as np data = np.array(altas_data, dtype=float) latitude = data[:, 1] longitude = data[:, 2]Take a look to cartopy to plot information in a map... the initial steps are a little bit difficult, but then you can do really amazing things.