日期和图例未在MatplotLib中正确显示

时间:2016-07-23 22:18:32

标签: python matplotlib tkinter

我的目标是使用来自GUI TKinter的Worldbank API创建一个图表(带有正确的xy标签和图例)。

我一直在处理诸如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 个答案:

答案 0 :(得分:1)

对于x轴标签,一种解决方案是将数据框索引类型更新为datetime。现在索引类型是object

至于图例,您必须在labels方法中指定legend。 在以下代码中的注释后查看添加和更新的行:

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)