1. 程式人生 > >吳裕雄 實戰PYTHON編程(6)

吳裕雄 實戰PYTHON編程(6)

生成 end red 導入 mat value ray odin plt

import matplotlib.pyplot as plt

plt.rcParams[‘font.sans-serif‘]=[‘Simhei‘]
plt.rcParams[‘axes.unicode_minus‘]=False

listx1 = [1,5,7,9,13,16]
listy1 = [15,50,80,40,70,50]
plt.bar(listx1, listy1, label="男性")
listx2 = [2,6,8,11,14,16]
listy2 = [10,40,30,50,80,60]
plt.bar(listx2, listy2, color="red", label="女性")
plt.legend()
plt.xlim(0, 20)
plt.ylim(0, 100)
plt.title("零花錢統計")
plt.xlabel("年齡")
plt.ylabel("零花錢數量")
plt.show()

技術分享圖片

import matplotlib.pyplot as plt

listx = [1,5,7,9,13,16]
listy = [15,50,80,40,70,50]
plt.plot(listx, listy, color ="red")
plt.show()

技術分享圖片

import matplotlib.pyplot as plt

listx1 = [1,5,7,9,13,16]
listy1 = [15,50,80,40,70,50]
plt.plot(listx1, listy1, label="Male")
listx2 = [2,6,8,11,14,16]
listy2 = [10,40,30,50,80,60]
plt.plot(listx2, listy2, color="red", linewidth=5.0, linestyle="--", label="Female")
plt.legend()
plt.xlim(0, 20)
plt.ylim(0, 100)
plt.title("Pocket Money")
plt.xlabel("Age")
plt.ylabel("Money")
plt.show()

技術分享圖片

import matplotlib.pyplot as plt

labels = ["東部", "南部", "北部", "中部"]
sizes = [5, 10, 20, 15]
colors = ["red", "green", "blue", "yellow"]
explode = (0, 0, 0.05, 0)
plt.pie(sizes,explode = explode,labels = labels,colors = colors,\
labeldistance = 1.1,autopct = "%3.1f%%",shadow = True,\
startangle = 90,pctdistance = 0.6)
plt.axis("equal")
plt.legend()
plt.show()

技術分享圖片

import numpy as np
import matplotlib.pyplot as plt #導入繪圖模塊,重命名為plt
import requests #導入網頁內容抓取包
from bs4 import BeautifulSoup as bs #導入網頁解析模塊,重命名為bs
from pylab import * #導入pylab包

rcParams[‘font.sans-serif‘] = [‘SimHei‘] #讓matplotlib支持簡體中文

year = [] #橫坐標列表
gdp = [] #縱坐標列表
#url = "http://value500.com/M2GDP.html" #設置要在哪個網頁抓數據
url = "http://value500.com/M2GDP.html"
content = requests.get(url) #獲取網頁內容
print(content)
content.encoding=‘utf-8‘ #轉為utf-8編碼
content1=content.text #取得網頁內容的text部分
parse = bs(content1,"html.parser") #進行html解析
data1 = parse.find_all("table") #獲取所有表元素
rows = data1[19].find_all("tr") #取出包含所需數據的表(網頁第20個表)
i=0 #為了不讀取表頭數據,設置此控制變量
for row in rows:
cols = row.find_all("td") #把每一行表數據存入cols變量
if(len(cols) > 0 and i==0): #如果是第一行,則控制變量加1
i+=1
else: #如果不是第一行,則寫入繪圖列表
year.append(cols[0].text[:-2]) #取得年份數據(數據的最後兩個字符不是數據需去除)並寫入圖形的year軸
gdp.append(cols[2].text) #把gdp值存入gdp軸
plt.plot(year, gdp, linewidth=2.0) #繪制圖形,線寬為2
plt.title("1990~2016年度我國GDP") #設置圖形標題
plt.xlabel("年度") #設置x軸標題
plt.ylabel("GDP(億元)") #設置y軸標題
plt.show() #顯示所繪圖形

from bokeh.plotting import figure, show

p = figure(width=800, height=400)
listx = [1,5,7,9,13,16]
listy = [15,50,80,40,70,50]
p.line(listx, listy)
show(p)

from bokeh.plotting import figure, show, output_file

output_file("F:\\pythonBase\\pythonex\\lineout.html")
p = figure(width=800, height=400)
listx = [1,5,7,9,13,16]
listy = [15,50,80,40,70,50]
p.line(listx, listy)
show(p)

from bokeh.plotting import figure, show

p = figure(width=800, height=400, title="零花錢統計")
# p.title_text_color = "green"
# p.title_text_font_size = "18pt"
p.xaxis.axis_label = "年齡"
p.xaxis.axis_label_text_color = "violet"
p.yaxis.axis_label = "零花錢"
p.yaxis.axis_label_text_color = "violet"
dashs = [12, 4]
listx1 = [1,5,7,9,13,16]
listy1 = [15,50,80,40,70,50]
p.line(listx1, listy1, line_width=4, line_color="red", line_alpha=0.3, line_dash=dashs, legend="男性")
listx2 = [2,6,8,11,14,16]
listy2 = [10,40,30,50,80,60]
p.line(listx2, listy2, line_width=4, legend="女性")
show(p)

from bokeh.plotting import figure, show

p = figure(width=800, height=400, title="零花錢統計")
# p.title_text_font_size = "18pt"
p.xaxis.axis_label = "X 軸"
p.yaxis.axis_label = "y 軸"
listx = [1,5,7,9,13,16]
listy = [15,50,80,40,70,50]
sizes=[10,20,30,30,20,10]
colors=["red","blue","green","pink","violet","gray"]
#sizes=25 #所有點相同大小
#colors="red" #所有點相同顏色
p.circle(listx, listy, size=sizes, color=colors, alpha=0.5)
show(p)

from bokeh.plotting import figure, show
import matplotlib.pyplot as plt #導入繪圖模塊,重命名為plt
import requests #導入網頁內容抓取包
from bs4 import BeautifulSoup as bs #導入網頁解析模塊,重命名為bs

year = [] #橫坐標列表
gdp = [] #縱坐標列表
url = "http://value500.com/M2GDP.html" #設置要在哪個網頁抓數據
content = requests.get(url) #獲取網頁內容
content.encoding=‘utf-8‘ #轉為utf-8編碼
content1=content.text #取得網頁內容的text部分
parse = bs(content1,"html.parser") #進行html解析
data1 = parse.find_all("table") #獲取所有表元素
rows = data1[19].find_all("tr") #取出包含所需數據的表(網頁第20個表)
i=0 #為了不讀取表頭數據,設置此控制變量
for row in rows:
cols = row.find_all("td") #把每一行表數據存入cols變量
if(len(cols) > 0 and i==0): #如果是第一行,則控制變量加1
i+=1
else: #如果不是第一行,則寫入繪圖列表
year.append(cols[0].text[:-2]) #取得年份數據(數據的最後兩個字符不是數據需去除)並寫入圖形的year軸
gdp.append(cols[2].text) #把gdp值存入gdp軸

p = figure(width=800, height=400, title="1990~2016年度我國GDP") #在瀏覽器生成畫圖區域
p.title_text_font_size = "20pt" #設置字體大小為20
p.xaxis.axis_label = "年度" #設置x軸標題
p.yaxis.axis_label = "GDP(億元)" #設置y軸標題
p.circle(year,gdp, size=6) # 圓點顯示,點的大小為6
show(p) #顯示圖形

吳裕雄 實戰PYTHON編程(6)