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

username = 'your_username'  # 请替换为你的用户名

toolpath = (r'C:\Users\%s\Desktop' % username)
os.chdir(toolpath)
df = pd.read_csv(r'BatteryMonitor1_modified.csv')
variation_array = df['Variation'].values
variation_str = ','.join(map(str, variation_array))  # 将数据中间用逗号隔开
variation_ray = '[' + variation_str + ']'  # 数据列
print(variation_ray)
capacity_array = df['capacity'].values
capacity_str = ','.join(map(str, capacity_array))  # 将数据中间用逗号隔开
capacity_ray = '[' + capacity_str + ']'  # 数据列
print(capacity_ray)
total_rows = len(df)
print(total_rows)  # 总数量
y1 = np.array(eval(variation_ray))
y2 = np.array(eval(capacity_ray))
x = np.linspace(1, total_rows, len(y1))  # 生成与y长度相同的x坐标

# 调整画布大小为20x10
fig, ax1 = plt.subplots(figsize=(20, 10))
ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax1.plot(x, y1, '-', label='Variation', alpha=0.3, color='r')
ax2.bar(x, y2, width=0.4, label='capacity', alpha=0.3, color='b')
ax1.set_xlabel('x')
ax1.set_ylabel('Variation', color='r')
ax2.set_ylabel('capacity', color='b')
ax1.tick_params(axis='y', colors='r')
ax2.tick_params(axis='y', colors='b')
ax3.tick_params(axis='x')
plt.grid(True)
plt.legend()
plt.show()

在上述代码中,通过将 figsize 参数设置为 (20, 10),将画布大小调整为更大的尺寸,以便能够容纳整个图形。你可以根据需要调整 figsize 的值来获得适合的画布大小。

Python 使用 Matplotlib 绘制双 Y 轴图表,并调整画布大小

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

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