Quiver 2D colormap
I am plotting a dipole field which has a singularity at the origin.
Therefore I want to colour code the arrows to indicate the strength of the field.
Right now I manage to produce the arrows I want but the colour goes along the theta-axis and not along the r-axis:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as cm
from matplotlib.colors import Normalize
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
m = np.meshgrid(thetas,radii)
#This is where one should define m such that it results in the color coding I want. Unfortunately, I am not completely sure how the color is decoded in the quiver function.
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), m, pivot='mid')
plt.show()
I would like the arrows to be darker near the origin and brighter as the distance r=sqrt (x^2+y^2) from the origin grows.
matplotlib
|
show 1 more comment
I am plotting a dipole field which has a singularity at the origin.
Therefore I want to colour code the arrows to indicate the strength of the field.
Right now I manage to produce the arrows I want but the colour goes along the theta-axis and not along the r-axis:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as cm
from matplotlib.colors import Normalize
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
m = np.meshgrid(thetas,radii)
#This is where one should define m such that it results in the color coding I want. Unfortunately, I am not completely sure how the color is decoded in the quiver function.
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), m, pivot='mid')
plt.show()
I would like the arrows to be darker near the origin and brighter as the distance r=sqrt (x^2+y^2) from the origin grows.
matplotlib
1
I don't know why usingm
even works here, but I would think that you want to replace it byr
.
– ImportanceOfBeingErnest
Nov 23 '18 at 17:37
haha, wow thank you @ImportanceOfBeingErnest - I didn't know it's as easy as that. Do you know how I can change the color coding to a map like e.g. cm.copper?
– exchange
Nov 23 '18 at 17:56
Addingcmap="copper"
?
– ImportanceOfBeingErnest
Nov 23 '18 at 18:04
Oh...kay thank you! I am sorry, I was confused because I saw in another example that cmap and color were defined together and then given to the plot function in the color argument as "color=colormap(norm(colors))", so I thought this has to be done here as well but it did not work. Thanks again. If you write your comments as answers, I'd accept them.
– exchange
Nov 23 '18 at 18:15
1
You would need something likecolor=colormap(norm(colors))
in case you'd use thecolor
argument. But here you use theC
argument, hence everything works as expected for any ScalarMappable like scatter, imshow etc as well. Maybe you can write this up as answer yourself?
– ImportanceOfBeingErnest
Nov 23 '18 at 18:21
|
show 1 more comment
I am plotting a dipole field which has a singularity at the origin.
Therefore I want to colour code the arrows to indicate the strength of the field.
Right now I manage to produce the arrows I want but the colour goes along the theta-axis and not along the r-axis:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as cm
from matplotlib.colors import Normalize
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
m = np.meshgrid(thetas,radii)
#This is where one should define m such that it results in the color coding I want. Unfortunately, I am not completely sure how the color is decoded in the quiver function.
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), m, pivot='mid')
plt.show()
I would like the arrows to be darker near the origin and brighter as the distance r=sqrt (x^2+y^2) from the origin grows.
matplotlib
I am plotting a dipole field which has a singularity at the origin.
Therefore I want to colour code the arrows to indicate the strength of the field.
Right now I manage to produce the arrows I want but the colour goes along the theta-axis and not along the r-axis:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as cm
from matplotlib.colors import Normalize
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
m = np.meshgrid(thetas,radii)
#This is where one should define m such that it results in the color coding I want. Unfortunately, I am not completely sure how the color is decoded in the quiver function.
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), m, pivot='mid')
plt.show()
I would like the arrows to be darker near the origin and brighter as the distance r=sqrt (x^2+y^2) from the origin grows.
matplotlib
matplotlib
edited Nov 23 '18 at 16:21
kvantour
8,01131129
8,01131129
asked Nov 23 '18 at 16:19
exchange
1062
1062
1
I don't know why usingm
even works here, but I would think that you want to replace it byr
.
– ImportanceOfBeingErnest
Nov 23 '18 at 17:37
haha, wow thank you @ImportanceOfBeingErnest - I didn't know it's as easy as that. Do you know how I can change the color coding to a map like e.g. cm.copper?
– exchange
Nov 23 '18 at 17:56
Addingcmap="copper"
?
– ImportanceOfBeingErnest
Nov 23 '18 at 18:04
Oh...kay thank you! I am sorry, I was confused because I saw in another example that cmap and color were defined together and then given to the plot function in the color argument as "color=colormap(norm(colors))", so I thought this has to be done here as well but it did not work. Thanks again. If you write your comments as answers, I'd accept them.
– exchange
Nov 23 '18 at 18:15
1
You would need something likecolor=colormap(norm(colors))
in case you'd use thecolor
argument. But here you use theC
argument, hence everything works as expected for any ScalarMappable like scatter, imshow etc as well. Maybe you can write this up as answer yourself?
– ImportanceOfBeingErnest
Nov 23 '18 at 18:21
|
show 1 more comment
1
I don't know why usingm
even works here, but I would think that you want to replace it byr
.
– ImportanceOfBeingErnest
Nov 23 '18 at 17:37
haha, wow thank you @ImportanceOfBeingErnest - I didn't know it's as easy as that. Do you know how I can change the color coding to a map like e.g. cm.copper?
– exchange
Nov 23 '18 at 17:56
Addingcmap="copper"
?
– ImportanceOfBeingErnest
Nov 23 '18 at 18:04
Oh...kay thank you! I am sorry, I was confused because I saw in another example that cmap and color were defined together and then given to the plot function in the color argument as "color=colormap(norm(colors))", so I thought this has to be done here as well but it did not work. Thanks again. If you write your comments as answers, I'd accept them.
– exchange
Nov 23 '18 at 18:15
1
You would need something likecolor=colormap(norm(colors))
in case you'd use thecolor
argument. But here you use theC
argument, hence everything works as expected for any ScalarMappable like scatter, imshow etc as well. Maybe you can write this up as answer yourself?
– ImportanceOfBeingErnest
Nov 23 '18 at 18:21
1
1
I don't know why using
m
even works here, but I would think that you want to replace it by r
.– ImportanceOfBeingErnest
Nov 23 '18 at 17:37
I don't know why using
m
even works here, but I would think that you want to replace it by r
.– ImportanceOfBeingErnest
Nov 23 '18 at 17:37
haha, wow thank you @ImportanceOfBeingErnest - I didn't know it's as easy as that. Do you know how I can change the color coding to a map like e.g. cm.copper?
– exchange
Nov 23 '18 at 17:56
haha, wow thank you @ImportanceOfBeingErnest - I didn't know it's as easy as that. Do you know how I can change the color coding to a map like e.g. cm.copper?
– exchange
Nov 23 '18 at 17:56
Adding
cmap="copper"
?– ImportanceOfBeingErnest
Nov 23 '18 at 18:04
Adding
cmap="copper"
?– ImportanceOfBeingErnest
Nov 23 '18 at 18:04
Oh...kay thank you! I am sorry, I was confused because I saw in another example that cmap and color were defined together and then given to the plot function in the color argument as "color=colormap(norm(colors))", so I thought this has to be done here as well but it did not work. Thanks again. If you write your comments as answers, I'd accept them.
– exchange
Nov 23 '18 at 18:15
Oh...kay thank you! I am sorry, I was confused because I saw in another example that cmap and color were defined together and then given to the plot function in the color argument as "color=colormap(norm(colors))", so I thought this has to be done here as well but it did not work. Thanks again. If you write your comments as answers, I'd accept them.
– exchange
Nov 23 '18 at 18:15
1
1
You would need something like
color=colormap(norm(colors))
in case you'd use the color
argument. But here you use the C
argument, hence everything works as expected for any ScalarMappable like scatter, imshow etc as well. Maybe you can write this up as answer yourself?– ImportanceOfBeingErnest
Nov 23 '18 at 18:21
You would need something like
color=colormap(norm(colors))
in case you'd use the color
argument. But here you use the C
argument, hence everything works as expected for any ScalarMappable like scatter, imshow etc as well. Maybe you can write this up as answer yourself?– ImportanceOfBeingErnest
Nov 23 '18 at 18:21
|
show 1 more comment
1 Answer
1
active
oldest
votes
Okay, thanks to the comments of @ImportanceOfBeingEarnest, I can answer the question as follows: The C-argument in the quiver function can just take a function of the plotting coordinates. Hence, it suffices to add "r" in the quiver function as follows:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
#we leave out the 1/r**3 part because it would make our arrows infinitely long near the origin.
#Instead we use a colormap to indicate the strength of the field as follows
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), r, pivot='mid', cmap='YlGnBu_r')
plt.show()
The result looks as follows:
The cmap command makes the color coding appear according to the cmap YlGnBu_r.
More color coding maps are given here:
http://matplotlib.org/examples/color/colormaps_reference.html
and here
http://matplotlib.org/users/colormaps.html.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450003%2fquiver-2d-colormap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Okay, thanks to the comments of @ImportanceOfBeingEarnest, I can answer the question as follows: The C-argument in the quiver function can just take a function of the plotting coordinates. Hence, it suffices to add "r" in the quiver function as follows:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
#we leave out the 1/r**3 part because it would make our arrows infinitely long near the origin.
#Instead we use a colormap to indicate the strength of the field as follows
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), r, pivot='mid', cmap='YlGnBu_r')
plt.show()
The result looks as follows:
The cmap command makes the color coding appear according to the cmap YlGnBu_r.
More color coding maps are given here:
http://matplotlib.org/examples/color/colormaps_reference.html
and here
http://matplotlib.org/users/colormaps.html.
add a comment |
Okay, thanks to the comments of @ImportanceOfBeingEarnest, I can answer the question as follows: The C-argument in the quiver function can just take a function of the plotting coordinates. Hence, it suffices to add "r" in the quiver function as follows:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
#we leave out the 1/r**3 part because it would make our arrows infinitely long near the origin.
#Instead we use a colormap to indicate the strength of the field as follows
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), r, pivot='mid', cmap='YlGnBu_r')
plt.show()
The result looks as follows:
The cmap command makes the color coding appear according to the cmap YlGnBu_r.
More color coding maps are given here:
http://matplotlib.org/examples/color/colormaps_reference.html
and here
http://matplotlib.org/users/colormaps.html.
add a comment |
Okay, thanks to the comments of @ImportanceOfBeingEarnest, I can answer the question as follows: The C-argument in the quiver function can just take a function of the plotting coordinates. Hence, it suffices to add "r" in the quiver function as follows:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
#we leave out the 1/r**3 part because it would make our arrows infinitely long near the origin.
#Instead we use a colormap to indicate the strength of the field as follows
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), r, pivot='mid', cmap='YlGnBu_r')
plt.show()
The result looks as follows:
The cmap command makes the color coding appear according to the cmap YlGnBu_r.
More color coding maps are given here:
http://matplotlib.org/examples/color/colormaps_reference.html
and here
http://matplotlib.org/users/colormaps.html.
Okay, thanks to the comments of @ImportanceOfBeingEarnest, I can answer the question as follows: The C-argument in the quiver function can just take a function of the plotting coordinates. Hence, it suffices to add "r" in the quiver function as follows:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(15,10))
ax = fig.gca(projection='polar')
n=30
m=8
thetas = np.linspace(0, 2*np.pi, n)
radii = np.linspace(0.15, 1, m)
theta, r = np.meshgrid(thetas, radii)
p = .3
Er = p*2*np.cos(theta)#/r**3
Et = p*np.sin(theta)#/r**3
#we leave out the 1/r**3 part because it would make our arrows infinitely long near the origin.
#Instead we use a colormap to indicate the strength of the field as follows
ax.set_title("Dipole field", va='bottom')
ax.quiver(theta, r, Er * np.cos(theta) - Et * np.sin (theta), Er * np.sin(theta) + Et * np.cos(theta), r, pivot='mid', cmap='YlGnBu_r')
plt.show()
The result looks as follows:
The cmap command makes the color coding appear according to the cmap YlGnBu_r.
More color coding maps are given here:
http://matplotlib.org/examples/color/colormaps_reference.html
and here
http://matplotlib.org/users/colormaps.html.
answered Nov 23 '18 at 18:33
exchange
1062
1062
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450003%2fquiver-2d-colormap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I don't know why using
m
even works here, but I would think that you want to replace it byr
.– ImportanceOfBeingErnest
Nov 23 '18 at 17:37
haha, wow thank you @ImportanceOfBeingErnest - I didn't know it's as easy as that. Do you know how I can change the color coding to a map like e.g. cm.copper?
– exchange
Nov 23 '18 at 17:56
Adding
cmap="copper"
?– ImportanceOfBeingErnest
Nov 23 '18 at 18:04
Oh...kay thank you! I am sorry, I was confused because I saw in another example that cmap and color were defined together and then given to the plot function in the color argument as "color=colormap(norm(colors))", so I thought this has to be done here as well but it did not work. Thanks again. If you write your comments as answers, I'd accept them.
– exchange
Nov 23 '18 at 18:15
1
You would need something like
color=colormap(norm(colors))
in case you'd use thecolor
argument. But here you use theC
argument, hence everything works as expected for any ScalarMappable like scatter, imshow etc as well. Maybe you can write this up as answer yourself?– ImportanceOfBeingErnest
Nov 23 '18 at 18:21