python數據可視化編程實戰


課程代碼地址:
https://github.com/driws/python-data-visualiztion-cookbook/tree/master/3367OS_02_Code

環境准備

虛擬環境

matplotlib文檔

https://matplotlib.org/




axex: 設置坐標軸邊界和表面的顏色、坐標刻度值大小和網格的顯示
backend: 設置目標暑促TkAgg和GTKAgg
figure: 控制dpi、邊界顏色、圖形大小、和子區( subplot)設置
font: 字體集(font family)、字體大小和樣式設置
grid: 設置網格顏色和線性
legend: 設置圖例和其中的文本的顯示
line: 設置線條(顏色、線型、寬度等)和標記
patch: 是填充2D空間的圖形對象,如多邊形和圓。控制線寬、顏色和抗鋸齒設置等。
savefig: 可以對保存的圖形進行單獨設置。例如,設置渲染的文件的背景為白色。
verbose: 設置matplotlib在執行期間信息輸出,如silent、helpful、debug和debug-annoying。
xticks和yticks: 為x,y軸的主刻度和次刻度設置顏色、大小、方向,以及標簽大小。

api

matplotlib.figure

實驗

體驗


#1
from matplotlib  import pyplot as  plt 
x = range(2,26,2)
y = [15,13,14.5,17,20,25,26,26,24,22,18,15]
plt.figure(figsize=(20,8),dpi=80)
plt.rc("lines", linewidth=2, color='g')
plt.plot(x, y)
plt.xticks(range(2,25))
plt.show()

#2 餅圖
import numpy  as np
from matplotlib  import pyplot as  plt 
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
plt.rcParams['lines.color'] = 'r'
plt.plot(t, s)
c  = np.cos(2 * np.pi * t)
plt.rcParams['lines.linewidth'] = '3'
plt.plot(t,c)
plt.show()


#3 正反余弦
import numpy  as np
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
plt.rcParams['lines.color'] = 'r'
plt.plot(t, s)
c = np.cos(2 * np.pi * t)
plt.rcParams['lines.linewidth'] = '3'
plt.plot(t,c)
plt.show()

基礎知識

Figure

Figure對象 一張畫板

import matplotlib.pyplot as plt
fig = plt.figure()

Axes軸

軸,大概意思是在畫布上Figure 有多個區域,每個區域是一個圖形,即一個坐標軸

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='An Example Axes',
       ylabel='Y-Axis', xlabel='X-Axis')
plt.show()


##1
fig = plt.figure()
ax = fig.add_subplot(1,2,1)  ##表示一行倆列里第一個坐標軸  左右排列   默認得畫布大小
ax1 = fig.add_subplot(1,2,2) ##表示一行倆列里第二個坐標軸
plt.show()

#2
fig = plt.figure()
ax = fig.add_subplot(2,1,1)  ##表示二行1列里第一個坐標軸 上下排列   默認得畫布大小
ax1 = fig.add_subplot(2,1,2)  ##表示二行1列里第二個坐標軸
plt.show()

更簡單得做法:

fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].set(title='Upper Left')
axes[0,1].set(title='Upper Right')
axes[1,0].set(title='Lower Left')
axes[1,1].set(title='Lower Right')

##手工畫
plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='red', linewidth=3)
plt.xlim(0.5, 4.5)
plt.show()

參考:https://blog.csdn.net/qq_34859482/article/details/80617391
https://www.cnblogs.com/zhizhan/p/5615947.html

書籍案例

讀取CSV

###############
cat  ch02-data.csv 
"day","ammount"
2013-01-24,323
2013-01-25,233
2013-01-26,433
2013-01-27,555
2013-01-28,123
2013-01-29,0
2013-01-30,221
########################


import csv

filename = '/home/jupyterlab/cookbook/02/ch02-data.csv'

data = []
try:
    with open(filename) as f:
        reader = csv.reader(f)
        c = 0
        for row in reader:
            if c == 0:
                header = row
            else:
                data.append(row)
            c += 1
except csv.Error as e:
    print("Error reading CSV file at line %s: %s" % (reader.line_num, e))
    sys.exit(-1)

if header:
    print(header) 
    print('==================')

for datarow in data:
    print(datarow)

讀取excel 文件

從定寬文件中讀取數據

###
head  -10 ch02-fixed-width-1M.data
161322597 0386544351896 0042
296411327 6945080785634 2301
164726383 4090941067228 5553
575768002 4862349192103 5643
483535208 6007251611732 4649
050291308 8263754667453 9141
207152670 3984356804116 9532
427053180 1466959270421 5338
316700885 9726131532544 4920
138359697 3286515244210 7400
###

import struct
import string

mask='9s14s5s'  ##定義將要讀取的字符串格式,掩碼格式
parse = struct.Struct(mask).unpack_from
print('formatstring {!r}, record size: {}'.format(mask, struct.calcsize(mask)))
datafile = '/home/jupyterlab/cookbook/02/ch02-fixed-width-1M.data'

with open(datafile, 'r') as f:
    i = 0 
    for line in f:
        if i < 30:
            fields = line.split()
            print('fields: ', [field.strip() for field in fields])
            i = i+1
        else:
            break


讀取json

https://developer.github.com/v3/activity/events/#list-public-events

import requests

url = 'https://api.github.com/events'
r = requests.get(url)
json_obj = r.json()

repos = set() # we want just unique urls
for entry in json_obj:
    try:
        repos.add(entry['repo']['url'])
    except KeyError as e:
        print("No key %s. Skipping..." % (e))

from pprint import pprint 
pprint(repos)
#######################顯示
{'https://api.github.com/repos/Hong1008/freelec-springboot2-webservice',
 'https://api.github.com/repos/Nice-Festival/nice-festival-backend',
 'https://api.github.com/repos/OurHealthcareHK/api',
 'https://api.github.com/repos/SkavenNoises/SynthRevolution',
 'https://api.github.com/repos/TQRG/Slide',
 'https://api.github.com/repos/actengage/patriotpoll',
 'https://api.github.com/repos/ahmad-elkhawaldeh/ICS2OR-Unit0-03-HTML',
 'https://api.github.com/repos/cjdenio/cjdenio.github.io',
 'https://api.github.com/repos/cris-Duarte/asisapp',
 'https://api.github.com/repos/devhub-blue-sea/org-public-empty-repo-eu-north-1',
 'https://api.github.com/repos/dginelli/repairnator-experiments-jkali-one-failing-test',
 'https://api.github.com/repos/esozen2012/python-dictionaries-readme-ds-apply-000',
 'https://api.github.com/repos/gywgithub/TypeScriptExamples',
 'https://api.github.com/repos/hknqp/test',
 'https://api.github.com/repos/hodersolutions/a1jobs-2.0',
 'https://api.github.com/repos/ikding/ikding.github.io',
 'https://api.github.com/repos/jaebradley/nba-stats-client',
 'https://api.github.com/repos/joshwnj/react-visibility-sensor',
 'https://api.github.com/repos/leewp14/android_kernel_samsung_smdk4412',
 'https://api.github.com/repos/necromancyonline/necromancy-server',
 'https://api.github.com/repos/pbaffiliate10/repoTestCheckout',
 'https://api.github.com/repos/rinorocks8/rinorocks8.github.io',
 'https://api.github.com/repos/rmagick/rmagick',
 'https://api.github.com/repos/schoolhackingclub/schoolhackingclub.github.io',
 'https://api.github.com/repos/shanemcq18/rom-operator-inference-Python3',
 'https://api.github.com/repos/soaresrodrigo/perfil',
 'https://api.github.com/repos/stanford-ssi/homebrew-rocket-sim',
 'https://api.github.com/repos/tacotaco2/Curie',
 'https://api.github.com/repos/viljamis/vue-design-system'}

numpy 打印圖像

import scipy.misc
import matplotlib.pyplot as plt
lena = scipy.misc.ascent()
plt.gray()
plt.imshow(lena)
plt.colorbar()
plt.show()

print(lena.shape)
print(lena.max())
print(lena.dtype)

PIL輸出圖像

import numpy
from PIL import Image
import matplotlib.pyplot as plt

bug = Image.open('/home/jupyterlab/cookbook/02/stinkbug.png')
arr = numpy.array(bug.getdata(), numpy.uint8).reshape(bug.size[1], bug.size[0], 3)

plt.gray()
plt.imshow(arr)
plt.colorbar()
plt.show()

matplotlib 畫圖


為plot 提供的是y值,plot 默認為x軸提供了0到8的線性值,數量為y值的個數


提供了x y 值

各種圖

from matplotlib.pyplot import *

x = [1,2,3,4]
y = [5,4,3,2]

figure()  ##創建新的圖表 ,所有接下來的繪圖操作都在該圖表


subplot(231)  ##231表示共有2行* 3列圖表,指定在第一個上描述
plot(x, y)

subplot(232)
bar(x, y)

subplot(233)
barh(x, y)

subplot(234)
bar(x, y)
y1 = [7,8,5,3]
bar(x, y1, bottom=y, color = 'r')

subplot(235)
boxplot(x)

subplot(236)
scatter(x,y)

show()

箱線圖

from pylab import *

dataset = [113, 115, 119, 121, 124, 
           124, 125, 126, 126, 126,
           127, 127, 128, 129, 130,
           130, 131, 132, 133, 136]


