Python Forum
There are problems with my data visualization function. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: There are problems with my data visualization function. (/thread-10424.html)



There are problems with my data visualization function. - Tony - May-20-2018

I am asked to extract data from a file and draw those coordinates on the background picture of a map by using the module marplotlib.
I have two major issues.
1.I am almost there.if I put plt.show respectively under plot function, output diagrams are what I want.like this.
 plt.imshow(mpimg.imread('map.png'),extent = (149.105, 149.130, -35.29, -35.27) )
    plt.show()
    plt.scatter(latitude, longtitude)
    plt.show()
    plt.show()
however, if I put those in comment and used only 1 plt.show function at the end of the whole function, all the scatter becomes only one dot.
plt.imshow(mpimg.imread('map.png'),extent = (149.105, 149.130, -35.29, -35.27) )
    #plt.show()
    plt.scatter(latitude, longtitude)
    #plt.show()
    plt.show()
2.In addition to the first problem, if I add argument 'extent' to my imshow function, the background picture would be gone in the output.
plt.imshow(mpimg.imread('map.png'),extent = (149.105, 149.130, -35.29, -35.27) )

And here is my entire function.
def map_of_sighting(altas_data):
    """this function is used to iterate through the data and extract
    the values of latitude and longtitude from each sighting,then these values
     will be ploted on the map"""

    starting_index = 1
    latitude = []
    longtitude = []
    while starting_index < len(altas_data):
        float_latitude = float(altas_data[starting_index][1])
        latitude.append(float_latitude)
        float_longtitude = float(altas_data[starting_index][2])
        longtitude.append(float_longtitude)
        starting_index = starting_index + 1
    plt.imshow(mpimg.imread('map.png'),extent = (149.105, 149.130, -35.29, -35.27) )
    #plt.show()
    plt.scatter(latitude, longtitude)
    #plt.show()
    plt.show()
I am grateful for any help.


RE: There are problems with my data visualization function. - killerrex - May-20-2018

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:
  • 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.