How to make a histogram in ipython laptop using ggplot2 (for python)

I am trying to make a histogram of a simple list of numbers in python using ipython notebook and ggplot for python . Using pylab is simple enough, but I can't get ggplot to work.

I am using this code (based on an example of a diamond histogram that works for me):

from ggplot import * a = [1, 1, 2, 1, 1, 4, 5, 6] p = ggplot(aes(x='carat'), data=a) p + geom_hist() + ggtitle("Histogram of Diamond Carats") + labs("Carats", "Freq") 

Using ipython and pylab, I can only make a histogram with hist(a) and it will display. How to create a histogram using ggplot?

+8
python ggplot2 ipython ipython-notebook python-ggplot
source share
2 answers

If you just want to make a histogram of the numbers in your "a" vector, there are a few problems.

First, ggplot accepts data in the form of a pandas Dataframe, so you need to create it first.

 import pandas as pd a = [1, 1, 2, 1, 1, 4, 5, 6] df = pd.DataFrame(a, columns=['a']) 

Secondly, the geometry of geom_histogram() not geom_hist() . And finally, it looks like you are dropping the code from one of the example diamond plots. You do not need this, so I deleted it.

 from ggplot import * p = ggplot(aes(x='a'), data=df) p + geom_histogram(binwidth=1) 

enter image description here

+17
source share

You added

%matplotlib inline

how is the first team in your laptop?

+7
source share

All Articles