Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

Python數據可視化庫

  • Matplotlib:其能夠支持所有的2D作圖和部分3D作圖。能通過交互環境做出印刷質量的圖像。
  • Seaborn:基於Matplotlib,seaborn提供許多功能,比如:內置主題、顏色調色板、函數和提供可視化單變量、雙變量、線性迴歸的工具。其能幫助我們構建複雜的可視化。

數據集

EMPIDGenderAgeSalesBMIIncomeE001M34123Normal350E002F40114Overweight450E003F37135Obesity169E004M30139Underweight189E005F44117Underweight183E006M36121Normal80E007M32133Obesity166E008F26140Normal120E009M32133Normal75E010M36133Underweight40

作圖

# -*- coding:UTF-8 -*-import matplotlib.pyplot as pltimport pandas as pdimport seaborn as snsimport numpy as np# 0、導入數據集df = pd.read_excel('first.xlsx', 'Sheet1')123456789# 1、直方圖fig = plt.figure()ax = fig.add_subplot(111)ax.hist(df['Age'], bins=7)plt.title('Age distribution')plt.xlabel('Age')plt.ylabel('Employee')plt.show()12345678
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

# 2、箱線圖 fig = plt.figure()ax = fig.add_subplot(111)ax.boxplot(df['Age'])plt.show()12345
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

# 3、小提琴圖sns.violinplot(df['Age'], df['Gender'])sns.despine()plt.show()1234
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

# 4、條形圖var = df.groupby('Gender').Sales.sum()fig = plt.figure()ax1 = fig.add_subplot(111)ax1.set_xlabel('Gender')ax1.set_ylabel('Sum of Sales')ax1.set_title('Gender wise Sum of Sales')var.plot(kind='bar')plt.show()123456789
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

# 5、折線圖var = df.groupby('BMI').Sales.sum()fig = plt.figure()ax = fig.add_subplot(111)ax.set_xlabel('BMI')ax.set_ylabel('Sum of Sales')ax.set_title('BMI wise Sum of Sales')var.plot(kind='line')plt.show()123456789
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

# 6、堆積柱形圖var = df.groupby(['BMI', 'Gender']).Sales.sum()var.unstack().plot(kind='bar', stacked=True, color=['red', 'blue'])plt.show()1234
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

# 7、散點圖fig = plt.figure()ax = fig.add_subplot(111)ax.scatter(df['Age'], df['Sales'])plt.show()12345
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

# 8、氣泡圖fig = plt.figure()ax = fig.add_subplot(111)ax.scatter(df['Age'], df['Sales'], s=df['Income']) # 第三個變量表明根據收入氣泡的大小plt.show()
Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

Python-Matplotlib(3) 條形圖實戰

Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

Python數據可視化編程,Matplotlib、 直方圖等你會幾種?

好了,今天的知識就分享到這裡,歡迎關注愛編程的南風,私信關鍵詞:學習資料,獲取更多學習資源,如果文章對你有有幫助,請收藏關注,在今後與你分享更多學習python的文章。同時歡迎在下面評論區留言如何學習python。


分享到:


相關文章: