Plot gradient vector in ContourPlot

In summary, the process of plotting a gradient vector in a ContourPlot involves calculating the gradient of a given function, which indicates the direction of the steepest ascent. This vector can be represented visually on a contour plot by overlaying arrows that illustrate both the direction and magnitude of the gradient at various points. This technique enhances the understanding of the function's behavior, highlighting areas of rapid change and providing insights into optimization and analysis in multivariable calculus.
  • #1
Lambda96
229
75
TL;DR Summary
Plot gradient vector in ContourPlot
Hi,

I have made the following ContourPlot in mathematica and now I wanted to ##\vec{r}_1= \left(\begin{array}{c} -1 \\ 1 \end{array}\right)##, ##\vec{r}_2= \left(\begin{array}{c} 0 \\ \sqrt{2} \end{array}\right)## and ##\vec{r}_3= \left(\begin{array}{c} 1 \\ 1 \end{array}\right)## insert the gradient into the ContourPlot.

Bildschirmfoto 2023-12-11 um 15.33.19.png


Is this possible and if so, how?
 
Physics news on Phys.org
  • #3
Thank you Orodruin for your help 👍

Since I want to have the vectors at certain points in the ContourPlot, I used ListVectorPlot[{{x1,y1},{vx1,vy1}},...}].

Unfortunately, Mathematica then plots a vector field

Bildschirmfoto 2023-12-12 um 12.35.57.png
 
  • #4
Check out the VectorPoints option
 

FAQ: Plot gradient vector in ContourPlot

How do I plot a gradient vector field on a ContourPlot?

To plot a gradient vector field on a ContourPlot, you can use the `quiver` function in combination with `contour`. First, calculate the gradient of your function, then use `quiver` to plot the vectors and `contour` to plot the contours. For example, in Python with matplotlib, you might use:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(-5, 5, 100)y = np.linspace(-5, 5, 100)X, Y = np.meshgrid(x, y)Z = X**2 + Y**2 # Example function# Calculate gradientsU, V = np.gradient(Z)# Plot contour and gradientplt.contour(X, Y, Z)plt.quiver(X, Y, U, V)plt.show()

How can I adjust the density of the gradient vectors in the plot?

You can adjust the density of the gradient vectors by changing the step size in your meshgrid or by using the `quiver` function's `scale` parameter. For instance, to reduce the density, you can create a coarser grid:

x = np.linspace(-5, 5, 20) # Fewer pointsy = np.linspace(-5, 5, 20)X, Y = np.meshgrid(x, y)Z = X**2 + Y**2U, V = np.gradient(Z)plt.contour(X, Y, Z)plt.quiver(X, Y, U, V)plt.show()

How can I normalize the gradient vectors for better visualization?

To normalize the gradient vectors, you can divide each vector by its magnitude. This ensures that all vectors have the same length, making the direction of the gradient more apparent:

magnitude = np.sqrt(U**2 + V**2)U_normalized = U / magnitudeV_normalized = V / magnitudeplt.contour(X, Y, Z)plt.quiver(X, Y, U_normalized, V_normalized)plt.show()

Can I color the gradient vectors based on their magnitude?

Yes, you can color the gradient vectors based on their magnitude by passing a color array to the `quiver` function using the `C` parameter. Here's how you can do it:

magnitude = np.sqrt(U**2 + V**2)plt.contour(X, Y, Z)plt.quiver(X, Y, U, V, magnitude)plt.colorbar

Similar threads

Replies
5
Views
398
Replies
19
Views
2K
Replies
5
Views
2K
Replies
2
Views
854
Replies
5
Views
1K
Replies
3
Views
1K
Back
Top