I have a Matplotlib scatter plot with 10,000+ points that I plan to insert as a figure in a LaTeX document for publication.
I would like the plot points to be raster graphics (e.g. PNG) because vector graphics with that many points often causes problems for PDF readers. I would like the ticks and axes labels to be vector graphics so I don't have to worry about resolution issues for the text and lines.
Is there a simple way to get matplotlib to make parts of the plot raster graphics while keeping the axes/ticks vector graphics?
My best guess so far is to do some sort of pre-render to PNG then imshow the resulting image with appropriate axes bounds before saving to PDF.
Add rasterized=True to the call to plt.scatter
See the docs here
You can control the dpi of the rasterized parts of the figure by setting dpi=300 (for example) in the call to plt.figure
Related
I am currently working on a 3D simulation data. I have a 3D surface, for simplicity, lets say, I have a hemispherical surface. So naturally, I have all the (x,y,z) coordinates that make up the surface. Now I also have a fourth array having the values of some variable (say Pressure for example) at all the (x,y,z) locations that make up the hemispherical surface. My aim is to plot the hemispherical surface and the surface should be coloured according to the fourth array (i.e according to the value of Pressure at that surface).
I have tried pyplot.scatter function from matplotlib, where i use pyplot.scatter(x,y,z, c= Pressure_array) but it leaves me with an artefact like the one shown below (image shows a zoomed in portion of the entire plot)
Notice the fringe like circular pattern. This arises because a Cartesian grid is sampled by a spherical surface and the same is plotted by the scatter points. This pattern remains even upon interpolation of the color values
I am looking for an alternative to the scatter plot method where the surface will be smoother and the circular fringes will be absent. I am aware that matplotlib has surface plots, but i am unable to use it because there, the 'z' coordinate sets both, the height of the plot in 3D and essentially the Color of the surface as well.
Any alternative to scatter plot or surface plot, or a way to get the same domne with the surface plot function in matplotlib will be much appreciated.
I'm using Python, and I have some data which can be projected on to points within an equilateral triangle whose side lengths sum to 1.
I'm not sure if there is an easy way to visualise a plot like this from Matplotlib or similar libraries, or if I'm just going to have to use a drawing package from scratch to make it happen. Any pointers gratefully recieved. Thanks!
If all you want to do is plot a few dots on a graph, you can infact use Matfplotlib's scatter plots for this:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html
Using plt.xlim(*min*, *max*) and plt.ylim(*min*, *max*), you can set the limits of the graph manually to fit your values (might not be neccessary though).
You can draw lines on the graph for the triangle shape (if you need that): How to draw a line with matplotlib?
If you don't need a scale, you can even remove that to have a blank canvas: Matplotlib plots: removing axis, legends and white spaces
I have a xarray dataset clip_ds and have visualised the data array using plot. Now, I want to add a country boundary using Basemap's drawcountries(). Apparently, there is something wrong with the extent I am using in basemap (I guess), but both the country border and the data plot won't show up together. I have tried interchanging the position of clip_ds.pr[0].plot() before and after I create basemap, and it gives me two different results as shown below:
Xarray PLOT BEFORE BASEMAP (Note that the colorbar from xarray plot is still there)
Xarray PLOT AFTER BASEMAP (Notice difference from 3, height of the plot shrinks here and the tick labels disappears, probably overlapped by basemap.)
Xarray plot only
Loading seperate shapefile using map.readshapefile also gives same kind of problem. I know there might be a way around this using cartopy, but I like the Basemap functionalities and would like to know if there is any solution to this.
map=Basemap(projection='merc',
resolution='l',
llcrnrlon=clip_ds.lon[0],
llcrnrlat=clip_ds.lat[0],
urcrnrlon=clip_ds.lon[-1],
urcrnrlat=clip_ds.lat[-1])
map.drawcountries()
clip_ds.pr[0].plot()
plt.show()
Please see below image:
I set the figure size of plt equal to something like (4,6) and set axis to off and margin to zero.
Then continue to draw polyline using coordinate array by ax.plot(line[:,1],line[:,0])
after this I don’t use the plt.show()
But convert the plot to numpy array which has correct (4,6) size but surprisingly fill the plot by stretched to only bbox of the draw line
How can i see all the unused space of figure?
Is there any flag that i have to change in somewhere in matplotlib?
Any help appreciated
The plot of matplotlib will define the output based on shape and size of drawing not based on the pre-defined figure size,some kind of back-end and front-end, or simply it is a responsive-layout when you resize the window everything will scale else those are passed through linewidth= and ...
then i changed my workflow and problem solved ;)
I have a set of data, 5 columns: (x,y,z,Temperature, Pressure), is not a regular mesh, is data from well logging. I would like to make a 3d grid in order to make some vertical cut. How can I do it?
Your question isn't very clear about what you want. If it is a graph packages like seaborn and matplotlib can make 3D graphs that can display heatmap information.