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