How to make a list of tuples with matplotlib?

I have a list of tuples with tuples ( minTemp, averageTemp, maxTemp). I would like to plot a line graph of each of these elements in a tuple, in the same matplotlib figure.

How can I do that?

+4
source share
1 answer

To get an array of minimum, average and maximum temperature, respectively, the zip function is good 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

You can also import these functions from the numpy and matplotlib modules, and you can change the layout (color in the example) as follows:

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

plot matplotlib documentation docstring .

+7

Source: https://habr.com/ru/post/1569221/


All Articles