Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖
matplotlib介紹
- Matplotlib 是 Python 的繪圖庫(kù)。 它可與 NumPy 一起使用,提供了一種有效的 MatLab 開(kāi)源替代方案。 它也可以和圖形工具包一起使用,如 PyQt 和 wxPython。
- 安裝Matplotlib庫(kù)命令:在cmd命令窗口輸入pip install matplotlib。
matplotlib繪制折線圖
1、繪制一條折線的折線圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
# 處理亂碼
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
x = [1, 2, 3, 4]
y = [10, 50, 20, 100]
# "r" 表示紅色,ms用來(lái)設(shè)置*的大小
plt.plot(x, y, "r", marker='*', ms=10, label="a")
# plt.plot([1, 2, 3, 4], [20, 30, 80, 40], label="b")
plt.xticks(rotation=45)
plt.xlabel("發(fā)布日期")
plt.ylabel("小說(shuō)數(shù)量")
plt.title("80小說(shuō)網(wǎng)活躍度")
# upper left 將圖例a顯示到左上角
plt.legend(loc="upper left")
# 在折線圖上顯示具體數(shù)值, ha參數(shù)控制水平對(duì)齊方式, va控制垂直對(duì)齊方式
for x1, y1 in zip(x, y):
plt.text(x1, y1 + 1, str(y1), ha='center', va='bottom', fontsize=20, rotation=0)
plt.savefig("a.jpg")
plt.show()
圖形效果展示:

注意:savefig()是圖形存儲(chǔ)成圖片,show()是將圖形顯示出來(lái)。
2、繪制多條折線
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
x = [1, 2, 3, 4]
y1 = [45, 50, 20, 100]
y2 = [26, 10, 76, 25]
y3 = [11, 66, 55, 88]
y4 = [69, 50, 35, 100]
plt.plot(x, y1, marker='*', ms=10, label="a")
plt.plot(x, y2, marker='*', ms=10, label="b")
plt.plot(x, y3, marker='*', ms=10, label="c")
plt.plot(x, y4, marker='*', ms=10, label="d")
plt.xticks(rotation=45)
plt.xlabel("發(fā)布日期")
plt.ylabel("小說(shuō)數(shù)量")
plt.title("80小說(shuō)網(wǎng)活躍度")
plt.legend(loc="upper left")
# 在折線圖上顯示具體數(shù)值, ha參數(shù)控制水平對(duì)齊方式, va控制垂直對(duì)齊方式
for y in [y1, y2, y3, y4]:
for x1, yy in zip(x, y):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
plt.savefig("a.jpg")
plt.show()
圖形效果展示:

matplotlib繪制柱狀圖
1、繪制普通柱狀圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = [1, 2, 3, 4]
y = [450, 500, 200, 1000]
# 繪圖
plt.bar(x=x, height=y, label='書(shū)庫(kù)大全', color='steelblue', alpha=0.8)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對(duì)齊方式, va控制垂直對(duì)齊方式
for x1, yy in zip(x, y):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標(biāo)題
plt.title("80小說(shuō)網(wǎng)活躍度")
# 為兩條坐標(biāo)軸設(shè)置名稱
plt.xlabel("發(fā)布日期")
plt.ylabel("小說(shuō)數(shù)量")
# 顯示圖例
plt.legend()
plt.savefig("a.jpg")
plt.show()
圖形效果展示:

2、繪制多組柱狀圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]
# 繪圖
plt.bar(x=x, height=y1, label='python', color='steelblue', alpha=0.8)
plt.bar(x=x, height=y2, label='java', color='indianred', alpha=0.8)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對(duì)齊方式, va控制垂直對(duì)齊方式
for x1, yy in zip(x, y1):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
for x1, yy in zip(x, y2):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標(biāo)題
plt.title("python與java圖書(shū)對(duì)比")
# 為兩條坐標(biāo)軸設(shè)置名稱
plt.xlabel("年份")
plt.ylabel("銷量")
# 顯示圖例
plt.legend()
plt.savefig("a.jpg")
plt.show()
圖形效果展示:

