CSV全文按照不固定数量的空格拆分,拆分后有50列,把所有标题字段以"a","bb","ccc"结尾的分类,剩余所有的为一组,把这4个折线图画在一张纸上,每一根线的名称标注在线左边
时间: 2023-07-19 07:23:33 浏览: 51
好的,可以使用 Matplotlib 库的 label 和 legend 函数来实现每条线的名称标注以及图例的显示。请参考以下代码实现:
```python
import csv
import matplotlib.pyplot as plt
import pandas as pd
# 读取 CSV 文件并按照空格拆分为各个字段
with open('your_csv_file.csv', 'r') as f:
reader = csv.reader(f)
rows = [row for row in reader]
# 将拆分后的数据转换成 Pandas 的 DataFrame
df = pd.DataFrame(rows)
# 筛选出所有以"a", "bb", "ccc"结尾的标题字段
a_df = df.filter(regex='a$', axis=1)
bb_df = df.filter(regex='bb$', axis=1)
ccc_df = df.filter(regex='ccc$', axis=1)
# 将剩余的所有字段作为一组
other_df = df.drop(a_df.columns, axis=1).drop(bb_df.columns, axis=1).drop(ccc_df.columns, axis=1)
# 绘制折线图
fig, ax = plt.subplots()
a_df.plot(ax=ax)
bb_df.plot(ax=ax)
ccc_df.plot(ax=ax)
other_df.plot(ax=ax)
plt.legend(loc='upper left')
plt.ylabel('Values')
plt.xlabel('Index')
plt.show()
```
上述代码中,legend 函数中的参数 loc='upper left' 表示将图例放在左上角。ylabel 和 xlabel 函数分别用于设置纵轴和横轴的标签。在绘制每条线的时候,可以使用 plot 函数的 label 参数来设置每条线的名称。例如:
```python
a_df.plot(ax=ax, label='a')
bb_df.plot(ax=ax, label='bb')
ccc_df.plot(ax=ax, label='ccc')
other_df.plot(ax=ax, label='other')
```
这样就可以在左边显示每条线的名称了。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)