In the following graph I have not found how to separate the values of the same referent and graph them by joining them by a line.
I explain better here:
I have my following input:
Fecha,Pais,count
"20/05/2017",Brazil,1
"20/05/2017",China,821
"20/05/2017",Czechia,31
"20/05/2017",France,1
"20/05/2017","Republic of Korea",1
"21/05/2017",Argentina,5
"21/05/2017",Australia,2
"21/05/2017",China,3043
"21/05/2017",Denmark,1
"21/05/2017",Egypt,1
...
..
.
I already have the array imported, converting all the values, both date and String and integer.
DatetimeIndex(['2017-05-20', '2017-05-20', '2017-05-20', '2017-05-20',
'2017-05-20', '2017-05-21', '2017-05-21', '2017-05-21',
'2017-05-21', '2017-05-21', '2017-05-21', '2017-05-21',
'2017-05-21', '2017-05-21', '2017-05-21', '2017-05-21',
'2017-05-21', '2017-05-21', '2017-05-21', '2017-05-21',
'2017-05-22', '2017-05-22', '2017-05-22', '2017-05-22',
'2017-05-22', '2017-05-22', '2017-05-22', '2017-05-22',
'2017-05-22', '2017-05-22', '2017-05-22', '2017-05-22',
'2017-05-22', '2017-05-22', '2017-05-22', '2017-05-22'],
dtype='datetime64[ns]', freq=None)
['Brazil' 'China' 'Czechia' 'France' 'Republic of Korea' 'Argentina'
'Australia' 'China' 'Denmark' 'Egypt' 'France' 'Hungary' 'Netherlands'
'Oman' 'Republic of Korea' 'Russia' 'Slovak Republic' 'Taiwan' 'Ukraine'
'United Arab Emirates' 'Argentina' 'Brazil' 'China' 'Czechia' 'Ecuador'
'France' 'Germany' 'India' 'Latvia' 'Liberia' 'Pakistan' 'Peru'
'Republic of Korea' 'Russia' 'Taiwan' 'Ukraine']
['1' '821' '31' '1' '1' '5' '2' '3043' '1' '1' '1' '1' '1' '1' '1' '1' '1'
'3' '48' '1' '2' '1' '3759' '79' '2' '1' '3' '1' '192' '1' '1' '1' '1' '2'
'1' '1']
I already have the graph too:
What I have not achieved is joining the lines of each country, what I am achieving is only plotting each value, but I am not plotting based on the country string.
here my code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, DayLocator, AutoDateLocator, AutoDateFormatter
import datetime
locator = DayLocator()
formatter = AutoDateFormatter(locator)
date, country, count = np.loadtxt("72hcountcountry.csv",
delimiter=',',
unpack=True,
dtype='string',
skiprows=1)
date = np.char.replace (date, '"', '')
country = np.char.replace (country, '"', '')
date2 = pd.to_datetime(date)
print date2
print country
print count
fig, ax = plt.subplots()
ax.plot_date(date2, count)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.autoscale_view()
ax.grid(True)
fig.autofmt_xdate()
plt.show()
How do I draw a line for each country that has data on the dates?
[Update]
Doubt, when you extract the csv, with parse_dates, how can I change the date format?
In the csv it comes native this way:
"05/06/2017","Republic of Korea",3
I don't know, but curious, when we were in May 2x, I got the month and day fine, but when I change to June (I imagine that from 1 to 12, as in this example), it is changing the day for the month, to which I was converting by forcing the format with:
pd.to_datetime(date, format="%d/%m/%Y")
Can I do it directly when reading from the csv? or would you change it later in the arrangement?
Thank you
Since you use Pandas exploit it, it is very simple using
groupby
to group by countries, once this is done, it is enough to graph each group:We obtain: