2016-07-23 29 views
0

我的目標是在GUI TKinter中使用來自Worldbank API的數據創建圖表(帶有正確的xy標籤和圖例)。日期和圖例未在MatplotLib中正確顯示

我一直在處理諸如x標籤顯示數字而不是年份等問題,並且圖例沒有出現。 enter image description here

有沒有人有這些解決方案?

下面是代碼:

from tkinter import * 

from numpy import arange, sin, pi 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 

from matplotlib.figure import Figure 

import wbdata 
import pandas 
import datetime 

class App(Tk): 
    def __init__(self): 
     Tk.__init__(self) 
     fig_population = Figure(figsize = (7.5, 4.5), dpi = 100) 
     addsubplot_population = fig_population.add_subplot(111) 

     period_population = (datetime.datetime(2010, 1, 1), datetime.datetime(2016, 7, 23)) 
     countries_population = ["USA","GBR"] 
     indicators_population = {'SP.POP.TOTL':'population'} 
     df_population = wbdata.get_dataframe(indicators_population, country = countries_population, data_date = period_population) 
     dfu_population = df_population.unstack(level = 0) 
     x_population = dfu_population.index 
     y_population = dfu_population.population 
     addsubplot_population.plot(x_population, y_population) 

     addsubplot_population.legend(loc = 'best') 
     addsubplot_population.set_title('Population') 
     addsubplot_population.set_xlabel('Time') 
     addsubplot_population.set_ylabel('Population')  

     canvas_population = FigureCanvasTkAgg(fig_population, self) 
     canvas_population.show() 
     canvas_population.get_tk_widget().pack(side = TOP, fill = BOTH, expand = False) 

if __name__ == "__main__": 
    app = App() 
    app.geometry("800x600+51+51") 
    app.title("World Bank") 
    app.mainloop() 

回答

1

對於您的x軸標籤,一種解決方案是更新數據幀索引類型datetime。現在索引類型是object

至於圖例,您必須在legend方法中指定labels。 查看下面代碼中的評論後添加和更新的行:

class App(Tk): 
    def __init__(self): 
     Tk.__init__(self) 
     fig_population = Figure(figsize=(8.5, 4.5), dpi=100) 
     addsubplot_population = fig_population.add_subplot(111) 

     period_population = (datetime.datetime(2010, 1, 1), datetime.datetime(2016, 7, 23)) 
     countries_population = ["USA", "GBR"] 
     indicators_population = {'SP.POP.TOTL': 'population'} 
     df_population = wbdata.get_dataframe(indicators_population, country=countries_population, 
              data_date=period_population) 
     dfu_population = df_population.unstack(level=0) 

     # update index type 
     dfu_population.index = dfu_population.index.astype('datetime64') 

     x_population = dfu_population.index 
     y_population = dfu_population.population 
     addsubplot_population.plot(x_population, y_population) 

     # legend needs labels 
     addsubplot_population.legend(labels=y_population, loc='best') 

     addsubplot_population.set_title('Population') 
     addsubplot_population.set_xlabel('Time') 
     addsubplot_population.set_ylabel('Population') 

     canvas_population = FigureCanvasTkAgg(fig_population, self) 
     canvas_population.show() 
     canvas_population.get_tk_widget().pack(side=TOP, fill=BOTH, expand=False) 
+0

它的工作原理!非常感謝你的幫助。有一個問題,我們是否需要每次處理日期時更新數據幀索引類型的日期時間? – Fxs7576