import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_excel("D:\我的文档\jupyter notebook.xlsx")
df
sns.set(style = "white", font_scale = 1.2)
# 解决中文乱码
plt.rcParams[ "font.sans-serif"] = "simhei"
# 设置字体及其大小
font1 = {"family" : "simhei",
"weight" : "normal",
"size" : 18}
# 数透
region_pivot = pd.pivot_table(df, values="总评成绩", index="课程性质", aggfunc='count').reset_index().sort_values(ascending=True,by="总评成绩")
# 设置画布大小
f, ax = plt.subplots(figsize=(8, 6))
# 绘制条形图
barh = plt.barh(region_pivot["课程性质"].values,region_pivot["总评成绩"].values, color='dodgerblue')
barh[-1].set_color("red")
# 给条形图添加数据标签
for y, x in enumerate(region_pivot["总评成绩"].values):
plt.text(x + 60, y - 0.1, "%s" %x)
#去掉四周的外框线
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
# ax.set(title = "各种性质课程的总评成绩之和", xlabel = "总评成绩", ylabel = "课程性质")
# 设置维度的字体大小
plt.tick_params(labelsize = 15)
plt.xlabel("课程性质", font1)
plt.ylabel("总评成绩", font1)
plt.title("各种性质课程的总评成绩之和", font1)
# 保存输出可视化图片
f.savefig(r"C:\Users\QDM\Desktop\1.png", bbox_inches= "tight")
sns.set(style = "white", font_scale = 1.2)
# 解决中文乱码
plt.rcParams["font.sans-serif"] = "simhei"
# 设置字体及其大小
font1 = {"family" : "simhei","weight" : "normal","size" : 18,}
# 数透
region_pivot = pd.pivot_table(df, values = "总评成绩", index = "课程性质", aggfunc = "count").reset_index().sort_values(ascending = False, by="总评成绩")
# 设置画布大小
f, ax = plt.subplots(figsize=(12, 6))
# 绘制柱形图
bar = plt.bar(region_pivot["课程性质"].values, region_pivot["总评成绩"].values, color = "dodgerblue")
bar[0].set_color("green")
# 添加标签
for x, y in enumerate(region_pivot["总评成绩"].values):
plt.text(x - 0.2, y + 60, "%s" %y)
#删除所有框线
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
# ax.set(title = "各种性质课程的总评成绩之和", xlabel = "课程性质", ylabel = "总评成绩")
plt.tick_params(labelsize = 14)
plt.xlabel("课程性质", font1)
plt.ylabel("总评成绩", font1)
plt.title("各种性质课程的总评成绩之和", font1)
f.savefig(r"C:\Users\QDM\Desktop\纵向柱形图.png", bbox_inches = "tight")