Bob
Bob

Reputation: 217

Location of contour lines

Below is my code and plot:

import matplotlib
import matplotlib.mlab as mlab
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

delta = 0.00025
A=0
x = np.arange(0, 0.10, delta)
y = np.arange(0, 0.1, delta)
X, Y = np.meshgrid(x, y)
Z = A*(X**2+Y**2)+2*X*Y

manual_locations = [(0.1,0.1), (0.2,0.2), (0.3,0.3), 
                    (0.015, 0.015), (0.00255, 0.0025), (0.00005,0.00005)]
line_widths = (1, 1, 1, 1, 1, 1)

plt.figure()
CS = plt.contour(X, Y, Z, 6,                        # add 6 contour lines
                 linewidths=line_widths,            # line widths
                 colors = line_colours)             # line colours

plt.clabel(CS, inline=1,                            # add labels
          fontsize=10,                             # label font size 
          manual=manual_locations)                 # label locations
plt.title('Indifference Map')        # title

plt.show()

enter image description here

It seems my manual_locations does nothing, python picks equally spaced contour lines automatically. While I want to investigate more details around 0. How can I see more curves/contour lines converging to (0,0)? Thanks in advance.

Upvotes: 1

Views: 712

Answers (1)

tom10
tom10

Reputation: 69192

The easiest way to explore parts of your data in more details is with levels. This sets what Z-values to examine, and in your question you phrase this as an (x,y) location to inspect, but it's a bit backwards from how contour works to specify the location points directly.

You could also do inspect the (0,0) region by changing the boundaries of the plot appropriately.

Below, I use log values for levels, but linear values work equally well, are far more common, and are easier to interpret. The log values just easily emphasis the part of the plot you're most interested in.

enter image description here

import matplotlib
import matplotlib.mlab as mlab
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
#%matplotlib inline

delta = 0.00025
A=0
x = np.arange(0, 0.10, delta)
y = np.arange(0, 0.1, delta)
X, Y = np.meshgrid(x, y)
Z = A*(X**2+Y**2)+2*X*Y

manual_locations = [(0.1,0.1), (0.2,0.2), (0.3,0.3), 
                    (0.015, 0.015), (0.00255, 0.0025), (0.00005,0.00005)]
line_widths = (1, 1, 1, 1, 1, 1)

plt.figure()
CS = plt.contour(X, Y, Z, 6,                        # add 6 contour lines
                 linewidths=line_widths, 
                 #levels=np.linspace(0, .003, 20))   
                 levels=np.logspace(-5, -2, 20))

plt.clabel(CS, inline=1,                            # add labels
          fontsize=10,
          fmt="%.5f")
plt.title('Indifference Map')        # title

plt.show()

If you really need the contour at a specific location, you could put the (x,y) values for that location into your equation to calculate the z-value at that location, and then use this value as one of the values in the levels argument.

Upvotes: 3

Related Questions