这段代码是如何做到把散点和折线放在一张图上的: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')
时间: 2023-10-07 16:07:45 浏览: 59
这段代码利用了 matplotlib 库中的 scatter 和 plot 方法。首先通过 scatter 方法绘制散点图,然后通过 plot 方法绘制折线图。由于两种图形的 x 轴和 y 轴是一致的,所以它们可以放在同一个图形中。通过 label 参数可以设置图例,使得不同的数据点和线条能够被区分开来。最后,通过 linestyle 和 color 参数可以设置折线的样式和颜色。
相关问题
改进这个代码使散点和折线在一张图上生成一张散点折线图: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.scatter(lsr_mean[lsr_mean['ownership'] == '非国有']['年份'], lsr_mean[lsr_mean['ownership'] == '非国有']['LSR'], label='非国有企业') plt.xlabel('年份') plt.ylabel('LSR') plt.legend() plt.show() # 分别绘制国有企业和非国有企业的折线图 plt.plot(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'], label='非国有企业') plt.xlabel('年份') plt.ylabel('LSR') plt.legend() plt.show()
可以使用plt.plot()函数来绘制折线图,并将两个散点图的绘制代码合并到一起,如下所示:
```
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.xlabel('年份')
plt.ylabel('LSR')
plt.legend()
plt.show()
```
这段代码将两个散点图和两个折线图绘制在同一张图中,其中折线图使用plt.plot()函数绘制,使用linestyle参数设置线条样式,使用color参数设置线条颜色。
帮我改进这个代码,增加横纵坐标的长度: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()
可以在绘图前添加如下两行代码,设置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()
```
阅读全文