python画图颜色填充_python画图的两种方法
python如何畫圖?這里給大家介紹兩款python繪圖的庫(kù):turtle和Matplotlib。
相關(guān)推薦:《python視頻》
1 安裝turtle
Python2安裝命令:pip install turtule
Python3安裝命令:pip3 install turtle
2 基礎(chǔ)概念
2.1 畫布(canvas)
畫布就是turtle為我們展開用于繪圖區(qū)域, 我們可以設(shè)置它的大小和初始位置。
常用的畫布方法有兩個(gè):screensize()和setup()。
(1)turtle.screensize(canvwidth=None, canvheight=None, bg=None)
參數(shù)分別為畫布的寬(單位像素), 高, 背景顏色
如:
turtle.screensize(800, 600, "green")
turtle.screensize() #返回默認(rèn)大小(400, 300)
(2)turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
參數(shù):
width, height: 輸入寬和高為整數(shù)時(shí), 表示像素; 為小數(shù)時(shí), 表示占據(jù)電腦屏幕的比例
(startx, starty): 這一坐標(biāo)表示 矩形窗口左上角頂點(diǎn)的位置, 如果為空,則窗口位于屏幕中心
如:turtle.setup(width=0.6, height=0.6)
turtle.setup(width=800, height=800, startx=100, starty=100)
2.2 畫筆
在畫布上,默認(rèn)有一個(gè)坐標(biāo)原點(diǎn)為畫布中心的坐標(biāo)軸, 坐標(biāo)原點(diǎn)上有一只面朝x軸正方向小烏龜。
這里我們描述小烏龜時(shí)使用了兩個(gè)詞語(yǔ):標(biāo)原點(diǎn)(位置),面朝x軸正方向(方向),turtle繪圖中, 就是使用位置方向描述小烏龜(畫筆)的狀態(tài)
(1)畫筆的屬性
畫筆有顏色、畫線的寬度等屬性。
1) turtle.pensize() :設(shè)置畫筆的寬度;
2) turtle.pencolor() :沒有參數(shù)傳入返回當(dāng)前畫筆顏色;傳入?yún)?shù)設(shè)置畫筆顏色,可以是字符串如"green", "red",也可以是RGB 3元組。>>> pencolor('brown')
>>> tup = (0.2, 0.8, 0.55)
>>> pencolor(tup)
>>> pencolor()
'#33cc8c'
3) turtle.speed(speed) :設(shè)置畫筆移動(dòng)速度,畫筆繪制的速度范圍[0,10]整數(shù), 數(shù)字越大越快
(2)繪圖命令
操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:運(yùn)動(dòng)命令,畫筆控制命令和全局控制命令
畫筆運(yùn)動(dòng)命令:
命令 說明
turtle.forward(distance) 向當(dāng)前畫筆方向移動(dòng)distance像素長(zhǎng)
turtle.backward(distance) 向當(dāng)前畫筆相反方向移動(dòng)distance像素長(zhǎng)度
turtle.right(degree) 順時(shí)針移動(dòng)degree°
turtle.left(degree) 逆時(shí)針移動(dòng)degree°
turtle.pendown() 移動(dòng)時(shí)繪制圖形,缺省時(shí)也為繪制
turtle.goto(x,y) 將畫筆移動(dòng)到坐標(biāo)為x,y的位置
turtle.penup() 移動(dòng)時(shí)不繪制圖形,提起筆,用于另起一個(gè)地方繪制時(shí)用
turtle.speed(speed) 畫筆繪制的速度范圍[0,10]整數(shù)
turtle.circle() 畫圓,半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓
畫筆控制命令:
命令 說明
turtle.pensize(width) 繪制圖形時(shí)的寬度
turtle.pencolor() 畫筆顏色
turtle.fillcolor(colorstring) 繪制圖形的填充顏色
turtle.color(color1, color2) 同時(shí)設(shè)置pencolor=color1, fillcolor=color2
turtle.filling() 返回當(dāng)前是否在填充狀態(tài)
turtle.begin_fill() 準(zhǔn)備開始填充圖形
turtle.end_fill() 填充完成;
turtle.hideturtle() 隱藏箭頭顯示;
turtle.showturtle() 與hideturtle()函數(shù)對(duì)應(yīng)
全局控制命令:
命令 說明
turtle.clear() 清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)改變
turtle.reset() 清空窗口,重置turtle狀態(tài)為起始狀態(tài)
turtle.undo() 撤銷上一個(gè)turtle動(dòng)作
turtle.isvisible() 返回當(dāng)前turtle是否可見
stamp() 復(fù)制當(dāng)前圖形
turtle.write(s[,font=("font-name",font_size,"font_type")])寫文本,s為文本內(nèi)容,font是字體的參數(shù),里面分別為字體名稱,大小和類型;font為可選項(xiàng), font的參數(shù)也是可選項(xiàng)。
例子:import turtle
def drawSnake(rad, angle, len, neckrad):
for _ in range(len):
turtle.circle(rad, angle)
turtle.circle(-rad, angle)
turtle.circle(rad, angle/2)
turtle.forward(rad/2) # 直線前進(jìn)
turtle.circle(neckrad, 180)
turtle.forward(rad/4)
if __name__ == "__main__":
turtle.setup(1500, 1400, 0, 0)
turtle.pensize(30) # 畫筆尺寸
turtle.pencolor("green")
turtle.seth(-40) # 前進(jìn)的方向
drawSnake(70, 80, 2, 15)
Matpliotlib
前提
linux ubuntu 下需安裝下面三個(gè)包:
Numpy, Scipy,Matplotlib
分別輸入下面的代碼進(jìn)行安裝:pip install numpy
pip install scipy
sudo apt-get install python-matplotlib
測(cè)試是否安裝成功python
>>> import pylab
如果沒有報(bào)錯(cuò)則安裝成功
開始畫圖
1. 畫最簡(jiǎn)單的直線圖
代碼如下:import numpy as np
import matplotlib.pyplot as plt
x=[0,1]
y=[0,1]
plt.figure()
plt.plot(x,y)
plt.savefig("easyplot.jpg")
結(jié)果如下:
代碼解釋:#x軸,y軸
x=[0,1]
y=[0,1]
#創(chuàng)建繪圖對(duì)象
plt.figure()
#在當(dāng)前繪圖對(duì)象進(jìn)行繪圖(兩個(gè)參數(shù)是x,y軸的數(shù)據(jù))
plt.plot(x,y)
#保存圖象
plt.savefig("easyplot.jpg")
以上就是python畫圖的兩種方法的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注Gxl網(wǎng)其它相關(guān)文章!
本條技術(shù)文章來源于互聯(lián)網(wǎng),如果無意侵犯您的權(quán)益請(qǐng)點(diǎn)擊此處反饋版權(quán)投訴
本文系統(tǒng)來源:php中文網(wǎng)
總結(jié)
以上是生活随笔為你收集整理的python画图颜色填充_python画图的两种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue实现时间转换功能(年月日时分秒)
- 下一篇: SPSS(基础篇07)--随机数生成器