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

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.

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.

Setting border spacing matplotlib figure [duplicate]

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

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.

Matplotlib Multi-Colored Graph Python [duplicate]

This question already has answers here:
Plotting different colors in matplotlib [duplicate]
(3 answers)
Closed 5 years ago.
I have a pretty short python program to map my RAM usage over time.
while i < 1000000000000:
x.append(i);
y.append(psutil.virtual_memory().used);
plt.plot(x,y)
i+=1;
plt.show()
plt.pause(0.0001)
For some reason, this graph changes color every time a new data point is added.
Does this have anything to do with the plt.ion() I have? It also re-opens every time I close it. Do you guys have any solutions? Thanks in advance!
I figured it out!
The color (my primary problem) I could fix by adding a color="black" to the plt.plot() line. My code looked like this:
plt.plot(x,y,color="black")
I wasn't able to figure out the thing where it doesn't close, but that's okay, I can still close it from the task manager.

save dataframe.hist() to a file [duplicate]

This question already has answers here:
Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig
(6 answers)
Closed 1 year ago.
I am attempting to create a dataframe histogram and save it as a file.
Here is my code:
ax=df.hist('ColumnName')
fig=ax.get_figure()
fig.savefig('pictureName.png', dpi=100, bbox_inches='tight')
The first line works fine; however, the second line returns an error:
AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'.
Because this question shows the get_figure() being applied to series.hist(), I have also tried using ax=df['ColumnName'].hist(), which successfully produced a histogram but led to the same error message when I attempted to implement get_figure().
As recommended in this other question, normally I would skip the get_figure() and the fig.savefig(), opting instead for plt.savefig, but I am making multiple figures. In my experience, plt.savefig() is unreliable in saving multiple figures, instead saving one figure multiple times, even when I use fig.close() after each figure creation and save.
I very much want to solve this problem as neatly as possible, so that I can carry the solution smoothly into other applications, rather than having to use a different duct-tape fix every time I have to make a graph.
Thank you for your help!
Can you try the following code?
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df.hist('ColumnName', ax=ax)
fig.savefig('example.png')

Categories

Resources