繁体   English   中英

如何使用matplotlib绘制元组列表?

[英]How do I plot a list of tuples with matplotlib?

我有一个元组幸福(元组列表minTempaverageTempmaxTemp )。 我想在同一个matplotlib图上绘制元组中每个元素的折线图。

如何才能做到这一点?

为了分别获得最小,平均和最高温度的数组,拉链功能很好,你可以在这里看到

from pylab import plot, title, xlabel, ylabel, savefig, legend, array

values = [(3, 4, 5), (7, 8, 9), (2, 3, 4)]
days = array([24, 25, 26])

for temp in zip(*values):
    plot(days, array(temp))
title('Temperature at december')
xlabel('Days of december')
ylabel('Temperature')
legend(['min', 'avg', 'max'], loc='lower center')
savefig("temperature_at_christmas.pdf")

在此输入图像描述

您也可以从numpy和matplotlib模块导入这些函数,您可以更改布局(示例中的颜色),如下所示:

from matplotlib.pyplot import plot, title, xlabel, ylabel, savefig, legend
from numpy import array

values = [(3, 4, 5), (5, 8, 9), (2, 3, 5), (3, 5, 6)]
days = array([24, 25, 26, 27])

min_temp, avg_temp, max_temp = zip(*values)
temperature_with_colors_and_labels = (
    (min_temp, 'green', 'min'),
    (avg_temp, 'grey', 'avg'),
    (max_temp, 'orange', 'max'),
)

for temp, color, label in temperature_with_colors_and_labels:
    plot(days, array(temp), color=color, label=label)
title('Temperature at december (last decade)')
xlabel('Days of december')
ylabel('Temperature (Celsius)')
legend()
savefig("temperature_at_christmas.png")

在此输入图像描述

您可以在matplotlib文档或函数的docstring中找到plot函数的关键字参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM