Last active
August 29, 2015 13:59
-
-
Save benjaminrose/10988083 to your computer and use it in GitHub Desktop.
Creating a function that takes two vectors used to make a scatter plot and converts them into the three arrays need for a contour plot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def ScatterToContour(x,y, bins=[10,10]): | |
''' | |
x is the x location of the points | |
y is the y location of the points | |
len(x) needs to equal len(y) | |
bins determins the refinement of the histogram and line edges. | |
All contour plot perameters (levels, ect.) are defined outside | |
but bins my have a minor effect on these | |
''' | |
xlist = np.linspace(min(x), max(x), bins[0]) | |
ylist = np.linspace(min(y), max(y), bins[1]) | |
X, Y = np.meshgrid(xlist, ylist) | |
H, xedges, yedges = np.histogram2d(x,y,bins) | |
H = np.rot90(H) #no idea why, but it works with asymetric plots. | |
H = np.flipud(H) | |
return X, Y, H | |
x = np.append( np.random.randn(9000), np.random.normal(4,2,1000)) #make the plots asymetirc aobut the center. | |
x = np.append( x, np.random.normal(14,1,5000)) #add an extra area so it has two peaks | |
# x = np.random.randn(10100) #more uniform | |
y = np.random.normal(0, 2, 15000) | |
plt.figure(1) | |
plt.scatter(x,y) | |
plt.savefig('scatter.pdf') | |
plt.show() | |
plt.figure(2) | |
xplot, yplot, z = ScatterToContour(x,y) | |
plt.pcolormesh(xplot,yplot,z) | |
plt.contour(xplot,yplot,z) | |
plt.savefig('contour.pdf') | |
plt.show() | |
############################## | |
# Created by Ben Rose | |
# on 2014-04-17 | |
# brose3@nd.edu | |
# shared under the MIT license | |
############################## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment