Python實(shí)現(xiàn)數(shù)據(jù)可視化案例分析
1. 問題描述
對(duì)右圖進(jìn)行修改:
- 請(qǐng)更換圖形的風(fēng)格
- 請(qǐng)將 x 軸的數(shù)據(jù)改為-10 到 10
- 請(qǐng)自行構(gòu)造一個(gè) y 值的函數(shù)
- 將直方圖上的數(shù)字,位置改到柱形圖的內(nèi)部垂直居中的位置
- 對(duì)成績數(shù)據(jù) data1402.csv 進(jìn)行分段統(tǒng)計(jì):每 5 分作為一個(gè)分?jǐn)?shù)段,展示出每個(gè)分?jǐn)?shù)段的人數(shù)直方圖。
- 自行創(chuàng)建出 10 個(gè)學(xué)生的 3 個(gè)學(xué)期排名數(shù)據(jù),并通過直方圖進(jìn)行對(duì)比展示。
- 線圖
- 把這個(gè)圖像做一些調(diào)整,要求出現(xiàn) 5 個(gè)完整的波峰。
- 調(diào)大 cos 波形的幅度
- 調(diào)大 sin 波形的頻率
- 用線圖展示北京空氣質(zhì)量數(shù)據(jù)
展示 10-15 年 PM 指數(shù)月平均數(shù)據(jù)的變化情況,一幅圖中有 6 條曲線,每年 1 條曲線。
2. 實(shí)驗(yàn)環(huán)境
Microsoft Windows 10 版本18363
? PyCharm 2020.2.1 (Community Edition)
? Python 3.8(Scrapy 2.4.0 + numpy 1.19.4 + pandas 1.1.4 + matplotlib 3.3.3)
3. 實(shí)驗(yàn)步驟及結(jié)果
對(duì)右圖進(jìn)行修改:
- 請(qǐng)更換圖形的風(fēng)格
- 請(qǐng)將 x 軸的數(shù)據(jù)改為-10 到 10
- 請(qǐng)自行構(gòu)造一個(gè) y 值的函數(shù)
- 將直方圖上的數(shù)字,位置改到柱形圖的內(nèi)部垂直居中的位置
from matplotlib import pyplot as plt import numpy as np fig, ax = plt.subplots() plt.style.use('classic') plt.title("square numbers") ax.set_xlim(-11, 11) ax.set_ylim(0, 100) x = np.array(range(-10, 11)) y = x * x rect1 = plt.bar(x, y) for r in rect1: ax.text(r.get_x(), r.get_height() / 2, r.get_height()) plt.show()
如圖使用 classic 風(fēng)格,x 軸數(shù)據(jù)為[-10, 10]的整數(shù),構(gòu)造的函數(shù)為 y=x2,顯示位置并將其將數(shù)值改到了柱形圖內(nèi)部垂直居中的位置。
對(duì)成績數(shù)據(jù) data1402.csv 進(jìn)行分段統(tǒng)計(jì):每 5 分作為一個(gè)分?jǐn)?shù)段,展示出每個(gè)分?jǐn)?shù)段的人數(shù)直方圖。
from matplotlib import pyplot as plt import numpy as np import pandas as pd df = pd.read_csv("./data1402.csv", encoding='utf-8', dtype=str) df = pd.DataFrame(df, columns=['score'], dtype=np.float) section = np.array(range(0, 105, 5)) result = pd.cut(df['score'], section) count = pd.value_counts(result, sort=False) fig, ax = plt.subplots() plt.style.use('classic') ax.set_xlim(0, 100) rect1 = plt.bar(np.arange(2.5, 100, 5), count, width=5) for r in rect1: ax.text(r.get_x(), r.get_height(), r.get_height()) plt.show()
自行創(chuàng)建出 10 個(gè)學(xué)生的 3 個(gè)學(xué)期排名數(shù)據(jù),并通過直方圖進(jìn)行對(duì)比展示。
import random semester1 = np.arange(1, 11) semester2 = np.arange(1, 11) semester3 = np.arange(1, 11) random.shuffle(semester1) random.shuffle(semester2) random.shuffle(semester3) df = pd.DataFrame({'semester1':semester1, 'semester2':semester2, 'semester3':semester3}) print(df) df.to_csv("data1403.csv", encoding="utf-8")
使用如上代碼創(chuàng)建出隨機(jī)的排名數(shù)據(jù)。
df = pd.read_csv("./data1403.csv", encoding='utf-8', dtype=str) df = pd.DataFrame(df, columns=['semester1', 'semester2', 'semester3'], dtype=np.int) df['total'] = (df['semester1'] + df['semester2'] + df['semester3']) / 3 df = df.sort_values('total') fig, ax = plt.subplots() plt.style.use('classic') plt.title('RANK') width = 0.2 x = np.array(range(0, 10)) rect1 = ax.bar(x-2*width, df['semester1'], width=width, label='semester1') rect2 = ax.bar(x-width, df['semester2'], width=width, label='semester2') rect3 = ax.bar(x, df['semester3'], width=width, label='semester3') for r in rect1: ax.text(r.get_x(), r.get_height(), r.get_height()) for r in rect2: ax.text(r.get_x(), r.get_height(), r.get_height()) for r in rect3: ax.text(r.get_x(), r.get_height(), r.get_height()) plt.legend(ncol=1) plt.show()
如上代碼繪圖:
線圖 :
- 把這個(gè)圖像做一些調(diào)整,要求出現(xiàn) 5 個(gè)完整的波峰。
- 調(diào)大 cos 波形的幅度
- 調(diào)大 sin 波形的頻率
import numpy as np from matplotlib import pyplot as plt x = np.linspace(-5 * np.pi, 5 * np.pi, 500) y1 = 3 * np.cos(x) y2 = np.sin(4*x) fig, ax = plt.subplots() plt.style.use('classic') ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines['bottom'].set_position(('data',0)) ax.xaxis.set_ticks_position('bottom') ax.spines['left'].set_position(('data',0)) ax.yaxis.set_ticks_position('left') plt.plot(x, y1, color='blue', linestyle='-', label='y=3cosx') plt.plot(x, y2, color='red', linestyle='-', label='y=sin3x') plt.legend() plt.show()
用線圖展示北京空氣質(zhì)量數(shù)據(jù)
展示 10-15 年 PM 指數(shù)月平均數(shù)據(jù)的變化情況,一幅圖中有 6 條曲線,每年 1 條曲線。
import numpy as np import pandas as pd from matplotlib import pyplot as plt orig_df = pd.read_csv("./BeijingPM20100101_20151231.csv", encoding='utf-8', dtype=str) orig_df = pd.DataFrame(orig_df, columns=['year', 'month', 'PM_US Post']) df = orig_df.dropna(0, how='any') df['month'] = df['month'].astype(int) df['year'] = df['year'].astype(int) df['PM_US Post'] = df['PM_US Post'].astype(int) df.reset_index(drop=True, inplace=True) num = len(df) section = np.arange(1, 13) record = 0 fig, ax = plt.subplots() plt.style.use('classic') plt.title("2010-2015 Beijing average PM2.5(from PM_US Post) per month") for nowyear in range(2010, 2016): i = record result = [0 for i in range(13)] nowsum = 0 cntday = 0 nowmonth = 1 while i < num: if df['month'][i] == nowmonth: cntday = cntday + 1 nowsum = nowsum + df['PM_US Post'][i] else: if df['year'][i] != nowyear: record = i result[nowmonth] = nowsum / cntday break result[nowmonth] = nowsum / cntday cntday = 1 nowsum = df['PM_US Post'][i] nowmonth = df['month'][i] i = i + 1 result = result[1:] # x = np.array(range(1, 13)) plt.plot(x, result, linestyle='-', label=str(nowyear)) plt.legend() plt.show()
到此這篇關(guān)于Python實(shí)現(xiàn)數(shù)據(jù)可視化案例分析的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
查看keras的默認(rèn)backend實(shí)現(xiàn)方式
這篇文章主要介紹了查看keras的默認(rèn)backend實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python添加列表元素append(),extend()及?insert()
這篇文章主要介紹了python添加列表元素append(),extend()及?insert(),列表是儲(chǔ)存元素的數(shù)據(jù)類型,既然能存儲(chǔ)元素,那么就類似數(shù)據(jù)庫一樣,增刪改查的一些功能就不能少了。下面我們就來先看看添加列表元素方法有哪些,需要的朋友可以參考一下2022-03-03詳談Python3 操作系統(tǒng)與路徑 模塊(os / os.path / pathlib)
下面小編就為大家分享一篇詳談Python3 操作系統(tǒng)與路徑 模塊(os / os.path / pathlib),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04詳解Python中range()與xrange()的區(qū)別
range()?和?xrange()?是兩個(gè)函數(shù),可用于在?Python的?for?循環(huán)中迭代一定次數(shù)。本文將通過示例詳細(xì)說說二者的區(qū)別與使用,需要的可以參考一下2022-09-09Python使用窮舉法求兩個(gè)數(shù)的最大公約數(shù)問題
這篇文章主要介紹了Python使用窮舉法求兩個(gè)數(shù)的最大公約數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12