Setting border spacing matplotlib figure [duplicate] - python

This question already has answers here:
Removing white space around a saved image
(15 answers)
Closed 4 years ago.
I want to have a matplotlib figure without ANY padding. Currently I do this manually via the IPython (Spyder3) interactive window and its configure subplots window. As shown in this figure.
I need to automate this process and decided to have a look at the Tight layout options.
fig.set_tight_layout(True)
fig.tight_layout(rect=[0, 0, 1, 1])
This does not result in the intended result as some margins do still remain. As shown here.
How do I force figures to have no padding at all. I expect the setting to exist as I'm able to change it in the GUI of IPython.

The GUI option you're using can be coded with the plt.subplots_adjust command. Something like plt.subplots_adjust(left=0, right=1, top=1, bottom=0) should achieve what you want

Related

Can anyone explain me how to work with images in matplotlib? [duplicate]

This question already has answers here:
Matplotlib: How to plot images instead of points?
(2 answers)
How to insert a UTF-8 character onto an image
(1 answer)
Placing Custom Images in a Plot Window--as custom data markers or to annotate those markers
(5 answers)
How to make a tkinter canvas background transparent?
(2 answers)
Closed last month.
I wanted to make a chess game in python, but then I saw that you can do a lot of amazing things in matplotlib in terms of designing. I know that this is probably not the best way of doing this but I want to try this. Here is my main question.
How to scale and position images in matplotlib and tell me a bit more about it if possible?
This is what I have at this point:
import matplotlib.pyplot as plt
im = plt.imread('C:\\...\\bb.png')
imgplot = plt.imshow(im)
plt.show()
This just displays the image, I cant realy scale it nor position it.
I also found something like this, it displays the image in the right corner but I can't change anything or it just disappears or nothing happens... In short only misery.
newax = fig.add_axes([0.6,0.8,0.2,0.2], anchor='NE', zorder=1,)
newax.imshow(im)
newax.axis('off') #If I remove this there is a frame
Please help me out.

Can I get a curve like MATLAB in Python? [duplicate]

This question already has answers here:
How to add hovering annotations to a plot
(12 answers)
Closed 3 years ago.
How can I plot a figure in Python like MATLAB with the following features?:
1) I can zoom in and zoom out.
2) I can modify the range for x and y on the figure
3) I can click on the figure to see actual numbers related to each data point
Another question, how can we specify with wath resolution the matplotlib saves the figure?
To the best of my knowledge, MATPLOTLIB does not have that. Anything else?
I would recommend looking at plotly.
Plotly allows you to make graphs that can be
1) zoomed in on
2) change the x and y-axis numbers by dragging on the axes (but cant switch from say, log scale to linear scale as easily -- something like this would require an interaction feature)
3) Plotly allows you to display info on cursor-hover
For an example of a plotly plot in use, see this washington post page.

how to adjust matplotlib chart figure [duplicate]

This question already has answers here:
How to adjust padding with cutoff or overlapping labels
(8 answers)
Closed 4 years ago.
So I'm using yellowbrick in Python, which is basically matplotlib and scikit-learn combined, to visualize some data.
My chart looks like this:
The labels get cut off. What I want to do is to adjust the figure so the labels on the right don't get cut off. I tried
plt.rcParams['figure.figsize'] = (10, 5)
plt.rcParams['font.size'] = 12
but when I rendered the figure, it's still cut off. Even when I save it as a png file it's still cut off. What am I missing here?
tight_layout method should solve your problem.
Generally you can use it with:
fig.tight_layout() # if fig is your figure handle
or
plt.tight_layout() # if stated within the context of your figure
This line of code should be added after the last plotting statement just before rendering the figure.
If this does not work, please post a fully working minimal code example, as described in mcve. Afterwards I'll be able to post a fully working solution for most, if not all, cases.

fig.show() does not bring the figure up [duplicate]

This question already has an answer here:
Matplotlib: re-open a closed figure? [duplicate]
(1 answer)
Closed 5 years ago.
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure()
axe = fig.add_subplot(111)
axe.plot(x,y,'b-')
axe.plot(z,h,'r-')
Now, I can see 2 lines (blue and red) plotted on figure. But I accidentally close that figure window.
when I issue command fig.show(), or fig.draw(), the figure will never show up again. How can I bring up my original figure without executing above commands again?
Thanks in advance.
I ran your code but I'm not getting the output you described. "2 lines (blue and red) plotted on figure"
Closing the figure via gui destroys the gui toolkit, however, the figure object still exists, you can save it with fig.savefig(). I think the best approach would be re-creating the figure.
Hopefully this helps: Matplotlib: how to show a figure that has been closed.

Matplolib, put text in front of graphs [duplicate]

This question already has an answer here:
Getting text to display in front of subplot images
(1 answer)
Closed 5 years ago.
I have created some subplots using matplotlib librairies (pyplot and gridspec). I am trying to put some text in front of the graphs, but sometimes they are located below, in the background so I can't see them.
I don't know if I should used plt.text or annonate or rather use methods of sublplots?
If you change the command window to white you should be able to see the text. If you are using the command %matplotlib inline, together with a black command window, the text will not always be visible.

Categories

Resources