如何在Python的 Matplotlib 中绘制具有各种变量的直方图?
在本文中,我们将了解如何使用Python在 Matplotlib 中绘制包含各种变量的直方图。
直方图是以分组形式呈现的数据的可视化表示。它是一种以图形方式显示数值数据分布的精确方法。这是一种条形图,其中 X 轴显示 bin 范围,Y 轴表示频率。在Python中,我们使用 plt.hist() 方法绘制直方图。
Syntax: matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \*\*kwargs)
简单直方图
下面的代码是绘制一个简单的直方图,没有额外的修改。导入包,读取 CSV 文件并使用 plt.hist() 方法绘制直方图。
要下载和阅读 CSV 文件,请单击 schoolimprovement2010grants
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('schoolimprovement2010grants.csv')
# creating a histogram
plt.hist(df['Award_Amount'])
plt.show()
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('schoolimprovement2010grants.csv')
# plotting histogram
plt.hist(df['Award_Amount'],bins = 35,
alpha = 0.45, color = 'red')
plt.show()
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('homelessness.csv')
# plotting two histograms on the same axis
plt.hist(df['individuals'], bins=25, alpha=0.45, color='red')
plt.hist(df['family_members'], bins=25, alpha=0.45, color='blue')
plt.title("histogram with multiple \
variables (overlapping histogram)")
plt.legend(['individuals who are homeless',
'family members who are homeless'])
plt.show()
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('medals_by_country_2016.csv')
# plotting three histograms on the same axis
plt.hist(df['Bronze'],bins = 25, alpha = 0.5,
color = 'red')
plt.hist(df['Gold'],bins = 25, alpha = 0.5,
color = 'blue')
plt.hist(df['Silver'],bins = 25, alpha = 0.5,
color = 'yellow')
plt.title("histogram with multiple \
variables (overlapping histogram)")
plt.legend(['Bronze','Gold','Silver'])
plt.show()
输出:
示例 1:具有各种变量的直方图
下面的直方图是使用额外参数(如 bin、alpha 和颜色)绘制的。 alpha 确定透明度,bins 确定 bin 的数量,color 表示直方图的颜色。
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('schoolimprovement2010grants.csv')
# plotting histogram
plt.hist(df['Award_Amount'],bins = 35,
alpha = 0.45, color = 'red')
plt.show()
输出:
示例 2:重叠直方图
在下面的代码中,我们在同一轴上绘制了两个直方图。我们使用 plt.hist() 方法两次,并使用参数、bin、alpha 和 color,就像在前面的示例中一样。
要下载和查看使用的 CSV 文件,请单击此处。
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('homelessness.csv')
# plotting two histograms on the same axis
plt.hist(df['individuals'], bins=25, alpha=0.45, color='red')
plt.hist(df['family_members'], bins=25, alpha=0.45, color='blue')
plt.title("histogram with multiple \
variables (overlapping histogram)")
plt.legend(['individuals who are homeless',
'family members who are homeless'])
plt.show()
输出:
示例 3:在同一轴上绘制三个直方图
plt.hist() 方法被多次使用来创建三个重叠直方图的图形。我们根据需要调整不透明度、颜色和箱数。数据框中的三个不同列被用作直方图的数据。
要查看或下载使用的 CSV 文件,请单击 Medals_by_country_2016
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('medals_by_country_2016.csv')
# plotting three histograms on the same axis
plt.hist(df['Bronze'],bins = 25, alpha = 0.5,
color = 'red')
plt.hist(df['Gold'],bins = 25, alpha = 0.5,
color = 'blue')
plt.hist(df['Silver'],bins = 25, alpha = 0.5,
color = 'yellow')
plt.title("histogram with multiple \
variables (overlapping histogram)")
plt.legend(['Bronze','Gold','Silver'])
plt.show()
输出: