在Python中以折线图连接数据点

时间:2018-11-15 02:28:24

标签: python matplotlib line

我在Python中定义了此函数,以便随时间绘制两个指标ginieconomic freedom

def PlotCountry(countries):
    simple = df_gini.loc[countries, :].sort_values(by = "year", ascending = True)
    simple = simple.reset_index()
    simple = simple.drop(['rank', '5_regulation'], axis = 1)
    simple = simple.round(2)
    simple.columns = ["Year", "ISO Code", "Gini Index", "Economic Freedom"]

    #Plot a double-axis lineplot
    fig, ax1 = plt.subplots()
    color = 'darkred'
    ax1.set_xlabel('Year')
    ax1.set_ylabel('Gini Index', color = color)
    ax1.plot("Year", "Gini Index", color = color, data = simple, linestyle='--', marker = ".")
    ax1.tick_params(axis='y', labelcolor = color)

    ax2 = ax1.twinx()  #Instantiate second axis that shares the same x-axis

    color = 'darkblue'
    ax2.set_ylabel('Economic Freedom', color = color)
    ax2.plot("Year", "Economic Freedom", color = color, data = simple)
    ax2.tick_params(axis='y', labelcolor=color)

    plt.title(countries)

    fig.tight_layout()  #Otherwise right y-label is slightly clipped
    plt.show()

有时候我得到的情节看起来像: enter image description here

如何连接红点?

0 个答案:

没有答案