代码:
from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd
import sys
import matplotlib
import numpy as np
import pylab as pl
pl.draw()
df = pd.read_csv("TB_burden_countries_2018-03-06.csv")
df = df.set_index(["country"])
df2 = df.loc["Zimbabwe", "e_mort_num"]
df = pd.DataFrame(data = df2, columns= ["e_mort_num"])
df.columns = ["Mortality"]
print(df2)
dataset = {'Year': ["2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007","2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015","2016"],
'Mortality': ["20000", "18000", "17000", "19000", "19000", "22000", "24000", "24000", "23000", "17000", "13000", "14000","14000", "11000", "11000", "9000","5600"]}
df3 = pd.DataFrame.from_dict(dataset)
df4 = df3[["Year", "Mortality"]]
plt.bar(df3['Year'], df3['Mortality'])
plt.title('Zimbabwe')
plt.xticks(df3['Year'], rotation=90)
plt.xlabel('Year')
plt.ylabel('Mortality')
plt.tight_layout()
plt.show()
print(df3)
输出结果为:
编辑:我想要实现的目标(由excel制作):
问题:
研究:
答案 0 :(得分:2)
您的问题是您的数据框列Year
和Mortality
是字符串,而matplotlib
并不能很好地工作,至少对于您来说是这样的试图实现。因此,在绘制之前将它们转换为int
:
df = pd.DataFrame(dataset)
df[['Year', 'Mortality']] = df[['Year', 'Mortality']].astype(int)
plt.bar(df['Year'], df['Mortality'], tick_label=df['Year'])
plt.title('Zimbabwe')
plt.xticks(df['Year'], rotation=90)
plt.xlabel('Year')
plt.ylabel('Mortality')
plt.tight_layout()
plt.show()
使用您给定的数据,y轴会自动设置为从0开始。但是,您可以使用ylim
参数将其更改为您想要的任何内容:例如,plt.ylim(500, 50000)
将设置y轴从500开始,到50000结束。