如何在python动画中添加图例标签

时间:2018-08-24 07:23:01

标签: python animation matplotlib plot

我正在使用Matplotlib / Python从数据库中绘制数据(计数)。我不知道如何制作动画图例。我希望图例显示行的当前值。

代码是:

def db_count():
    ### Connect to db and run query ...
    return count

x_data, y_data = [], []
figure = plt.figure()
count = db_count()
line, = plt.plot_date(x_data, y_data, '-', label=count)

def update(frame):
    x_data.append(datetime.now())
    count = db_count()
    y_data.append(count)
    line.set_data(x_data, y_data)
    figure.gca().autoscale_view()
    figure.gca().relim()
    return line,

animation = FuncAnimation(figure, update, interval=60000)

plt.xlabel('Time')
plt.ylabel('Counts')
plt.title('Total counts')
plt.legend(bbox_to_anchor=(1.05, 0.05))
plt.gcf().autofmt_xdate()
plt.xticks(rotation=45)
plt.show()

谢谢

1 个答案:

答案 0 :(得分:3)

您需要使用@ConfigurationProperties(prefix = "patterns") public class AppProperties { private Map<String, String> passwordPatterns = new HashMap<>(); public Map<String, String> getPasswordPatterns() { return passwordPatterns; } } @SpringBootApplication @EnableConfigurationProperties(AppProperties.class) public class DemoApplication implements CommandLineRunner { private static final Logger LOG = LoggerFactory .getLogger(DemoApplication.class); @Autowired private AppProperties appProperties; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) { LOG.info("pattern = {}", appProperties.getPasswordPatterns().get("pattern1")); } } 函数中设置行的标签,然后调用patterns: password-patterns: pattern1: "test"

update