以下代码示例展示如何使用 Python 读取 Excel 文档中的某一列数据,进行分类并统计数量,然后绘制折线图并注明坐标名称,横坐标轴上数据竖直排放:

import pandas as pd
import matplotlib.pyplot as plt

# 读取 Excel 文档
df = pd.read_excel('data.xlsx')

# 选择需要统计的列
column_name = 'category'
column_data = df[column_name]

# 对列数据进行分类并统计数量
category_counts = column_data.value_counts()

# 绘制折线图
plt.plot(category_counts.index, category_counts.values)

# 添加坐标轴标签和标题
plt.xlabel(column_name)
plt.ylabel('Count')
plt.title('Category Counts')

# 将横坐标轴上的数据竖直排放
plt.xticks(rotation=90)

# 显示图形
plt.show()

其中,'data.xlsx' 是 Excel 文档的文件名,'category' 是需要统计的列名,可以根据实际情况进行修改。运行代码后,就可以得到一个折线图,横坐标轴上的数据会自动竖直排放。

Python 读取 Excel 数据并绘制分类统计折线图

原文地址: https://www.cveoy.top/t/topic/fZ8k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录