Pandas DataFrame DataFrame.plot.hist() function
Python Pandas DataFrame.plot.hist() function plots a DataFrame
single histogram of a column. A histogram represents data in a graphical form. It can create a bar chart of a range. The higher the bar, the more data falls within the range of this bar.
pandas.DataFrame.plot.hist()
grammar
DataFrame.sample(by=None, bins=10, **kwargs)
parameter
by |
It is a string or a sequence. It represents DataFrame the columns in to be grouped. |
bins |
It is an integer. It represents the number of bins. A bin is like a range, for example, 0-5, 6-10, etc. |
**kwargs |
These are additional keyword arguments for customizing the histogram. You can see more information here . |
Return Value
It returns a plotted histogram and AxesSubplot
the data.
Sample code:DataFrame.plot.hist()
DataFrame
Let's start by plotting a histogram using a simple .
import pandas as pd
dataframe = pd.DataFrame({'Value':[100, 200, 300]})
print(dataframe)
Ours DataFrame
looks like,
Value
0 100
1 200
2 300
All the parameters of this function are optional. If we do not pass any parameters while executing this function, then it will produce the following output.
import pandas as pd
from matplotlib import pyplot as plt
dataframe = pd.DataFrame({"Value": [100, 200, 300]})
histogram = dataframe.plot.hist()
print(histogram)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
Example code: DataFrame.plot.hist()
Plotting a complex histogram
Now, we're going to convert our DataFrame
to a complex case.
import pandas as pd
import numpy as np
dataframe = pd.DataFrame(np.random.randint(0, 200, size=(200, 3)), columns=list("ABC"))
print(dataframe)
Our DataFrame
becomes:
A B C
0 15 163 163
1 29 7 54
2 195 40 6
3 183 92 57
4 72 167 40
.. ... ... ...
195 79 35 7
196 122 79 142
197 121 46 124
198 138 141 114
199 148 95 129
[200 rows x 3 columns]
We have created a containing random integers using the NumPy.random.randint() function DataFrame
. Now, we will DataFrame.plot.hist()
plot DataFrame
a histogram of this using the function.
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
dataframe = pd.DataFrame(np.random.randint(0, 200, size=(200, 3)), columns=list("ABC"))
histogram = dataframe.plot.hist()
print(histogram)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
This function draws a histogram, with 10 by default bin
. It shows DataFrame
the frequency distribution of the three columns in . Each column is represented by a specific color.
Example code: DataFrame.plot.hist()
Changing bin
the quantity
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
dataframe = pd.DataFrame(np.random.randint(0, 200, size=(200, 3)), columns=list("ABC"))
histogram = dataframe.plot.hist(bins=2)
print(histogram)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
dataframe = pd.DataFrame(np.random.randint(0, 200, size=(200, 3)), columns=list("ABC"))
histogram = dataframe.plot.hist(bins=50)
print(histogram)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
In the first example code, we bins
changed the number to 2, and in the second example code, it was 50. Notice that bins
the more numbers there are, the easier it is to understand the histogram. The first histogram is blurry because we can't see A
the bars.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:85 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
Pandas DataFrame DataFrame.isin() function
Publish Date:2025/04/30 Views:133 Category:Python
-
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis
Pandas DataFrame DataFrame.groupby() function
Publish Date:2025/04/30 Views:161 Category:Python
-
pandas.DataFrame.groupby() takes a DataFrame as input and divides the DataFrame into groups based on a given criterion. We can use groupby() the method to easily process large datasets. pandas.DataFrame.groupby() grammar DataFrame . groupby
Pandas DataFrame DataFrame.fillna() function
Publish Date:2025/04/30 Views:60 Category:Python
-
The pandas.DataFrame.fillna() function replaces the values DataFrame in NaN with a certain value. pandas.DataFrame.fillna() grammar DataFrame . fillna( value = None , method = None , axis = None , inplace = False , limit = None , down
Pandas DataFrame DataFrame.dropna() function
Publish Date:2025/04/30 Views:181 Category:Python
-
The pandas.DataFrame.dropna() function removes null values (missing values) from a DataFrame by dropping rows or columns that contain null values DataFrame . NaN ( Not a Number ) and NaT ( Not a Time ) represent null values. DataFrame
Pandas DataFrame DataFrame.assign() function
Publish Date:2025/04/30 Views:55 Category:Python
-
Python Pandas DataFrame.assign() function assigns new columns to DataFrame . pandas.DataFrame.assign() grammar DataFrame . assign( ** kwargs) parameter **kwargs Keyword arguments, DataFrame the column names to be assigned to are passed as k
Pandas DataFrame DataFrame.transform() function
Publish Date:2025/04/30 Views:120 Category:Python
-
Python Pandas DataFrame.transform() DataFrame applies a function on and transforms DataFrame . The function to be applied is passed as an argument to the function. The axis length of transform() the transformed should be the same as the ori