帮我改进这个代码,增加横纵坐标的长度:import matplotlib.pyplot as plt # 定义一个函数,用于将实际控制人性质分类为国有和非国有 def gov_or_not(x): gov_list = [1100, 2000, 2100, 2120] if x in gov_list: return '国有' else: return '非国有' # 将实际控制人性质进行分类 df['ownership'] = df['实际控制人性质'].apply(gov_or_not) # 按照企业性质和年份进行分组,计算LSR的均值 lsr_mean = df.groupby(['ownership', '年份'])['LSR'].mean().reset_index() # 绘制散点图和折线图 plt.scatter(lsr_mean[lsr_mean['ownership'] == '国有']['年份'], lsr_mean[lsr_mean['ownership'] == '国有']['LSR'], label='国有企业') plt.plot(lsr_mean[lsr_mean['ownership'] == '国有']['年份'], lsr_mean[lsr_mean['ownership'] == '国有']['LSR'], linestyle='-', color='blue') plt.scatter(lsr_mean[lsr_mean['ownership'] == '非国有']['年份'], lsr_mean[lsr_mean['ownership'] == '非国有']['LSR'], label='非国有企业') plt.plot(lsr_mean[lsr_mean['ownership'] == '非国有']['年份'], lsr_mean[lsr_mean['ownership'] == '非国有']['LSR'], linestyle='-', color='green') plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文字体设置-黑体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 plt.xlabel('年份') plt.ylabel('LSR') plt.legend() plt.show()
时间: 2023-09-09 20:12:46 浏览: 139
可以在绘图前添加如下两行代码,设置x轴和y轴的范围:
```
plt.xlim(2015, 2020) # 设置x轴范围
plt.ylim(0, 1) # 设置y轴范围
```
这样可以使图像更加清晰,代码如下:
```
import matplotlib.pyplot as plt
def gov_or_not(x):
gov_list = [1100, 2000, 2100, 2120]
if x in gov_list:
return '国有'
else:
return '非国有'
df['ownership'] = df['实际控制人性质'].apply(gov_or_not)
lsr_mean = df.groupby(['ownership', '年份'])['LSR'].mean().reset_index()
plt.scatter(lsr_mean[lsr_mean['ownership'] == '国有']['年份'],
lsr_mean[lsr_mean['ownership'] == '国有']['LSR'],
label='国有企业')
plt.plot(lsr_mean[lsr_mean['ownership'] == '国有']['年份'],
lsr_mean[lsr_mean['ownership'] == '国有']['LSR'],
linestyle='-', color='blue')
plt.scatter(lsr_mean[lsr_mean['ownership'] == '非国有']['年份'],
lsr_mean[lsr_mean['ownership'] == '非国有']['LSR'],
label='非国有企业')
plt.plot(lsr_mean[lsr_mean['ownership'] == '非国有']['年份'],
lsr_mean[lsr_mean['ownership'] == '非国有']['LSR'],
linestyle='-', color='green')
plt.xlim(2015, 2020) # 设置x轴范围
plt.ylim(0, 1) # 设置y轴范围
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel('年份')
plt.ylabel('LSR')
plt.legend()
plt.show()
```
阅读全文