Finding the Plot Coordinates from the Scatter Plot Data - python

So I want to annotate a plot of points in an ellipse by embedding a graph on top of each of the points. I have done this manually with the first few points via the inset axes function.
For instance, I plotted the graph with the line via:
ins = ax.inset_axes([0.45,0.015,0.1,0.1])
xxx = numpy.arange(10)
yyy = numpy.arange(10)
ins.plot(xxx, yyy)
I placed all the graphs manually so far, but this is tedious, and if the points change, then my code is invalid. Let's say the green circle in the above plot was plotted using:
ax.scatter(green_x, green_y, marker="o", s=50, edgecolors="green", c="green")
Question
How can I find the ax.inset_axes() coordinates for the green circle in the plot above so that I can overlay my graph automatically?

Related

A smart way of plotting a density map in galactic coordinates with astropy with z axis showing density of sources?

I am using matplotlib.pyplot and astropy to build a plot in galactic coordinates and my goal is to show the density of stars in the sky.
For that, the only data I have is a two-column table with the coordinates of the stars in Right Ascension (RA) and Declination (Dec).
Right now my code is doing the following:
import astropy.coordinates as coord
import matplotlib.pyplot as plt
import astropy.units as u
coordinates = coord.SkyCoord(ra=RA*u.deg, dec=DEC*u.deg)
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection="aitoff")
ax.plot(coordinates.galactic.l.wrap_at('180d').radian,
coordinates.galactic.b.radian, 'k.', alpha=0.01, ms=1)
ax.grid(True)
So for now I am basically using plt.plot to plot all datapoints (which in the case is half-million datapoints) using a very low alpha and symbol size and the plot looks like this:
However, this isn't the plot I want, as the colour scale quickly saturates.
My question is: Is there a way of making a similar plot but properly reflecting the density of datapoint in the z-axis (color)? For example, I want to be able of controling the color table for a given number-density of sources.
I've seen some answers to similar questions are available.
For example, this question (Plotting a heatmap in galactic coordinates) does a similar thing, but for a specific z-axis described by some data.
I am also aware of this question (How can I make a scatter plot colored by density in matplotlib?) and I tried each solution in this post, but they all failed since I am using a subplot which already has a projection.
Any ideas?

Plotting vertical and horizontal lines inside a seaborn heatmap plot

I had some data on which I had to do some image processing to obtain the centers of the 4 'circles' (refer figure). I was able to do that correctly using the Blob detection function of the OpenCV library in Python.
I want to plot the lines perpendicular to the x and y axis to indicate these centers on the seaborn heatmap output. I'm unable to do so. I have the co-ordinates for the lines in a separate list.
But I'm unable to plot even one line to begin with.
plt.figure(figsize = (12,8))
sns.heatmap(df_scaled)
plt.axvline(x = -10.86,color='white',linewidth=2) # For now let's assume value of x as -10.86
plt.tight_layout()
plt.show()]

Matplotlib: plot arbitrary vectors on polar axes

There are previous questions about quiver plots on polar axes in matplotlib, however they concern vector fields. I'm interested in drawing arbitrary vectors on polar axes. If there is a genuine duplicate, please link it.
I'm writing some software which concerns a circular world. I'm plotting an agent's trajectory from the centre of a circular arena to the edge. This is visualised by drawing a vector from the centre of the circle to the edge. I'm trying to use matplotlib's quiver plot to plot vectors on a set of polar axes. Here's a minimum working example:
import matplotlib.pyplot as plt
import numpy as np
if __name__ == '__main__':
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
# Plot origin (agent's start point)
ax.plot(0, 0, color='black', marker='o', markersize=5)
# Plot agent's path
ax.quiver((0, 0), (0, 1), color='black')
# Example of where (0, 1) should be
ax.plot(0, 1, color='black', marker='o', markersize=5)
# Plot configuration
ax.set_rticks([])
ax.set_rmin(0)
ax.set_rmax(1)
ax.set_thetalim(-np.pi, np.pi)
ax.set_xticks(np.linspace(np.pi, -np.pi, 4, endpoint=False))
ax.grid(False)
ax.set_theta_direction(-1)
ax.set_theta_zero_location("N")
plt.show()
If you run the code, you get this plot
The plot shows the origin plotted correctly, an example point at (0, 1) to show where the vector should end, then the vector itself which appears far too short (though the direction is correct). From the docs, I understand that quiver takes cartesian coordinates (x,y) denoting the start point of the vector and (u,v) denoting the vector's direction. In my previous experience with quiver (u,v) essentially denotes where the vector's tip will be, so in this case we'd expect the vector to be drawn from (0,0) to (0,1) which isn't the case and I don't know why.
In short, I want to be able to draw arbitrary vectors on a set of polar axes and quiver isn't working as I expected. Three questions:
Is my code actually sensible given my goal? I want to draw a unit vector from the origin to the edge of the polar plot.
Am I completely misunderstanding how to use quiver?
How can I draw arbitrary vectors on polar axes in matplotlib? I know about arrow and I'm going to give that a try though initial attempts were unsuccessful.
Short of using a standard plot and just defining my own polar system within it I'm completely stumped.
You did not specify u and v in ax.quiver(x,y,u,v). To make sure the arrow is 1 unit long you will need to set the scale und units as well.
ax.quiver(0,0,0,1, color='black', angles="xy", scale_units='xy', scale=1.)

Define plot color by density of overlap with other plots in matplotlib

I'm looking to plot multiple GPS tracks from lat, long points. I build each plot individually and then show them all on the same final figure. I am wondering if I can use the overlap of the individual plots to define their color at that point. Essentially, I want to create a heatmap based in different colors instead of just alpha, so I can still see every track.
Currently, each plot is simply put together with:
plt.plot(lon, lat, color = 'deepskyblue', lw = 0.2, alpha = 0.8)

Superimpose a networkx graph and a scatter plot

I have two figures:
1) networkx graph
2) a normal x-y plot.
1) The variable 'graph' is the Graph created using networkx. 'var' is x-y coordinates of the nodes of graph.
2) Also, I have two plot few points using scatter plot that will super impose on some of the existing points. I tried the following code. coors are the coordinates of x-y plot
fig, ax = plt.subplots()
ax.plot(nx.draw(graph,[(x,y) for x,y in var],node_size=50))
ax.scatter(coors[:,0],coors[:,1])
plt.show()
When I plot each of them separately it works but when I try to plot it together only the networkx comes up. I know they are different because scatter plot is in blue color and networkx graph is in a kinda orange color. Can someone help me out ?

Categories

Resources