csv数据如下:
代码如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'simhei'
plt.rcParams['axes.unicode_minus']=False
data = pd.read_csv('student.csv')
# 分组函数,按照行索引进行分组,把省份相等的字段分组在一起
# obj.groupby([‘key1’,’key1’]),把key1和key2相等的字段分组在一起
data = data.groupby('省份')
hnsq = []
hnrs = []
# 必须遍历拿数据,且里面只有两组数据,分组的索引和所有的数据
for i,info in data:
if i == '湖北':
sq = info['市区']
rs = info['人数']
plt.subplot(2,2,1)
plt.plot(np.arange(len(sq)),rs,'-*')
plt.xticks(np.arange(len(sq)),sq)
elif i == '湖南':
sq = info['市区']
rs = info['人数']
plt.subplot(2,2,2)
plt.plot(np.arange(len(sq)),rs,'-*')
plt.xticks(np.arange(len(sq)),sq)
elif i == '西藏':
sq = info['市区']
rs = info['人数']
plt.subplot(2,2,3)
plt.plot(np.arange(len(sq)),rs,'-*')
plt.xticks(np.arange(len(sq)),sq)
elif i == '贵州':
sq = info['市区']
rs = info['人数']
plt.subplot(2,2,4)
plt.plot(np.arange(len(sq)),rs,'-*')
plt.xticks(np.arange(len(sq)),sq)
else:
pass
plt.tight_layout()
plt.show()
效果图如下: