数据分析(使用matplotlib,seaborn,ploty进行可视化)——1

版权声明:本BLOG上原创文章未经本人许可,不得用于商业用途及传统媒体。网络媒体转载请注明出处,否则属于侵权行为。 https://blog.csdn.net/qq_38266635/article/details/83691523

柱状图


 使用matplotlib画图

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# 导入数据
df = pd.read_csv("./HR.csv")
#设置标题
plt.title("SALARY")
#设置x轴标注
plt.xlabel("salary")
#设置y轴标注
plt.ylabel("Number")
#设置x轴上的标注为工资的等级
plt.xticks(np.arange(len(df["salary"].value_counts()))+0.5,df["salary"].value_counts().index)
#设置显示范围([xmin,xmax,ymin,ymax])
plt.axis([0,4,0,10000])
#画出柱状图,x轴的内容,y轴的内容,柱宽
plt.bar(np.arange(len(df["salary"].value_counts()))+0.5,df["salary"].value_counts(),width=0.5)
#数字标在图表上
for x,y in zip(np.arange(len(df["salary"].value_counts()))+0.5,df["salary"].value_counts()):
    plt.text(x,y,y,ha="center",va="bottom")
#显示图
plt.show()

使用seaborn画图

#设置图标背景
sns.set_style("whitegrid")
#设置文字的字体,大小
sns.set_context(context="poster",font_scale=0.8)
#设置调色板,颜色可在matplotlib官网Tutorials/Colors/Colormaps in Matplotlib中选择或者使用sns.color_palette()中的颜色
sns.set_palette([sns.color_palette("RdBu", n_colors=7)[5]])
#hue设置以department为分割,又一次进行count(多层)
sns.countplot(x="salary",hue="department",data=df)

猜你喜欢

转载自blog.csdn.net/qq_38266635/article/details/83691523