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

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

Related

Create a subplot and apply it to several figures rather than just one [duplicate]

This question already has answers here:
matplotlib: can I create AxesSubplot objects, then add them to a Figure instance?
(5 answers)
pyplot - copy an axes content and show it in a new figure
(2 answers)
Closed 3 years ago.
When I want to make several subplots in a figure I can do, for example, the following
figA=plt.figure('figA',figsize=(30,25))
(ax2,ax1) = figA.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})
And I can then make the plot using ax2.plot(data2) and ax1.plot(data1)
Then I might want to do another separate figure:
figB=plt.figure('figB',figsize=(30,25))
(ax2B,ax1B) = figB.subplots(2,1, gridspec_kw={'height_ratios':[1,15]})
However, I need the exact same top panel as in the previous figure.
What should I do if I want ax2B to always be identical to ax2, no matter what changes I make to the subplot?
In other words, I would like to define a subplot and apply it to several figures, rather than defining it inside a specific figure.
If for example in the top panel I want a straight line f(x)=x, I will do
import numpy as np
X=np.linspace(0.,10.,10)
ax2.plot(X,X)
ax2B.plot(X,X)
but I do not want to define the exact same plot twice. I want to just define a subplot once and for all and then call it in a new figure when I need it.

Incorrect backend configuration with macosx in (old) matplotlib: plt.ion different from savefig, here overwritten alpha keyword

I am using interactive python with plt.ion() for generating figures (v2.7) and have noticed that the figure looks different from the figure exported by savefig (this is not a DPI issue (cf. matplotlib savefig() plots different from show()) - I think it might be a backend issue, but would appreciate help as I don't understand this properly).
Specifically, I wanted visualise the importance of a series of points by the intensity of their colour, which I thought I could do with the "alpha" keyword in matplotlib.
When I just do this, this works fine,
but when I want to add a line to the figure, the alpha keyword seemed to not work any more, and plt.ion() shows this:
I initially thought that perhaps the following issue on github may be related:
https://github.com/matplotlib/matplotlib/issues/4580
but then I noticed that exporting the figure actually produced the following file (i.e. as desired):
It would be great to understand a bit better what is going on, and how I can avoid such issues in the future. Is plt.ion()/plt.show() not the best way to show figures in interactive python, or is this an issue with the alpha keyword?
The code is here:
import numpy as np
from numpy import random as random
from matplotlib import pyplot as plt
fig2,ax2=plt.subplots(1,1,figsize=(3,3),sharey=True)
for ii in range(1):
ax2.plot(np.linspace(0,200,200), [0.1]*200, c= 'k')
for i in range(200):
test2=random.randint(5)
ydata= random.rand(test2)
test = random.rand(test2)
for j in range(test2):
ax2.plot(i,ydata[j],'o',ms=4, c= 'Darkblue',alpha=test[j],markeredgecolor='None')

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.

plt.show() not showing data instead holding it for next plot (spyder)

I have been using the same setup for quite some time now but suddenly I am no longer allowed to plot more than one graph in a program.
Usually I can plot multiple plots after each other and let the program run through it. It executes the next lines of code after closing the first window. However, recently the first plot is not shown but instead the data is added to the last plot.
I have included a sample code which used to give me two plots but now only one.
import matplotlib.pyplot as plt
import numpy as np
random_num = np.random.randint(0,5,10)
random_num_2 = np.random.randint(0,100,10)
plt.plot(random_num, 'ko')
plt.show()
plt.plot(random_num_2, 'g*')
plt.show()
The first image shows the output from my program. But I would like to have them separated into two plots like Figure 2 and 3 show.
Maybe I should add that I am using Python 3.6 with Spyder 3.2.4. The graphics option is set to display it in Qt5 even though I tried all settings and only 'Inline' shows me the results the way I want it.
Sorry if this is a very simple question. I have tried googling but I only come up with questions about my topic where the way mine works would be the solution not the problem.
#TheresaOtt. I would suggest you create a new figure instance (plt.figure()) for each plot and use only once at the end the plt.show() command.

X-label of graph is not visible [duplicate]

This question already has an answer here:
Show categorical x-axis values when making line plot from pandas Series in matplotlib
(1 answer)
Closed 4 years ago.
I have imported an Excel file and want to plot a 2D graph by using data from that file.
I have used matplotlib to plot graph by using
df.plot("Col1","Col2") but its is not showing X-labels in the graph which should be text(Col1)
What changes should I apply to Code()?
According to pandas docs your code is correct. Perhaps you can try another method of showing the x-label in order to see where the problem comes from. You can try the following
ax = df.plot() # retrieve the matplotlib Axes object
ax.xlabel("Col1") # set the x-label
ax.ylabel("Col2") # set the y-label
See this docpage
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xlabel.html

Categories

Resources