0

I'm currently working with matplotlib in order to create a module of a specific vector field using matplotlib.pyplot.streamplot

after the creation and coloring of the lines in the streamplot, i'm trying to color the whitespace around the divergence points around the plot, im trying to achieve a gradient of color that is dependent on the distance of the white pixels around it.

The streamplot in question is built according to:

xs=np.linspace(-10,10,2000)
ys=np.linspace(-10,10,2000)

Therefore, if the divergence is located (for demonstration purposes) at (0,0) it will be located exactly in the middle of the plot.

Now, the only method i can think of for coloring according to distance from it, is kind of clunky since it requires me to:

  1. add a matplotlib.patches.Rectangle on top of the divergence point in a specific color that is not in the image yet.

  2. convert the plot, with the streamlines and rectangles (one rectangle for each divergence point in streamplot) to a np.array

  3. find the new coordinates of the colors of the rectangles (they represent the location of the divergence point in the new np.array created from streamplot).

  4. calculate the pixels like i want from the colored pixels.

This whole method feels way to clunky and over-complicating, and obviously slower than i could do. im sure theres a way to convert the coordinates from the matplotlib plot to the ones in np.array somehow or perhaps handle the coloring in matplotlib which will be even easier. sadly i couldn't find a solution that answers this specific need yet.

thanks in advance for any help given!

EDIT

I'm adding an example (not my code, but a representation of what I wish to achieve).

I want to clarify that the solution of adding a patches.circle on top of a circle patch is not my go to, since i'm looking to keep my painting options more dynamic.

colored center of a streamplot

4
  • Could you add an image to clarify what you want to do? Could you add some code to create a similar plot with some test data? How do you locate the divergences? Did you look at Matplotlib: How to increase colormap/linewidth quality in streamplot?? Commented Oct 9, 2021 at 16:30
  • @JohanC added an image, sadly i tried all kinds of ways but scratched them all so i don't currently have lead on this issue, but if it helps, im trying to achieve whats in the image, only i want to color each pixel around accordingly while keeping the black lines intact. Commented Oct 9, 2021 at 16:59
  • Maybe I don't understand your issue, but I'd just plot the divergence, using pcolormesh or contourf, in the background, using a grayscale and possibly some level of transparency, and on top of that the stream plot Commented Oct 10, 2021 at 8:52
  • @gboffi The problem i need to slove is that i need to color the white pixels around my divergence points but in a specific order, according to the distance. Edit: what i need is a way to know the coordinates of the pixel in a numpy array in relation to its position to its location in the plot itself. Commented Oct 10, 2021 at 8:54

1 Answer 1

3

If you can define the color intensity you want as a 2-dimensional function, you can plot that function with plt.imshow() and then put the streamplot on top of it. You just need to transform the coordinates linearly to match the image coordinates.

Here is an example:

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10, 10]

# plot 2d function
grid = np.arange(-1, 1, 0.001) 
x, y = np.meshgrid(grid, grid)
z = 1 - (x ** 2 + y ** 2) ** 0.5
plt.imshow(z, cmap='Blues')

# streamplot example from matplotlib docs (modified)
w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = Y ** 2
V = X ** 2 

# transform according to previous plot
n = len(grid) / 2
scale = n / w 
X = (X + w) * scale
Y = (Y + w) * scale
U = (U + w) * scale
V = (V + w) * scale

plt.xticks(ticks=[0, n, 2*n], 
           labels=[-w, 0, w])
plt.yticks(ticks=[0, n, 2*n], 
           labels=[-w, 0, w])
plt.streamplot(X, Y, U, V);

streamplot

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, it helps alot to understand the limitations of matplotlib in its drawing capabilities, im actually looking for something not bound to a mathematical function but more of a system that defines the coloring itself, i.e coloring in specific patterns and shapes. but i will use this as a base for more calculations.
@YoavTarazy please try to be more clear explaining what you need, because two people already (Arne and me) have misunderstood your requirements, in exactly the same way, and if this were an election we have 67% of votes already… The fact that you know what you want and we also know what you know, like by magic, is named telepathy and probably doesn't even exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.