用标记作为头

时间:2016-09-23 14:29:42

标签: python matplotlib plot

我有以下代码生成绘制圆圈的动画。

from math import cos, sin
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def update_plot(num, x, y, line):
    line.set_data(x[:num], y[:num])
    line.axes.axis([-1.5, 1.5, -1.5, 1.5])
    return line

def plot_circle():
    x = []
    y = []
    for i in range(100):
        x.append(cos(i/10.0))
        y.append(sin(i/10.0))

    fig, ax = plt.subplots()
    line, = ax.plot(x, y, color = "k")
    ani = animation.FuncAnimation(fig, update_plot, len(x), fargs=[x, y, line], interval = 1, blit = False)
    plt.show()

plot_circle()

线条比整圈更长,因此当线条重叠时仍能看到图纸,我想要一个标记来显示正在绘制的内容。我尝试在更新调用中添加散点图,例如

scat = plt.scatter(0, 0)
ani = animation.FuncAnimation(fig, update_plot, len(x), fargs=[x, y, line, scat], interval = 1, blit = False)

并尝试使用x[num]中的y[num]update_plot更新散点图的位置,但未成功。我怎样才能达到这个效果?

2 个答案:

答案 0 :(得分:1)

您需要在scat中返回update_plot()

这是另一种方法,用markevery参数绘制线:

line, = ax.plot(x, y, "-o", color="k", markevery=100000)

反转积分顺序:

line.set_data(x[:num][::-1], y[:num][::-1])

例如:

import numpy as np
import pylab as pl

t = np.linspace(0, 2, 100)
x = np.cos(t)
y = np.sin(t)

pl.plot(x[::-1], y[::-1], "-o", markevery=10000)

输出:

enter image description here

答案 1 :(得分:0)

我最终找到了一种将散点图添加到同一动画中的方法。关键是使用<?php $connection = mysql_connect("localhost", "root", ""); $db = mysql_select_db("db_new", $connection); echo "Roll No:-$rollno\n"; echo "Name is:-$name\n"; echo "Division:-$division\n"; echo "Std:-$std\n"; echo "Gender:-$gender\n"; echo "Subject:-$subject\n"; $query=mysql_query("SELECT * FROM student_info where id=17"); $row= mysql_fetch_row($query); $rollno = $row[1]; $name = $row[2]; $division= $row[3]; $std = $row[4]; $gender = $row[5]; $subject=mysql_query("select * from student_info where id=17"); while($fetch=mysql_fetch_object($subject)) { $r=$fetch->subject; $i=explode(",",$r); echo $i[0]."</br>"; echo $i[1]."</br>"; echo $i[2]."</br>"; } mysql_close($connection); ?> <!DOCTYPE html> <html> <head> <title>Edit Form</title> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="jquery%20validate/jquery.validate.min.js"></script> <script src="jquery%20validate/additional-methods.min.js"></script> <script src="form.js"></script> </head> <body> <form id="editform" method="GET"> <div> <h1>Form</h1> <label><b>Roll No:</b></label> <input id="rollno" name="rollno" type="text" value="<?php echo $rollno ?>"> <br><br> <label><b>Name:</b></label> <input id="name" name="name" type="text" value="<?php echo $name ?>"> <br><br> <label><b>Division:</b></label> <select id="division"> <option value=" " <?php if (!empty($division) && $division == '' ) echo 'selected = "selected"'; ?>>--select--</option> <option value="A" <?php if (!empty($division) && $division == 'A') echo 'selected = "selected"'; ?>>A</option> <option value="B" <?php if (!empty($division) && $division == 'B') echo 'selected = "selected"'; ?>>B</option> <option value="C" <?php if (!empty($division) && $division == 'C') echo 'selected = "selected"'; ?>>C</option> <option value="D" <?php if (!empty($division) && $division == 'D') echo 'selected = "selected"'; ?>>D</option> </select><br><br> <label><b>Std:</b></label> <input id="std" name="std" type="text" value="<?php echo $std ?>"><br><br> <label><b>Gender:</b></label> <input type="radio" name="gender" value="male" <?php if($gender=='male'){ echo "checked=checked";}?>>Male <input type="radio" name="gender" value="female" <?php if($gender=='female'){ echo "checked=checked";}?>>Female<br><br> <label><b>Subject:</b></label><br> <input type="checkbox" name="box[]" class="subject" value="maths" <?php echo "checked";?>>maths<br> <input type="checkbox" name="box[]" class="subject" value="science" <?php echo "checked";?>>science<br> <input type="checkbox" name="box[]" class="subject" value="english" <?php echo "checked";?>>english<br> <input id="submit" type="submit" value="submit"> </div> </form> </body> </html> 来设置数据。需要进行的更改如下:

scat.set_offsets