畫圖並保存Python
❶ 怎樣用python畫圖
思路:
1. 確定好需要畫的圖。
2. 創建一個畫布,用來畫你需要的圖。
(1)畫布大小,可以使用默認大小,也可以自定義畫布大小。
(2)畫布背景色bgcolor()。
(3)確定起點位置。
3. 畫筆的設置。
(1)畫筆的大小,顏色。
(2)畫筆運行屬性。
二、定義畫布參數
importturtleast
t.screensize(width=None,heigh=None,bg=None)#以像素為單位,參數分別為畫布的寬,高,背景色
t.screensize()#返回默認大小(400,300)
t.Screen()#也是表示默認畫布大小,注意S大寫
t.setup(w=0.5,h=0.75,startx=None,starty=None)#w,h為整數是表示像素,為小數時表示占據電腦屏幕的比例
#startx,starty坐標表示矩形窗口左上角頂點的位置,默認為屏幕中心位置
三、定義畫筆
1、畫筆的狀態
在畫布上默認有一個坐標原點為畫布中心的坐標軸,坐標原點上有一隻面朝x軸正方向小烏龜。這里我們描述小烏龜時使用了兩個詞語:坐標原點(位置),面朝x軸正方向(方向),turtle繪圖中,就是使用位置方向描述小烏龜(畫筆)的狀態。
2、畫筆的屬性
畫筆(畫筆的屬性,顏色、畫線的寬度)
(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]整數,數字越大越快。
相關推薦:《Python入門教程》
3、繪圖命令
操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:一種為運動命令,一種為畫筆控制命令,還有一種是全局控制命令。
(1)畫筆運動命令:
(3)全局控制命令:
四、命令詳解
turtle.circle(radius,extent=None,steps=None)
描述:以給定半徑畫圓
參數:
radius(半徑);半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓;
extent(弧度) (optional);
steps (optional) (做半徑為radius的圓的內切正多邊形,多邊形邊數為steps);
舉例:
circle(50)#整圓;
circle(50,steps=3)#三角形;
circle(120,180)#半圓
五、繪圖舉例
(1)菱形太陽花
importturtleast#turtle庫是python的內部庫,直接import使用即可
defdraw_diamond(turt):
foriinrange(1,3):
turt.forward(100)#向前走100步
希望我的回答對你有幫助
❷ python用turtle作圖能保存嗎
用turtle畫完圖後使用如下代碼保存
ts = turtle.getscreen()
ts.getcanvas().postscript(file="pic.eps")
然後可以再用ps等軟體來轉換為常用的jpg等格式
❸ python中tkinter怎麼將畫布保存為圖像
可以通過Canvas的postscript方法,將畫布內容封裝成 PostScript 格式,然後再通過其它圖片操作轉為你需要的圖片格式
❹ python畫圖
matplotlib就可以,看他示例文件里動畫那個文件夾。
❺ Python畫圖
python作為一門膠水語言,很多功能是靠調用其他語言的庫來實現的。
像你這種需求,最好是用C/C++對畫圖操作進行封裝,然後Python調用動態鏈接庫
❻ Python語言畫圖
1)首先Python畫圖與WING IDE無關,最簡單的是使用Tkinter畫圖
2)畫出單詞有很多方法,最笨的是用劃線方式一筆一筆的畫。其次是直接輸出文本,但意義不大。另外一種方法是調用圖片,你可以在圖片上任意畫好東西後顯示出來。
3)代碼示例:(這個例子就畫了個簡單的字母P)
from Tkinter import *
root=Tk()
root.title('Drawing Example')
canvas=Canvas(root,width=200,height=160,bg='white')
canvas.create_line(10,10,100,70)
canvas.create_line(10,10,40,10)
canvas.create_line(40,10,40,40)
canvas.create_line(10,40,40,40)
canvas.pack()
root.mainloop()
❼ Python 畫圖存儲(savefig)
你可以安裝python的第三方應用chartdirector,如下面用python代碼生成多個曲線的png圖形,並可以自定義layout.
#!/usr/bin/python
from pychartdir import *
# The data for the line chart
data0 = [42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52, 37, 34, 51, 56, 56, 60, 70,
76, 63, 67, 75, 64, 51]
data1 = [50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50, 64, 60, 67, 67, 58, 59, 73,
77, 84, 82, 80, 84, 98]
data2 = [36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24, 28, 15, 21, 26, 46, 42, 48,
45, 43, 52, 64, 60, 70]
# The labels for the line chart
labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"]
# Create an XYChart object of size 600 x 300 pixels, with a light blue (EEEEFF)
# background, black border, 1 pxiel 3D border effect and rounded corners
c = XYChart(600, 300, 0xeeeeff, 0x000000, 1)
c.setRoundedFrame()
# Set the plotarea at (55, 58) and of size 520 x 195 pixels, with white background.
# Turn on both horizontal and vertical grid lines with light grey color (0xcccccc)
c.setPlotArea(55, 58, 520, 195, 0xffffff, -1, -1, 0xcccccc, 0xcccccc)
# Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 9 pts
# Arial Bold font. Set the background and border color to Transparent.
c.addLegend(50, 30, 0, "arialbd.ttf", 9).setBackground(Transparent)
# Add a title box to the chart using 15 pts Times Bold Italic font, on a light blue
# (CCCCFF) background with glass effect. white (0xffffff) on a dark red (0x800000)
# background, with a 1 pixel 3D border.
c.addTitle("Application Server Throughput", "timesbi.ttf", 15).setBackground(
0xccccff, 0x000000, glassEffect())
# Add a title to the y axis
c.yAxis().setTitle("MBytes per hour")
# Set the labels on the x axis.
c.xAxis().setLabels(labels)
# Display 1 out of 3 labels on the x-axis.
c.xAxis().setLabelStep(3)
# Add a title to the x axis
c.xAxis().setTitle("Jun 12, 2006")
# Add a line layer to the chart
layer = c.addLineLayer2()
# Set the default line width to 2 pixels
layer.setLineWidth(2)
# Add the three data sets to the line layer. For demo purpose, we use a dash line
# color for the last line
layer.addDataSet(data0, 0xff0000, "Server #1")
layer.addDataSet(data1, 0x008800, "Server #2")
layer.addDataSet(data2, c.dashLineColor(0x3333ff, DashLine), "Server #3")
# Output the chart
c.makeChart("multiline.png")
❽ 怎麼用python繪圖
你可以使用numpy和matplotlab這兩個庫來實現的你功能。
你的圖可以參考:
http://matplotlib.org/examples/pylab_examples/histogram_percent_demo.html
importmatplotlib
fromnumpy.randomimportrandn
importmatplotlib.pyplotasplt
frommatplotlib.tickerimportFuncFormatter
defto_percent(y,position):
#Ignorethepassedinposition.
#ticklocations.
s=str(100*y)
#
ifmatplotlib.rcParams['text.usetex']==True:
returns+r'$\%$'
else:
returns+'%'
x=randn(5000)
#Makeanormedhistogram.It'llbemultipliedby100later.
plt.hist(x,bins=50,normed=True)
#_percent.Thismultipliesallthe
#defaultlabelsby100,makingthemallpercentages
formatter=FuncFormatter(to_percent)
#Settheformatter
plt.gca().yaxis.set_major_formatter(formatter)
plt.show()
最主要的就是x軸和y軸的處理,我按照對數算了一下你提供的數據,好像和這個圖效果不一樣。
如果解決了您的問題請採納!
如果未解決請繼續追問
❾ 關於Python的畫圖的東西。
畫正方形
importturtle
importtime
#定義繪制時畫筆的顏色
turtle.color("purple")
#定義繪制時畫筆的線條的寬度
turtle.size(5)
#定義繪圖的速度
turtle.speed(10)
#以0,0為起點進行繪制
turtle.goto(0,0)
#繪出正方形的四條邊
foriinrange(4):
turtle.forward(100)
turtle.right(90)
#畫筆移動到點(-150,-120)時不繪圖
turtle.up()
turtle.goto(-150,-120)
#再次定義畫筆顏色
turtle.color("red")
#在(-150,-120)點上列印"Done"
turtle.write("Done")
time.sleep(3)
❿ 為什麼用python保存圖片為0K
保存的方式有兩種。
1、使用matplotlib畫圖保存方法:首先,import頭文件如下:import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.pyplot import plot,savefig畫圖:fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') #im是要顯示的圖像。保存圖片:savefig("D:/1.jpg")展示圖片:plt.show()2、turtle生成圖片保存代碼示例如下:from Tkinter import * from turtle import * import turtle forward(100) ts = turtle.getscreen() ts.getcanvas().postscript(file="ck.eps") #.eps文件即postscript腳本以上就是python畫圖後如何保存的詳細內容。