Python对薪资图表的代码
时间: 2023-07-10 10:05:41 浏览: 45
以下是用Python绘制薪资图表的示例代码:
```python
import matplotlib.pyplot as plt
# 岗位列表
jobs = ['数据分析师', '机器学习工程师', '人工智能研究员', '大数据工程师', '深度学习工程师']
# 月薪列表(单位:元)
salaries = [15000, 25000, 35000, 20000, 28000]
# 绘制柱状图
plt.bar(jobs, salaries)
# 添加标题和标签
plt.title('不同岗位月薪水平')
plt.xlabel('岗位')
plt.ylabel('月薪(元)')
# 显示图表
plt.show()
```
这段代码使用了`matplotlib`库,通过`plt.bar()`函数绘制柱状图,并使用`plt.title()`、`plt.xlabel()`和`plt.ylabel()`函数添加标题和标签。最后使用`plt.show()`函数显示图表。您可以根据自己的需求对代码进行修改,例如更改岗位和薪资数据。
相关问题
python回归分析代码
以下是一个简单的 Python 回归分析代码示例:
```python
# 导入所需的库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# 读取数据
data = pd.read_csv('data.csv')
# 创建 X 和 y
X = data.iloc[:, :-1].values
y = data.iloc[:, 1].values
# 将数据拆分为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# 创建线性回归对象并拟合训练集
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# 预测测试集结果
y_pred = regressor.predict(X_test)
# 绘制训练集结果
plt.scatter(X_train, y_train, color='red')
plt.plot(X_train, regressor.predict(X_train), color='blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
# 绘制测试集结果
plt.scatter(X_test, y_test, color='red')
plt.plot(X_train, regressor.predict(X_train), color='blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
```
这个示例使用简单线性回归模型来预测员工工资和工作经验之间的关系。我们首先从 CSV 文件中读取数据,然后使用 scikit-learn 库的 train_test_split() 函数将数据集拆分为训练集和测试集。我们然后创建一个 LinearRegression 对象并将其拟合到训练集。最后,我们使用 predict() 函数来预测测试集结果,并使用 matplotlib 库绘制结果图表。
用python代码实现matplotlib绘制折线图,工作经验是X轴、工资是Y轴
好的,以下是用Python代码实现matplotlib绘制折线图,工作经验是X轴,工资是Y轴的示例代码:
```python
import matplotlib.pyplot as plt
# X轴数据
work_experience = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Y轴数据
salary = [3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000]
# 绘制折线图
plt.plot(work_experience, salary)
# 设置X轴标签和Y轴标签
plt.xlabel('Work Experience')
plt.ylabel('Salary')
# 设置图表标题
plt.title('Salary vs Work Experience')
# 显示图表
plt.show()
```
运行以上代码,即可得到绘制的折线图,图中展示了工作经验和工资之间的关系。