Stormvirux
Stormvirux

Reputation: 909

Limit the frequency shown on histogram

I am using matplotlib to plot a histogram and I have the following code:

plt.hist(data["Main Total"],alpha=.7)
plt.title("Total marks Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

This produces a histogram as follows: enter image description here

but when I add histtype=stepfilled as follows

plt.hist(data["Main Total"],histtype="stepfilled", alpha=.7)

It produces: enter image description here

When using stepfilled why is that the frequency maximum that is shown in the graph a very high value even though there is no data having frequency higher than 20 unlike the first histogram which shows it properly.

Here is the code with which you can recreate the issue

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import statsmodels.api as sm
from numpy.random import randn
import matplotlib as mpl
import seaborn as sns

sns.set_color_palette("deep", desat=.6)
mpl.rc("figure", figsize=(8, 4))

data=pd.read_csv("output11cs.csv")
df3=data[['Total','Total.1','Total.2','Total.3','Total.4','Total.5','Total.6','Total.7']]
data["Main Total"]=df3.sum(axis=1)
data = data.dropna()
data.reset_index(drop=True)
plt.hist(data["Main Total"],alpha=.7)
plt.title("Total marks Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

Here is output11cs.csv file. It has just 90 lines

Upvotes: 0

Views: 672

Answers (1)

mwaskom
mwaskom

Reputation: 48992

As noted in my comment above, I think this will work fine if you upgrade matplotlib to 1.3.x, but in case you're on a system where upgrading is not possible, you can also change the "linewidth" of the histogram to get the same effect:

plt.hist(data["Main Total"], alpha=.7, linewidth=0)
plt.title("Total marks Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency");

enter image description here

Upvotes: 1

Related Questions