Superimpose a networkx graph and a scatter plot - python

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 ?

Related

Finding the Plot Coordinates from the Scatter Plot Data

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?

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?

Plot on one fig two independent plots with matplotlib, different scale on x and y

I want two independent plots to be overlayed on one matplotlib figure.
So I plot a network graph from osmnx
import osmnx as ox
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
fig, ax = ox.plot_graph(G)
Then I plot the networkx graph with:
nx.draw(nx.generators.barabasi_albert_graph(10,3), ax = ax)
And I want one to be overlayed on the other nicely, however the coordinates are completely different.
First one is lon, lat and the second is randomly generated from networkx.
How to scale them nicely so that they are visible?
I tried ax.twinx(), but it doesn't work for me.
PS. For networks I can pass ax nx.draw(G, ax = ax) for osmnx not, it is returned from plot only fig, ax = ox.plot(G)
For networks I can pass ax nx.draw(G, ax = ax) for osmnx not, it is returned from plot only fig, ax = ox.plot(G)
Note that in the latest OSMnx you can pass an ax into the plot_graph function. This should be released in the new version next week, or you can use the GitHub master branch in the meantime.
Regarding scaling your non-spatial graph, I do not believe that networkx gives you any ability to plot nodes or edges at specific coordinates. You'd have to do that all manually. But, as to your specific question, you'd want to first normalize your non-spatial graph coordinates and then scale them to the range of your spatial OSMnx graph's coordinates. This matlab example shows how and is easily translated into python.

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.)

How to draw BarPlot or Histogram using Subplot in MatplotLib?

I want to draw Grid of Bar graph/Histogram for my data.My Data contains 1 NUMERIC and 3 CATEGORICAL Column
PAIRGraph is not suitable for my purpose as my purpose as I have only 1 Numeric and 3 Categorical Column
Tried to Refer Documentation https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html
However, I am unable to find exact way to fulfill my requirement.
Using Demo code I am able to draw only LineGraph. However, I am required to draw Bar Graph.
fig, axes = plt.subplots(1, 2, figsize=(10,4))
x = np.linspace(0, 5, 11)
axes[0].plot(x, x**2, x, np.exp(x),x,20*x)
axes[0].set_title("Normal scale")
axes[0].plot
axes[1].plot(x, x**2, x, np.exp(x))
axes[1].set_yscale("log")
axes[1].set_title("Logarithmic scale (y)");
Please feel free to correct my approach or guide me as I have just started learning.
If you specify exactly what you want to use for the bar and hist, I can modify, but generally it is simply changing the plot to the type of chart you need
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(1, 2, figsize=(10,4))
x = np.linspace(0, 5, 11)
axes[0].bar(x,x**2) # bar plot
axes[0].set_title("Normal scale")
axes[0].plot
axes[1].hist(x) # histogram
axes[1].set_yscale("log")
axes[1].set_title("Logarithmic scale (y)");
plt.show()
After going through the API documentation from Matplotlip Subplot Axes, I found ways to draw different graph not just Line graph.
https://matplotlib.org/api/axes_api.html
DEFAULT:-
axes[0].plot by-default draws line graph.
CUSTOM GRAPH:-
axes[0].bar can be used to draw BAR graph in selected Subplot
axes[0].scatter can be used to draw Scatter graph in selected Subplot
axes[0].hist can be used to draw a histogram. in selected Subplot
Like above example more graph can be drawn with below API:-

Categories

Resources