matplotlib plot to fill figure only with data points, no borders, labels, axes,

2024/10/15 23:27:28

I am after an extreme form of matplotlib's tight layout. I would like the data points to fill the figure from edge to edge without leaving any borders and without titles, axes, ticks, labels or any other decorations. I want to do something like what figimage does, but for plots instead of raw images.

How do I do that in matplotlib?

Answer

While a solution may be found taking the bits and pieces from an answer to this question it may not be obvious at first sight.

The main idea to have no padding around the axes, is to make the axes the same size as the figure.

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])

Alternatively, one can set the outer spacings to 0.

fig, ax = plt.subplots()
plt.subplots_adjust(left=0, right=1, bottom=0, top=1)

Then, in order to remove the axis decorations one can use

ax.set_axis_off()

or

ax.axis("off")

This may now still leave some space between the plotted line and the edge. This can be removed by setting the limits appropriately using ax.set_xlim() and ax.set_ylim(). Or, by using ax.margins(0)

A complete example may thus look like

import matplotlib.pyplot as pltfig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.axis("off")ax.plot([2,3,1])
ax.margins(0)plt.show()

enter image description here

https://en.xdnf.cn/q/117776.html

Related Q&A

Chromedriver: FileNotFoundError: [WinError 2] The system cannot find the file specified Error

Have looked for an answer, but couldnt find anything. It seems insistent on saying it cant find the file specified and then checks PATH, but cant see it even then :/ Ive put the directory in PATH: http…

Python - mutable default arguments to functions

I was going through https://docs.python.org/3.5/tutorial/controlflow.html#default-argument-values. I modified the example there a little bit as below:x = [4,5] def f(a, L=x):L.append(a)return Lx = [8,9…

Python: remove duplicate items from a list while iterating

I have a list named results, I want to get rid of the duplicate items in the list, the expected result and my results does not match, I checked my code but cannot find what is the problem, what happene…

Python - SciPy Kernal Estimation Example - Density 1

Im currently working through this SciPy example on Kernal Estimation. In particular, the one labelled "Univariate estimation". As opposed to creating random data, I am using asset returns. …

PyQt QFileDialog custom proxy filter not working

This working code brings up a QFileDialog prompting the user to select a .csv file:def load(self,fileName=None):if not fileName:fileName=fileDialog.getOpenFileName(caption="Load Existing Radio Log…

If I have Pandas installed correctly, why wont my import statement recognize it?

Im working on a project to play around with a csv file, however, I cant get pandas to work. Everything I have researched so far has just told me to make sure that pandas is installed. Using pip I have …

Python Issues with a Class

I am having issues with my below class. I keep getting the below traceback, butI am not sure were I am going wrong. I am expecting to see a dictionary with photo tags. Any help would be great. Tracebac…

Dynamically populate drop down menu with selection from previous drop down menu

I have a cgi script written in Python. There are two drop down menus and then a submit button. Id like to be able to make a selection from the first menu, and based off that choice, have the second dro…

Web Scrape page with multiple sections

Pretty new to python... and Im trying to my hands at my first project.Been able to replicate few simple demo... but i think there are few extra complexities with what Im trying to do.Im trying to scrap…

Python recv Loop

I am try to display data that is sent over a socket via an iteration from a loop. The way I have it at the moment isnt working on the Admin client. What should I do to fix my loop? Thank youAdmin t…