subplot(121)
boxplot(dataset, vert=False)

subplot(122)
hist(dataset)

show()

正余弦圖

import matplotlib.pyplot as pl
import numpy as np

# generate uniformly distributed 
# 256 points from -pi to pi, inclusive
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)

# these are vectorised versions
# of math.cos, and math.sin in built-in Python maths
# compute cos for every x
y = np.cos(x)

# compute sin for every x
y1 = np.sin(x)

# plot both cos and sin
pl.plot(x, y)
pl.plot(x, y1)

pl.show()

美化后:

from pylab import *
import numpy as np

# generate uniformly distributed 
# 256 points from -pi to pi, inclusive
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)

# these are vectorised versions
# of math.cos, and math.sin in built-in Python maths
# compute cos for every x
y = np.cos(x)

# compute sin for every x
y1 = np.sin(x)

# plot cos
plot(x, y)

# plot sin
plot(x, y1)

# define plot title
title("Functions $\sin$ and $\cos$")

# set x limit
xlim(-3.0, 3.0)
# set y limit
ylim(-1.0, 1.0)

# format ticks at specific values
xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
          [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
yticks([-1, 0, +1],
          [r'$-1$', r'$0$', r'$+1$'])

show()

設置坐標軸的長度和范圍

l = [-1, 1 , -10 , 10]  ##分別代表xmin xmax ymin ymax   xy 坐標軸的刻度
axis(l)


使用axis 設置坐標軸的刻度,不設置默認使用最小值
做標軸添加一條線

添加網格屬性,默認是關閉的

設置圖表的線型,屬性,格式化字符串

##線條粗細
plt.grid()
axhline(4, linewidth=8)
plot(x, y , linewidth=10)

設置線條屬性


顏色:

color = "#eeefff"
title("title is ", color="#123456")
plt.grid()
plot(x, y , linewidth=10)

設置刻度,刻度標簽和網格

figure 圖形
subplots 子區
figure()會顯式的創建一個圖形,表示圖型用戶界面窗口
plot() 隱士的創建圖形
一個圖形包含一個或多個子區,子區以網格方式排列plot subplot() 調用時指定所有plot的行數和列數及要操作的plot 序號

##圖1
from pylab import *

# get current axis
ax = gca()

# set view to tight, and maximum number of tick intervals to 10
ax.locator_params(tight=True, nbins = 10)

# generate 100 normal distribution values
ax.plot(np.random.normal(10, .1, 100))

show()

##圖2
from pylab import *
import matplotlib as mpl
import datetime

fig = figure()

# get current axis
ax = gca()

# set some daterange
start = datetime.datetime(2013, 1, 1)
stop = datetime.datetime(2013, 12, 31)
delta = datetime.timedelta(days = 1)

# convert dates for matplotlib
dates = mpl.dates.drange(start, stop, delta)

# generate some random values
values = np.random.rand(len(dates))

ax = gca()

# create plot with dates
ax.plot_date(dates, values, linestyle='-', marker='')

# specify formater
date_format = mpl.dates.DateFormatter('%Y-%m-%d')

# apply formater
ax.xaxis.set_major_formatter(date_format)

# autoformat date labels
# rotates labels by 30 degrees by default
# use rotate param to specify different rotation degree 
# use bottom param to give more room to date labels
fig.autofmt_xdate()

show()

添加圖例和注解

from matplotlib.pyplot import *

# generate different normal distributions
x1 = np.random.normal(30, 3, 100)
x2 = np.random.normal(20, 2, 100)
x3 = np.random.normal(10, 3, 100)

# plot them
plot(x1, label='plot')
plot(x2, label='2nd plot')
plot(x3, label='last plot')

# generate a legend box
legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
       ncol=3, mode="expand", borderaxespad=0.)

# annotate an important value
annotate("Important value", (55,20), xycoords='data',
         xytext=(5, 38), 
         arrowprops=dict(arrowstyle='->')) 
show()

移動軸線到圖中央

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-np.pi, np.pi, 500, endpoint=True) 
y = np.sin(x)

plt.plot(x, y)

ax = plt.gca()

# hide two spines 
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# move bottom and left spine to 0,0
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

# move ticks positions
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

plt.show()

直方圖

import numpy as np
import matplotlib.pyplot as plt

mu = 100
sigma = 15
x = np.random.normal(mu, sigma, 10000)

ax = plt.gca()

# the histogram of the data
ax.hist(x, bins=35, color='r')

ax.set_xlabel('Values')
ax.set_ylabel('Frequency')

ax.set_title(r'$\mathrm{Histogram:}\ \mu=%d,\ \sigma=%d$' % (mu, sigma))

plt.show()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM