我在Python中定义了此函数,以便随时间绘制两个指标gini
和economic 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()
如何连接红点?