Pandas DataFrame.hist() doesn't work - python

i follow the google course about machine learning. i'm on this part : pandas
But on my mac when i want to generate a chart with this command :
california_housing_dataframe.hist('housing_median_age')
it doesn't work. The python icon appear but nothing is displaying on the screen.
i have see some tips with the backend parameter into matplotlibrc but mine is equals to MacOSX and it should work ?
Thanks for help

To elaborate on the comment from T. Kelly:
You need to call plt.show(). This worked for me:
import matplotlib.pyplot as plt
california_housing_dataframe.hist('housing_median_age')
plt.show()

I'm following Google ML Crash Course(I think you are also following it based on the variable name).
I too encountered the same problem.
When I call
california_housing_dataframe.hist('housing_median_age')
It is not showing any histogram. Instead it is showing
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x12bb814e0>]],
dtype=object)
To show the histogram, add this line in your imports:
%matplotlib inline
It should show the histogram.

Related

Jupyter and %matplotlib inline lost axis

I am having a really weird issue with using the %matplotlib inline code in my jupyter notebook for plotting graphs using both pyplot and the pandas plotting function.
The problem is they show up without any axes, and basically just show the graph area without anything aside from data points.
I found adding:
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
reverse it, but I find it odd that should do that every time as the effect disappears as soon as I run %matplotlib inlinecommand.
an example could be
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(A,A)
plt.tight_layout()
plt.xlabel('here')
plt.show()
This would generate the graph below:
Weird enough if I uses the savefig it get plotted with the axis, if I uses the right-click -> new output -> save as figure, I also get the graph with the figures !!
like this:
Can anyone help me understand what is wrong, which global setting did I mess up, and how do I revert it?
(I don't remember messing around with any settings aside from some settings for pandas, but don't think they should have had an impact)
as mentioned running mpl.rcParams.update(mpl.rcParamsDefault) command does bring it back to normal until I run %matplotlib inline` again !!
Any help would be much appreciated.
Okay I am sorry I think I can answer the question myself now.
With the helpfull #Mr. T asking for the imgur link made me realize what was going on. I had starting using the dark jupyter lab theme, and the graph would generate plots with transparent background, ie. the text and lines where there, but I just couldn't see them.
The trick is to change the background color preferably globally, but that will be a task for tomorrow.

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

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.

Python - Seaborn grid shows, but no content (image) is showing

I'm very new to python, and recently started learning seaborn. When I ran the code, there was no track back and the grid was showed up in a new window. But the problem was no image showed for the FaceGrid, the distplot was showning. Not sure what happened, really appreciate if anyone could help me! Thnank you!
import pandas as pd
import seaborn as sns
import matplotlib as plt
train = pd.read_csv("train.csv")
train["Age"] = train["Age"].fillna(train["Age"].median())
#THIS ONE IS NOT SHOWN
sns.FacetGrid(train, col='Survived', row='Pclass', size=2.2, aspect=1.6)
#THIS ONE WAS SHOWED
sns.distplot(train['Age'])
sns.plt.show()
click to see the image
From the seaborn docs it appears that calling sns.FacetGrid initializes the grid. After that you need to map plots onto the grid. Hopefully that helps.

Cannot get plots to show in a jupyter notebook using %matplotlib inline and plt.show()

So I am trying to simply copy a code from the following link:
http://rajeshrinet.github.io/blog/2014/ising-model/
It is the first block of code that produces the 4 subplots. It already has the lines %matplotlib inline and plt.show() which are the normal reasons that plots won't show up. Also, no errors are produced to guide me in what is going wrong. Does anyone see what the problem is? Thank you for your time.

Categories

Resources