Add blur to patch outline in matplotlib? - python

I am plotting a regular patch in matplotlib, defining an area. However, there is uncertainty around the edges of this area. i would like to add 'blur'.
By brute-forcing it I did it one way - sliced the shape along the x-direction and constructed segments of sub-patches, each with their custom facealpha. I could do this by slicing in 2D and then adjusting facealpha with a more convoluted algorithm.
Any simpler ideas?

I'm not aware of any simple way to do this directly. Matplotlib can do things like drop shadows but that won't give you blur. However, matplotlib's Agg renderer has support for custom filters. You can see examples here.
Specifically, you might be able to do something with the GaussianFilter example. Here I think it's being used to generate the blurred drop shadows but you could figure out how to get it to do what you want in your case. Note that what you are doing in these cases is manually defining a process_image() which works directly on image data.
You may also want to look at this question regarding plotting blurred points.

Related

Alternative to plotly scatter_geo for skymap instead of Earth

I'm wanting to create an animated and interactive skymap using plotly, and I like scatter_geo (https://plotly.com/python-api-reference/generated/plotly.express.scatter_geo) but it seems to only allow for the use with the Earth (hence "geo"). I like the functionality in general of scatter_geo, I just need to be able to map it to sky coordinates (because mapping to Earth's lat/long gets annoying with the Earth's rotation) and to be able to have an image of my choosing as the backdrop (I'm wanting to use the Planck image of the MW). Oh, and it needs to be able to do the Mollweide projection.
I'm basically trying to create something like this animation of Fast Radio Bursts (https://vimeo.com/146295242) but with different data and with more interactivity, and I think plotly has the features I need for that - if I can map my sources to the sky, that is!
Does anyone know either how to make scatter_geo do this, or an alternative I can use? I have not yet been able to find one. It doesn't need to be a plotly function, but I would prefer to stick with Python.
Cheers!
...not sure how you create the flashes, but for the rest have a look at EOmaps
It can do all the stuff that .scatter_geo does, it should be able to handle any projection that pyproj can handle and gives you a map in Mollweide projection if you want :-)
... and on top of that you can interact with the map and assign assign any callbacks you like.

Matplotlib circle as arrowhead

I am working on a plotting tool for graphs and want to use use circles as arrowheads for FancyArrowPatch when connecting to nodes, like this:
Also I would like to allow three different styles: "o--o", "o--" and "--o".
Is there is a generic way to use Patch objects as arrowheads?
My alternative approach would be to use small circle patches and manually plot them on top of the edges right before the nodes. But this would include calculating the correct coordinates when dealing with curved edges which I would like to avoid. I have already read this answer, but as far as I understand it, the solution won't work with FancyArrowPatch.
Thanks in advance.

Description of arguments in surface plot function in Python

Hi I am writing a code which uses ax.plot_surface() to plot data on a unit sphere for theta = linspace(0,pi,100) and phi = linspace(0,2*pi,100).
For some reason my image is distorted in the sense that the sphere is ahead of the axis. Does anyone have any idea of why this would be the case?
3D plotting isn't necessarily a good place to start learning how to use plotting libraries; the fundamentals are more often explained in simpler 2d plots. But to get started,
read the 3d tutorial
use the 3d examples for reference
experiment! Produce the same figure with different parameter settings.
The specific parameters you asked about:
linewidth is not relevant for the plot_surface, but does make a big difference in the closely related plot_wireframe. See this example and experiment with the linewidth value. 1 is default.
alpha refers to transparency, of a graphical element. Any value <1 will mean it is possible to see other lines etc, even directly behind. This example uses alpha=0.3 in 3d
antialiased controls whether the rendering is done with anti-aliasing or not. It is more expensive to use, but the result is to reduce visual distortions. See https://stackoverflow.com/a/8750463 which also links this explanation of the method.

Python Matplotlib nonlinear scaling in contour plot

I'm having some trouble visualizing a certain dataset that I have in a contour plot. The issue is that I have a bunch of datapoints (X,Y,Z) for which the Z values range from about 2 to 0, where a lot of the interesting features are located in the 0 to 0.3 range. Using a normal scaling, they are very difficult to see, as illustrated in this image:
Now, I have thought about what else to do. Of course there is logarithmic scaling, but then I first need to think about some sort of mapping, and I am not 100% sure how one would do that. Inspired by this question one could think of a mapping of the type scaling(x) = Log(x/min)/Log(max/min) which worked reasonably well in that question.
Also interesting was the followup discussed here.
where they used some sort of ArcSinh scaling function. That seemed to enlarge the small features quite well, proportionally to the whole.
So my question is two fold in a way I suppose.
How would one scale the data in my contour plot in such a way that the small amplitude features do not get blown away by the outliers?
Would you do it using either of the methods mentioned above, or using something completely different?
I am rather new to python and I am constantly amazed by all the things that are already out there, so I am sure there might be a built in way that is better than anything I mentioned above.
For completeness I uploaded the datafile here (the upload site is robustfiles.com, which a quick google search told me is a trustworthy website to share things like these)
I plotted the above with
data = np.load("D:\SavedData\ThreeQubitRess44SpecHighResNormalFreqs.npy")
fig, (ax1) = plt.subplots(1,figsize=(16,16))
cs = ax1.contourf(X, Y, data, 210, alpha=1,cmap='jet')
fig.colorbar(cs, ax=ax1, shrink=0.9)
ax1.set_title("Freq vs B")
ax1.set_ylabel('Frequency (GHz)'); ax1.set_xlabel('B (arb.)')
Excellent question.
Don't scale the data. You'll be looking for compromises in ranges with many scaling functions.
Instead, use a custom colormap. That way, you won't have to remap your actual data and can easily customize the visualization of the regions you'd like to highlight. Another example can be found in the scipy cookbook and there's quite a few more on the internet.
Another option is to break the plot into 2 separate regions by breaking the axis like so

Best way to create a 2D Contour Map with Python

I am trying to create a 2D Contour Map in Python that looks like this:
In this case, it is a map of chemical concentration for a number of points on the map. But for the sake of simplicity, we could just say it's elevation.
I am given the map, in this case 562 by 404px. I am given a number of X & Y coordinates with the given value at that point. I am not given enough points to smoothly connect the line, and sometimes very few data points to draw from. It's my understanding that Spline plots should be used to smoothly connect the points.
I see that there are a number of libraries out there for Python which assist in creation of the contour maps similar to this.
Matplotlib's Pyplot Contour looks promising.
Numpy also looks to have some potential
But to me, I don't see a clear winner. I'm not really sure where to start, being new to this programming graphical data such as this.
So my question really is, what's the best library to use? Simpler would be preferred. Any insight you could provide that would help get me started the proper way would be fantastic.
Thank you.
In the numpy example that you show, the author is actually using Matplotlib. While there are several plotting libraries, Matplotlib is the most popular for simple 2D plots like this. I'd probably use that unless there is a compelling reason not to.
A general strategy would be to try to find something that looks like what you want in the Matplotlib example gallery and then modify the source code. Another good source of high quality Matplotlib examples that I like is:
http://astroml.github.com/book_figures/
Numpy is actually a N-dimensional array object, not a plotting package.
You don't need every pixel with data. Simply mask your data array. Matplotlib will automatically plot the area that it can and leave other area blank.
I was having this same question. I found that matplotlib has interpolation which can be used to smoothly connect discrete X-Y points.
See the following docs for what helped me through:
Matplotlib's matplotlib.tri.LinearTriInterpolator docs.
Matplotlib's Contour Plot of Irregularly Spaced Data example
How I used the above resources loading x, y, z points in from a CSV to make a topomap end-to-end

Categories