how to insert value into list of matplotlib since

2020-07-27 23:36发布

问题:

i have this script in py :

show_grid = True

with plt.style.context(('seaborn-darkgrid')):
    plt.plot([?, ?, ?, ?, ?, ?, ?, ?], [?, ?, ?, ?, ?, ?, ?, ?]) # the lists
    plt.ylabel('Temperatures °C')
    plt.xlabel('Dates')
    plt.title('Relevé des températures du mois de FEVRIER 2020')
    plt.grid(show_grid)
    plt.show()

and i would like to add values from an other file such as txt or json :

{'dicolist': ['22/02/2020', '+22.5']}

How should i proceed plz, and which format is better ?

dicolist=dict_keys(['02/02/2020']):dict_values(['+23.0'])

or

[
{key,value}
{key,value}
{key,value}
{key,value}
]

Help me !!??? :D

回答1:

I still have not really clear if you have already your dictionary or you have to decide the format of it. If I understood you correctly you can make like the following example:

import matplotlib.pyplot as plt
from datetime import datetime

dicolist = {'22/02/2020': '+22.5', '23/02/2020': '+18'}
list_a = ['19/02/2020','20/02/2020','21/02/2020'] # your lists
list_b = ['+20.5','+21.5', '+19'] # your lists

for key, value in dicolist.items():
    list_a.append(key)
    list_b.append(value)

list_a = [datetime.strptime(d, '%d/%m/%Y') for d in list_a]
list_b = [float(d) for d in list_b]

show_grid = True
with plt.style.context(('seaborn-darkgrid')):
    plt.plot(list_a,list_b) # the lists
    plt.ylabel('Temperatures °C')
    plt.xlabel('Dates')
    plt.title('Relevé des températures du mois de FEVRIER 2020')
    plt.xticks(rotation=45)
    plt.grid(show_grid)
    plt.show()