Prevent Matplotlib to stretch plot to only bbox of drawing area - python

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 ;)

Related

Plotting points within a triangle

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

Can matplotlib commands shift the coordinates of a plot (raster image)?

I have some code that eventually produces a contourf plot at a certain location (lat/lon), like below:
The purple in the image represents the matplotlib plot, which is then overlain on a vector world shapefile. Here, it can be seen that the plot is shifted to the left and up of the location on the vector (blue background). The center location on the vector is the red 'X' and the same coordinates on the matplotlib plot is the red '+'. I first thought this shift was coming from some PyQGIS code, but now I think its the matplotlib commands I have in my Python code.
The commands that I use to create the plot and then save the plot to become a .png image are below:
plt.contourf(Xa,Ya,Result)
plt.grid(color='w')
plt.subplots_adjust(left=0,bottom=0,right=1,top=1,wspace=0,hspace=0)
filename="Results/submerged.png"
plt.savefig(filename, dpi=599, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False)
Where Xa and Ya are the grid coordinates and Result is the result that is plotted.
I then take the saved .png file and overlay it on the vector in PyQGIS. I asked a question on stackexchange about the shifted result here, but haven't gotten any responses.
Any suggestions would be helpful!

Vector axes but raster points for Matplotlib scatter plots

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

Margins in 2D image plot after adding scatterplot point in Matplotlib

I am trying to label points on the image, but whenever I do an extra marker on the plots by coordinate values and the margins becomes unnecessarily large. What is the issue here, and is there a way to fix this?
The image is fine. I even plotted it below and everything seems okay when I don't add the plotted point.
imp = plt.imshow(processed[::-1],cmap='gray_r',vmin=1000,vmax=2000)
plt.colorbar()
plt.figure()
imp = plt.imshow(processed[::-1],cmap='gray_r',vmin=1000,vmax=2000)
plt.plot(600,400,'*',color='r')
plt.colorbar()
Large Margin around image generated

"continuous" plotting in Pygame

I do I plot a "continuous", moving plot in Pygame, like the plot in this "NetLogo" simulation? My main difficulty is not the plotting itself (lines between points) but the process of moving the framework of the plot when the curve getting close to the edge of the box.
Thank you.
Ok, so you can make a plot similar to the one in the application in the following way:
create a surface of the size of the graph. Then create a pixel array, so you will be able to modify the graph.
pxarray = pygame.PixelArray (surface)
you can then manipulate the array like any other array:
pxarray[x][y] = 0xFF00FF # this will set pixel at x,y to purple
you can normally then normally blit the surface to the screen.
more on pixel arrays : http://www.pygame.org/docs/ref/pixelarray.html
EDIT: Using pxarray, and transform you will have a shrinking graph if your numbers go out of range. Simply when the point is too big for the plot, you add enough rows, and use transform.scale to scale back to the original resolution.

Categories