Reputation: 1062
I have a list of tuples with the tuples being (minTemp
, averageTemp
, maxTemp
).
I would like to plot a line graph of each of these elements in the tuple, on the same matplotlib figure.
How can this be done?
Upvotes: 7
Views: 24086
Reputation: 1882
To get the array of the min, average and max temperature respectively, the zip functionis 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")
You can import these functions from numpy and matplotlib modules as well, and you can alter the layout (the 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")
You can find the keyword arguments of the plot function on the matplotlib documentation or in the docstring of the function.
Upvotes: 9