plt.figure(figsize=(10,10))
data = [0.12,0.2341,0.4355,0.6778,0.0234,0.22,0.3341]
labels = ['Java','Python','A','B','C','D','E']
colors = ['red','pink','purple','orange']
explode = [0,0,0.3,0,0,0,0]
plt.xlim(0,12)
plt.ylim(0,12)
plt.axis(aspect = 'equal')
plt.pie(x = data,#数据
explode = explode,#突出显示
labels=labels,#标签
colors = colors,#颜色
autopct = '%.3f%%',#百分比设置显示位数
startangle=180,#设置饼图的初始角度
labeldistance=1.3,#设置标签与圆心的距离
pctdistance=0.8,#设置百分比标签与圆心的距离
center = (6,6),#圆心的位置
radius= 4,#设置饼图的半径
counterclock=False,#是否为逆时针方向 False为顺时针
wedgeprops={'linewidth':0.1,'edgecolor':'green'},#内外边界的属性设置
textprops={'fontsize':15,'color':'black'},#设置文本标签的属性
frame = 1)#是否显示饼图的外方框,此处显示
plt.xticks(())#不显示坐标
plt.yticks(())#不显示坐标
plt.show()
import matplotlib
import matplotlib.pyplot as plt
def get_data(csv):
data = {}
with open(csv) as file:
for line in file:
print(line)
gender = line.split(",")[2]
# 跳过表头列
if('gender' == gender):
continue
if (data.__contains__(gender)):
data[gender] += 1
else:
data[gender] = 1
return data
def pie(data):
x = [i for i in data]
y = [j for j in data.values()]
# 控制画布大小
plt.figure(figsize=(15, 15))
# autopct用来输出数据所占据的比例, textprops规定字体大小, labeldistance规定数据距离饼图的位置
plt.pie(y, labels=x, autopct="%.2f%%", colors=['c', 'm', 'y', 'b'],
textprops={'fontsize': 24}, labeldistance=1.1)
# 饼图标题
plt.title("人数统计饼图", fontsize=30)
# upper left 将图例显示到左上角
plt.legend(loc="upper left", fontsize=20)
# 在py文件同一目录下 下载名为饼图的jpg文件
plt.savefig("img/pie.jpg")
plt.show()
def bar(data):
x = [i for i in data]
y = [j for j in data.values()]
# 设置图形对象 :窗口
plt.figure('Figure Object 1', # 图形对象名称 窗口左上角显示
figsize = (8, 5), # 窗口大小
dpi = 120, # 分辨率
facecolor = 'white', # 背景色
)
#绘制条形图
"""
left:长条形中点横坐标
height:长条形高度
长条形宽度,默认值0.8
label:为后面设置legend准备
"""
rects1=plt.bar(x, y, width=0.2,label='人数',alpha=0.8,color='orange')
#刻度
plt.xticks(range(len(x)),x,rotation=90,fontproperties='SimHei',fontsize=8)
#plt.yticks(range(len(x)), x, rotation=90)
#条形图高度的标注
for rect in rects1:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width()/2, height+0.2, height, ha="center", va="bottom",fontsize=7)
#坐标轴范围
plt.xlim(-1,len(x)+1)
plt.ylim(0, max(y)*1.2)
#显示图例
plt.legend()
#坐标轴标注
plt.xlabel("性别",fontproperties='SimHei',fontsize=8)
plt.ylabel("人数",fontproperties='SimHei',fontsize=10)
#标题
plt.title('人数统计条形图',fontproperties='SimHei',fontsize=12)
# 在py文件同一目录下 下载名为饼图的jpg文件
plt.savefig("img/bar.jpg")
plt.show()
# 处理乱码
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体正常显示中文标签
# 传入数据
data = get_data('img/person.csv')
print(data)
pie(data)
bar(data)