LSR原数值非常大,可是在最后呈现出来的图表中y轴数值特别的小,我觉得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的均值 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.xticks(range(2008, 2022, 1)) plt.ylim(0, 200000000000) # 设置y轴范围 # 使用科学计数法表示 y 轴的数值 plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文字体设置-黑体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 plt.xlabel('年份') plt.ylabel('LSR') plt.legend() plt.show()
时间: 2023-09-06 15:09:47 浏览: 162
LSR.rar_lsr_计算机网络_计算机网络lsr
你可以将 y 轴标签设为 'LSR (10亿)',表示单位为 10 亿。同时可以适当调整 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的均值
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.xticks(range(2008, 2022, 1))
plt.ylim(0, 200000000000) # 设置y轴范围
# 调整 y 轴刻度
plt.yticks([0, 50000000000, 100000000000, 150000000000, 200000000000], ['0', '50', '100', '150', '200'])
plt.ticklabel_format(style='plain', axis='y') # 取消科学计数法表示
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文字体设置-黑体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
plt.xlabel('年份')
plt.ylabel('LSR (10亿)')
plt.legend()
plt.show()
```
运行修改后的代码,即可得到修改后的图形。
阅读全文