mshumko / asilib

An open source package providing data access and analysis tools for the world's all-sky imager (ASI) data.
https://aurora-asi-lib.readthedocs.io/
GNU General Public License v3.0
10 stars 4 forks source link

Scatterplotting on axes after calling from animate_map_gen() #19

Closed ksd3 closed 5 months ago

ksd3 commented 5 months ago
            >>> from datetime import datetime
            >>> import matplotlib.pyplot as plt
            >>> import asilib
            >>> import asilib.asi
            >>>
            >>> location = 'FSMI'
            >>> time_range = (datetime(2015, 3, 26, 6, 7), datetime(2015, 3, 26, 6, 12))
            >>> asi = asilib.asi.themis(location, time_range=time_range)
            >>> ax = asilib.map.create_map(lon_bounds=(-120, -100), lat_bounds=(55, 65))
            >>> plt.tight_layout()
            >>>
            >>> gen = asi.animate_map_gen(overwrite=True, ax=ax)
            >>>
            >>> for image_time, image, ax, im in gen:
            >>>     # Add your code that modifies each image here...
            >>>     # To demonstrate, lets annotate each frame with the timestamp.
            >>>     # We will need to delete the prior text object, otherwise the current one
            >>>     # will overplot on the prior one---clean up after yourself!
            >>>     if 'text_obj' in locals():
            >>>             ax.texts.remove(text_obj)
            >>>     text_obj = ax.text(0, 0.9, f'THEMIS-{location} at {image_time:%F %T}',
            >>>             transform=ax.transAxes, color='white', fontsize=15)
            >>>
            >>> print(f'Animation saved in {asilib.config["ASI_DATA_DIR"] / "animations" / asi.animation_name}')

Calling animate_map_gen() returns 4 objects: a datetime.datetime of the current image, an ndarray corresponding to the image pixels, an plt.Axes object corresponding to the axis where the image is plotted, and a plt.Imshow object to add colorbars and so on.

Calling an extra scatterplot, like ax.scatterplot(x,y,c=z) where x and y are array-like objects of longitudes and latitudes and z is a quantity I want to plot, results in a singular spot being plotted on the image, even when x,y,z have more than one element. Why is this happening and is there a workaround?

To reproduce this issue, you can run the following code:

from datetime import datetime
import matplotlib.pyplot as plt
import asilib
import asilib.asi

# Sample Data
latitudes = [60, 61, 62]  
longitudes = [-110, -111, -112]  
values = [10, 20, 30] 

# Configuration for the map
location = 'FSMI'
time_range = (datetime(2015, 3, 26, 6, 7), datetime(2015, 3, 26, 6, 12))
asi = asilib.asi.themis(location, time_range=time_range)
ax = asilib.map.create_map(lon_bounds=(-120, -100), lat_bounds=(55, 65))
plt.tight_layout()

# Generate the animation frames
gen = asi.animate_map_gen(overwrite=True, ax=ax)

text_obj = None  # Initialize text_obj outside the loop
for image_time, image, ax, im in gen:
    # Add scatter plot on each frame
    scatter_plot = ax.scatter(longitudes, latitudes, c=values, cmap='viridis', s=100) 

    # Example of adding time-stamped annotations
    if text_obj:
        text_obj.remove()  # Remove the previous text object from the axes
    text_obj = ax.text(0, 0.9, f'THEMIS-{location} at {image_time:%F %T}',
                       transform=ax.transAxes, color='white', fontsize=15)

# Save the final animation
print(f'Animation saved in {asilib.config["ASI_DATA_DIR"] / "animations" / asi.animation_name}')

and the result is Screenshot 2024-04-12 at 5 29 04 PM

ksd3 commented 5 months ago

Nevermind, figured out that I was missing a cartopy transform