3、繪制柱狀圖的條柱并列顯示
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]
bar_width = 0.3
# 將X軸數(shù)據(jù)改為使用range(len(x_data), 就是0、1、2...
plt.bar(x=range(len(x)), height=y1, label='python', color='steelblue', alpha=0.8, width=bar_width)
# 將X軸數(shù)據(jù)改為使用np.arange(len(x_data))+bar_width,
# 就是bar_width、1+bar_width、2+bar_width...這樣就和第一個(gè)柱狀圖并列了
plt.bar(x=np.arange(len(x)) + bar_width, height=y2, label='java', color='indianred', alpha=0.8, width=bar_width)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對(duì)齊方式, va控制垂直對(duì)齊方式
for x1, yy in enumerate(y1):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
for x1, yy in enumerate(y2):
plt.text(x1 + bar_width, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標(biāo)題
plt.title("python與java對(duì)比")
# 為兩條坐標(biāo)軸設(shè)置名稱
plt.xlabel("年份")
plt.ylabel("銷量")
# 顯示圖例
plt.legend()
plt.savefig("a.jpg")
plt.show()
圖形效果展示:

matplotlib繪制柱線混合圖
1、繪制柱線混合圖
# -*- coding:utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑體顯示中文
# 構(gòu)建數(shù)據(jù)
x = [2, 4, 6, 8]
y = [450, 500, 200, 1000]
# 繪圖
plt.bar(x=x, height=y, label='書(shū)庫(kù)大全', color='steelblue', alpha=0.8)
# 在柱狀圖上顯示具體數(shù)值, ha參數(shù)控制水平對(duì)齊方式, va控制垂直對(duì)齊方式
for x1, yy in zip(x, y):
plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# 設(shè)置標(biāo)題
plt.title("80小說(shuō)網(wǎng)活躍度")
# 為兩條坐標(biāo)軸設(shè)置名稱
plt.xlabel("發(fā)布日期")
plt.ylabel("小說(shuō)數(shù)量")
# 顯示圖例
plt.legend()
# 畫(huà)折線圖
plt.plot(x, y, "r", marker='*', ms=10, label="a")
plt.xticks(rotation=45)
plt.legend(loc="upper left")
plt.savefig("a.jpg")
plt.show()
圖形效果展示:

總結(jié)
到此這篇關(guān)于Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖的文章就介紹到這了,更多相關(guān)Python matplotlib繪制折線圖 柱狀圖 柱線混合圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Python的matplotlib庫(kù)繪制柱狀圖
- 教你利用python的matplotlib(pyplot)繪制折線圖和柱狀圖
- python如何利用matplotlib繪制并列雙柱狀圖并標(biāo)注數(shù)值
- Python使用matplotlib給柱狀圖添加數(shù)據(jù)標(biāo)簽bar_label()
- python中如何利用matplotlib畫(huà)多個(gè)并列的柱狀圖
- Python用?matplotlib?繪制柱狀圖
- python使用matplotlib畫(huà)柱狀圖、散點(diǎn)圖
- python使用matplotlib繪制柱狀圖教程
- Python 如何利用pandas 和 matplotlib繪制柱狀圖
相關(guān)文章
淺談numpy數(shù)組中冒號(hào)和負(fù)號(hào)的含義
下面小編就為大家分享一篇淺談numpy數(shù)組中冒號(hào)和負(fù)號(hào)的含義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python十進(jìn)制和二進(jìn)制的轉(zhuǎn)換方法(含浮點(diǎn)數(shù))
這篇文章主要介紹了python十進(jìn)制和二進(jìn)制的轉(zhuǎn)換方法(含浮點(diǎn)數(shù)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python實(shí)現(xiàn)為PDF去除水印的示例代碼
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)PDF去除水印功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Python中關(guān)鍵字global和nonlocal的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Python中關(guān)鍵字global和nonlocal的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
python 簡(jiǎn)單備份文件腳本v1.0的實(shí)例
下面小編就為大家?guī)?lái)一篇python 簡(jiǎn)單備份文件腳本v1.0的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望對(duì)大家有所幫助2017-11-11
pytorch神經(jīng)網(wǎng)絡(luò)之卷積層與全連接層參數(shù)的設(shè)置方法
今天小編就為大家分享一篇pytorch神經(jīng)網(wǎng)絡(luò)之卷積層與全連接層參數(shù)的設(shè)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python數(shù)學(xué)建模庫(kù)StatsModels統(tǒng)計(jì)回歸簡(jiǎn)介初識(shí)
這篇文章主要為大家介紹了Python數(shù)學(xué)建模庫(kù)StatsModels統(tǒng)計(jì)回歸的基本概念,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝打擊多多進(jìn)步2021-10-10

