簡體   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