import os import pandas as pd import numpy as np import matplotlib.pyplot as plt

设置工作路径

toolpath = r"C:\Users%s\Desktop" % username os.chdir(toolpath)

读取CSV文件

df = pd.read_csv(r"BatteryMonitor1_modified.csv")

获取capacity列的值

variation_array = df["capacity"].values

将数据转换为字符串,并用逗号隔开

variation_str = ','.join(map(str, variation_array))

构造x_ray字符串

x_ray = '[' + variation_str + ']'

打印x_ray和数据总量

print(x_ray) total_rows = len(df) print(total_rows) # 总数量

定义绘图函数

def photo(x_ray, total_rows): # 将x_ray字符串转换为numpy数组 y = np.array(eval(x_ray)) x = np.arange(1, total_rows + 1) # 生成离散的 x 坐标

# 清除之前的图形数据
plt.clf()

# 设置中文显示
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False

# 绘制柱状图
plt.bar(x, y, width=0.5, label="capacity", alpha=0.3, color='b')

# 设置网格线和图例
plt.grid(True)
plt.legend()

# 显示图形
plt.show()

调用绘图函数

photo(x_ray, total_rows)

用python完成以下需求请示例代码 toolpath = rCUserssDesktop username oschdirtoolpath df = pdread_csvrBatteryMonitor1_modifiedcsv variation_array = dfcapacityvalues variation_str = joinmapstr variation

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

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