2017-06-06 5 views
1

文字列による値をタイムチャートにプロットする方法を見つけることができません。日付によるプロット値は文字列で示されます

ここは私のデータです。

(CSVからの)入力:

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日付、文字列とよく解析された整数値を持つCSVデータらをインポートした:実際に

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が持っていますプロット:

see plot figure

しかし、私は値を参加can't同じ国では、データが含まれている日付でそれぞれの履歴をプロットします。ここで

は私のコードです:

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, format="%d/%m/%Y") 

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() 

はどうすれば各国がデータに合わせて、それぞれの日付をプロット分けることができますか?

+0

また、最後にあなたの実際の質問に関連する質問のタイトルをもう1つ変更することをお勧めしますか? 「列の1つに(文字列)値で区切られたファイルからのプロットデータ」と同様のものが、IMHOのほうが良いかもしれません。 –

+0

ありがとうございます@Pablo –

答えて

0

私はあなたがやろうとしているものを正しく理解していれば、あなたはパンダのライブラリを使用して、それを達成することができますが:あなたは(それが正しく日付フォーマットを処理する必要があります)DataFrameに入力されたデータを読み、その後の使用をする必要がありますgroupby方法(文献hereを参照)。

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

infile = "foo.csv" 

# Read in the file to a Pandas 'DataFrame' 
df = pd.read_csv(infile) 

# Group the different entries by the content of the 
# Country/Pais column 
dfg = df.groupby('Pais') 

fig, ax = plt.subplots() 

# Loop over group names (country names), 
# and plot each one separately (assigning the appropriate label) 
for country in dfg.groups.keys(): 
    thisdf = dfg.get_group(country) 
    ax.plot_date(thisdf['Fecha'], thisdf['count'], 'o-', label=country) 


ax.legend() 
fig.autofmt_xdate() 

plt.show() 

そして、ここでは最小限のために(結果です:

あなたのcsvファイルの場合の簡単な例は、(おそらく、さらにダニの形式を変更するなどしたいと思う)はこちらあなたの入力ファイルのバージョン): example plot

関連する問題