パンダのpyplotにおける間違ったx値は整数にインデックスを変換してデータフレーム。どのように私は、正しい値を得ることができますか?

smcintyre247:

解決しよう:私は長年のようにインデックスと1980年から2013年にカナダに移住ハイチ人の数に関するデータが含まれているパンダのデータフレーム(DF)を持っているので、

>>>len(df)
34
>>>df.index
Index(['1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988',
   '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997',
   '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006',
   '2007', '2008', '2009', '2010', '2011', '2012', '2013'],
  dtype='object')

私が書いたので、私は、簡単にプロットするために整数にインデックスを変換したいです

>>>df.index = df.index.map(int)
Int64Index([1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990,
        1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
        2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
        2013],
       dtype='int64')

変換が正常に動作するように見えたが、私は自分のデータをプロットしてみたときのxの値が間違っています。

>>>df.plot()
...plt.title("Immigration from Haiti")
...plt.xlabel("Year")
...plt.ylabel("# of Immigrants")
...plt.text(2000, 6000, '2010 Earthquake-->')
...plt.show()

ここでは、画像の説明を入力します。

私は、これらのx値から来ているか見当もつかないが、彼らは、私が使用することを意図したインデックス値ではありません。どのように私は正しいのxの値を使用して、このプロットを生成しますか?

私は、インデックス値に文字列のままにして(つまり、プロットで追加されたテキストのインデックスの位置を使用して文字列と使用plt.text(20、6000に変換をスキップすることができることを知って、「2010地震 - >」) )が、私はむしろ、実際の年間使用すると思います。あなたはどのように正しくこれを行う方法を教えてくださいすることができ、私は間違って何をやっていますか?


ここではそれを望んでいた人のための完全なコードです。それは自動的に目盛りをオフセットなぜ私はまだ興味があります。これは、データの可視化にEDXコースのためであるとの.xlsxデータは安全です。私は上記のDF呼んでいたことをデータフレームは、実際には、このコードでは「ハイチ」と命名されます。

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

#import data
df_can = pd.read_excel(
        "https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/Canada.xlsx",
        sheetname="Canada by Citizenship", skiprows=range(20), skip_footer=2)

#pre-process data a bit
df_can.columns.tolist()
df_can.index.tolist()
df_can.drop(["AREA", "REG", "DEV", "Type", "Coverage"], axis=1, inplace=True)
df_can.rename(columns={'OdName':'Country', 'AreaName':'Continent', 'RegName':'Region'}, inplace=True) #rename some columns to be more intuitive
df_can["Total"] = df_can.sum(axis=1) #add total # of immigrants column
df_can.set_index("Country", inplace=True) #change index from number to Country
df_can.columns = list(map(str,df_can.columns)) #convert years to string to avoid confusion
years = list(map(str,range(1980,2014))) #useful for plotting later

#line plot with mpl
mpl.style.use('ggplot')
haiti = df_can.loc['Haiti',years]
haiti.index = haiti.index.map(int)

haiti.plot(kind="line",figsize=(14,8))
plt.title('Immigration Trend of Top 5 Countries')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')
plt.text(2005, 6000, '2010 Earthquake---------->')
plt.show()
Sheldore:

あなたはとても私たちだけ動作するコードを提供することができなかった、それが動作するかどうかをテストすることができます:あなたは、次のようにオフセットをオフにすることができます。これは詳細に説明されていますここに

ウェイ1

fig, ax = plt.subplots()

df.plot(ax=ax)
ax.ticklabel_format(useOffset=False)

ウェイ2

 ax = df.plot()
 ax.ticklabel_format(useOffset=False)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=14944&siteId=1