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

标签 python list matplotlib tuples

我有一个元组列表,这些元组是 (minTemp, averageTemp, maxTemp)。 我想在同一个 matplotlib 图上绘制元组中每个元素的折线图。

如何做到这一点?

最佳答案

要分别获取最低、平均和最高温度的数组,zip 函数很好 as you can see here .

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")

enter image description here

您也可以从 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")

enter image description here

您可以在 matplotlib documentation 上找到 plot 函数的关键字参数或者在函数的文档字符串中。

关于python - 如何使用 matplotlib 绘制元组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27679354/

相关文章:

python - 如何获取函数输出的多个值

python - 如何使用 Nuitka 从 Python 获取已编译二进制文件的当前路径?

python - 在条件列表理解中使用 or 语句来过滤数据框中的列

jquery - 获取接下来的 6 个结果

python - YTick 在 Matplotlib 中重叠

python - 如何计算文本中不包括空格和数字的字母频率?

list - 字典列表中值的总和

python - Seaborn Lineplot 模块对象没有属性 'Lineplot'

python - 如何绘制一列平均值的直方图,而 bin 由 Pandas 中的另一列定义

python - 提取字典中的特定键值