⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plota.py

📁 Python.Tkinter编程实例代码多多学习
💻 PY
📖 第 1 页 / 共 2 页
字号:
	    text_height[0] = h
	    text_height[1] = max(text_height[1], h)
	else:
	    yticks = None
	text1 = [text_width[0], -text_height[1]]
	text2 = [text_width[1], -text_height[0]]
 	scale = ((self.plotarea_size[0]-text1[0]-text2[0]) / \
                 (p2[0]-p1[0]),
                 (self.plotarea_size[1]-text1[1]-text2[1]) / \
                 (p2[1]-p1[1]))
 	shift = ((-p1[0]*scale[0]) + self.plotarea_origin[0] + \
                 text1[0],
                 (-p1[1]*scale[1]) + self.plotarea_origin[1] + \
                 text1[1])
	self._drawAxes(self.canvas, xaxis, yaxis, p1, p2,
                        scale, shift, xticks, yticks)
	graphics.fitToScale(scale, shift)
	graphics.draw(self.canvas)

    def _axisInterval(self, spec, lower, upper):
	if spec is None:
	    return None
	if spec == 'minimal':
	    if lower == upper:
		return lower-0.5, upper+0.5
	    else:
		return lower, upper
	if spec == 'automatic':
	    range = upper-lower
	    if range == 0.:
		return lower-0.5, upper+0.5
	    log = math.log10(range)
	    power = math.floor(log)
	    fraction = log-power
	    if fraction <= 0.05:
		power = power-1
	    grid = 10.**power
	    lower = lower - lower % grid
	    mod = upper % grid
	    if mod != 0:
		upper = upper - mod + grid
	    return lower, upper
	if type(spec) == type(()):
	    lower, upper = spec
	    if lower <= upper:
		return lower, upper
	    else:
		return upper, lower
	raise ValueError, str(spec) + ': illegal axis specification'

    def _drawAxes(self, canvas, xaxis, yaxis,
                  bb1, bb2, scale, shift, xticks, yticks):
	dict = {'anchor': N, 'fill': 'black'}
	if self.font is not None:
	    dict['font'] = self.font
	if xaxis is not None:
	    lower, upper = xaxis
	    text = 1
	    for y, d in [(bb1[1], -3), (bb2[1], 3)]:
		p1 = (scale[0]*lower)+shift[0], (scale[1]*y)+shift[1]
		p2 = (scale[0]*upper)+shift[0], (scale[1]*y)+shift[1]
		Line(self.canvas, p1[0], p1[1], p2[0], p2[1],
                     fill = 'black', width = 1)
                if xticks:
                    for x, label in xticks:
                        p = (scale[0]*x)+shift[0], \
                            (scale[1]*y)+shift[1]
                        Line(self.canvas, p[0], p[1], p[0], p[1]+d,
                             fill = 'black', width = 1)
                        if text:
                            dict['text'] = label
                            apply(CanvasText, (self.canvas, p[0],
                                               p[1]), dict)
		text = 0

	dict['anchor'] = E
	if yaxis is not None:
	    lower, upper = yaxis
	    text = 1
	    for x, d in [(bb1[0], -3), (bb2[0], 3)]:
		p1 = (scale[0]*x)+shift[0], (scale[1]*lower)+shift[1]
		p2 = (scale[0]*x)+shift[0], (scale[1]*upper)+shift[1]
		Line(self.canvas, p1[0], p1[1], p2[0], p2[1],
                     fill = 'black', width = 1)
                if yticks:
                    for y, label in yticks:
                        p = (scale[0]*x)+shift[0], \
                            (scale[1]*y)+shift[1]
                        Line(self.canvas, p[0], p[1], p[0]-d, p[1],
                             fill = 'black', width = 1)
                        if text:
                            dict['text'] = label
                            apply(CanvasText, (self.canvas,
                                               p[0]-2, p[1]), dict)
		text = 0

    def _ticks(self, lower, upper):
	ideal = (upper-lower)/7.
	log = math.log10(ideal)
	power = math.floor(log)
	fraction = log-power
	factor = 1.
	error = fraction
	for f, lf in self._multiples:
	    e = math.fabs(fraction-lf)
	    if e < error:
		error = e
		factor = f
	grid = factor * 10.**power
        if power > 3 or power < -3:
            format = '%+7.0e'
        elif power >= 0:
            digits = max(1, int(power))
            format = '%' + `digits`+'.0f'
        else:
            digits = -int(power)
            format = '%'+`digits+2`+'.'+`digits`+'f'
	ticks = []
	t = -grid*math.floor(-lower/grid)
	while t <= upper and len(ticks) < 200:
	    ticks.append(t, format % (t,))
	    t = t + grid
	return ticks

    _multiples = [(2., math.log10(2.)), (5., math.log10(5.))]

    def _textBoundingBox(self, text):
	bg = self.canvas.cget('background')
	dict = {'anchor': NW, 'text': text, 'fill': bg}
	if self.font is not None:
	    dict['font'] = self.font
	item = apply(CanvasText, (self.canvas, 0., 0.), dict)
	bb = self.canvas.bbox(item)
	self.canvas.delete(item)
	return bb

    def replot(self):
	if self.last_drawn is not None:
	    apply(self.draw, self.last_drawn)

    def clear(self):
        self.canvas.delete('all')


if __name__ == '__main__':

    root = Tk()

    line1 = GraphLine([(0,22),(1,77),(2,129),(3,169),(4,260),
                       (5,377),(6,695),(7,661),(8,655),(9,596),
                       (10,693),(11,800)], smooth=0, color='gray')
    line1b = GraphLine([(0,122),(1,145),(2,151),(3,147),(4,22),(5,31),
                        (6,77),(7,125),(8,220),(9,550),(10,560),(11,540)],
                        smooth=0, color='green')
    line1d = GraphLine([(0,132),(1,155),(2,181),(3,397),(4,222),(5,231),
                        (6,177),(7,105),(8,80),(9,50),(10,98),(11,140)],
                        smooth=0, color='orange')

    line2 = GraphLine([(0,22),(1,77),(2,129),(3,169),(4,260),
                       (5,377),(6,695),(7,661),(8,655),(9,596),
                       (10,693),(11,800)], smooth=0, color='gray')
    line2a = GraphLine([(0,22),(1,77),(2,129),(3,169),(4,260),
                        (5,377),(6,695),(7,661),(8,655),(9,596),
                        (10,693),(11,800)], smooth=1, color='black')
    line2b = GraphLine([(0,122),(1,145),(2,151),(3,147),(4,22),(5,31),
                        (6,77),(7,125),(8,220),(9,550),(10,560),(11,540)],
                        smooth=0, color='green')
    line2c = GraphLine([(0,122),(1,145),(2,151),(3,147),(4,22),(5,31),
                        (6,77),(7,125),(8,220),(9,550),(10,560),(11,540)],
                        smooth=1, color='black')
    line2d = GraphLine([(0,132),(1,155),(2,181),(3,397),(4,222),(5,231),
                        (6,177),(7,105),(8,80),(9,50),(10,98),(11,140)],
                        smooth=0, color='orange')
    line2e = GraphLine([(0,132),(1,155),(2,181),(3,397),(4,222),(5,231),
                        (6,177),(7,105),(8,80),(9,50),(10,98),(11,140)],
                        smooth=1, color='black')

    line3 = GraphSymbols([(0,22),(1,77),(2,129),(3,169),(4,260),
                          (5,377),(6,695),(7,661),(8,655),(9,596),
                          (10,693),(11,800)], marker='circle',
                         fillcolor='red')
    line3a = GraphSymbols([(0,122),(1,145),(2,151),(3,147),(4,22),(5,31),
                           (6,77),(7,125),(8,220),(9,550),(10,560),(11,540)],
                          marker='triangle', fillcolor='green')
    line3b = GraphSymbols([(0,132),(1,155),(2,181),(3,397),(4,222),(5,231),
                        (6,177),(7,105),(8,80),(9,50),(10,98),(11,140)],
                        marker='cross', color='black')

    line4  = GraphLine([(0,22),(1,77),(2,129),(3,169),(4,260),
                        (5,377),(6,695),(7,661),(8,655),(9,596),
                        (10,693),(11,800)], smooth=1, color='black')
    line4a = GraphLine([(0,122),(1,145),(2,151),(3,147),(4,22),(5,31),
                        (6,77),(7,125),(8,220),(9,550),(10,560),(11,540)],
                        smooth=1, color='black')
    line4b = GraphLine([(0,132),(1,155),(2,181),(3,397),(4,222),(5,231),
                        (6,177),(7,105),(8,80),(9,50),(10,98),(11,140)],
                        smooth=1, color='black')
    line4c = GraphSymbols([(0,22),(1,77),(2,129),(3,169),(4,260),
                          (5,377),(6,695),(7,661),(8,655),(9,596),
                          (10,693),(11,800)], marker='circle',
                         fillcolor='red')
    line4d = GraphSymbols([(0,122),(1,145),(2,151),(3,147),(4,22),(5,31),
                           (6,77),(7,125),(8,220),(9,550),(10,560),(11,540)],
                          marker='triangle', fillcolor='green')
    line4e = GraphSymbols([(0,132),(1,155),(2,181),(3,397),(4,222),(5,231),
                        (6,177),(7,105),(8,80),(9,50),(10,98),(11,140)],
                        marker='cross', color='black')

    f1 = Frame(root)
    f2 = Frame(root)

    graphObject  = GraphObjects([line1,line1b,line1d,])
    graphObject2 = GraphObjects([line2,line2a,line2b,line2c,line2d,line2e])
    graphObject3 = GraphObjects([line3, line3a, line3b])    
    graphObject4 = GraphObjects([line4,line4a,line4b,line4c,line4d,line4e])    

    graph  = GraphBase(f1, 400, 300, relief=SUNKEN, border=2)
    graph.pack(side=LEFT, fill=BOTH, expand=YES)
    graph.draw(graphObject, 'automatic', 'automatic')

    graph2  = GraphBase(f1, 400, 300, relief=SUNKEN, border=2)
    graph2.pack(side=LEFT, fill=BOTH, expand=YES)
    graph2.draw(graphObject2, 'automatic', 'automatic')

    graph3  = GraphBase(f2, 400, 300, relief=SUNKEN, border=2)
    graph3.pack(side=LEFT, fill=BOTH, expand=YES)
    graph3.draw(graphObject3, 'automatic', 'automatic')

    graph4  = GraphBase(f2, 400, 300, relief=SUNKEN, border=2)
    graph4.pack(side=LEFT, fill=BOTH, expand=YES)
    graph4.draw(graphObject4, 'automatic', 'automatic')

    f1.pack()
    f2.pack()
    
    Button(root, text='Clear',  command=graph.clear).pack(side=LEFT)
    Button(root, text='Redraw', command=graph.replot).pack(side=LEFT)
    Button(root, text='Quit',   command=root.quit).pack(side=RIGHT)

    root.mainloop()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -