anon_swe
anon_swe

Reputation: 9335

Python: Simple Histogram From DataFrame Gone Awry

I have a Pandas DataFrame that looks something like this:

    pageviews   type
0   48.0    original
1   181.0   licensed
2   50.0    original
3   17.0    original
...

I want to create two histograms, one for the "original" type and one for the "licensed" type.

So I do:

ax = df.hist(by='type')
plt.show()

And I get this:

enter image description here

That's a start, but it's way too zoomed out and I don't know why the height is uniform (it's just a single rectangle). I thought this might have something to do with how zoomed out I am, so I ran:

for plot in ax:
    plot.set_xlim(0, 400000)

And then I get this, which also seems no better:

enter image description here

I just want a normal looking histogram of the pageviews per type :(

Any ideas?

Thanks!

Upvotes: 0

Views: 72

Answers (1)

AaronDT
AaronDT

Reputation: 4050

Try this:

ax = df.groupby("type").sum().plot(kind="bar")
ax2 = ax.twinx()
for r in ax.patches[len(df):]:
    r.set_transform(ax2.transData)
ax2.set_ylim(0, 2);

Upvotes: 1

Related Questions