1、绘制图形需要的数据,需要自己新建一个Cpu_Load.xlsx,数据如下
| DATE | CPU0 | CPU1 | CPU2 | CPU3 | CPU4 | CPU5 | CPU6 | CPU7 | CPU8 | CPU9 | CPU10 | CPU11 | CPU ave |
| 2023-12-27 07:59:49.694527896 | 100 | 30 | 5.555555 | 22.222221 | 100 | 60 | 100 | 33.333332 | 5.263158 | 9.523809 | 5.555555 | 25 | 41.371136 |
| 2023-12-27 07:59:49.873075864 | 100 | 27.777779 | 94.444443 | 29.411764 | 94.444443 | 50 | 100 | 45 | 0 | 0 | 5.882353 | 22.222221 | 47.431915 |
| 2023-12-27 07:59:50.822951928 | 90.322578 | 33.67347 | 98.936172 | 34.042553 | 94.680855 | 54.255318 | 100 | 36.263737 | 6.25 | 5.154639 | 11.22449 | 23.157894 | 48.996811 |
| 2023-12-27 07:59:51.871393976 | 49.504951 | 31.775702 | 11.881188 | 26.470589 | 41.58416 | 55.140186 | 97.087379 | 36.274509 | 2.941176 | 1.923077 | 9.523809 | 16.494844 | 31.716795 |
| 2023-12-27 07:59:52.925004152 | 51.851852 | 28.440367 | 44.117645 | 25.961538 | 35.922329 | 57.798164 | 44.339622 | 33.333332 | 7.692307 | 4.854369 | 13.131313 | 24.509804 | 30.996056 |
| 2023-12-27 07:59:54.137226904 | 61.983471 | 31.746031 | 10.169492 | 29.203539 | 15.652174 | 57.5 | 30.172413 | 37.606838 | 20 | 13.00813 | 59.322033 | 28.181818 | 32.878826 |
| 2023-12-27 07:59:55.194366104 | 65.420563 | 27.35849 | 12.5 | 29.523809 | 22.772278 | 60 | 29.411764 | 44.545456 | 77.669907 | 10.185185 | 22.330097 | 22.916666 | 35.386185 |
| 2023-12-27 07:59:56.282056056 | 68.627449 | 28.828829 | 10.576923 | 31.067961 | 24.038462 | 59.633026 | 31.428572 | 42.990654 | 66.037735 | 8.571428 | 23.364487 | 19.148935 | 34.526203 |
| 2023-12-27 07:59:57.340569624 | 60.952381 | 20.202021 | 9.803922 | 31.132076 | 21.359222 | 58.653847 | 32.07547 | 46.78899 | 20.192308 | 21.568628 | 20.754717 | 16.304348 | 29.982332 |
| 2023-12-27 07:59:58.403075000 | 58.095238 | 18.556702 | 9.708738 | 27.272728 | 22.857143 | 58.878506 | 30.392157 | 43.809525 | 20.754717 | 28.037384 | 20.38835 | 17.021276 | 29.647705 |
| 2023-12-27 07:59:59.456661720 | 56.73077 | 18.75 | 8 | 30.392157 | 25.688074 | 58.878506 | 30.476191 | 43.26923 | 17.475729 | 23.30097 | 17.647058 | 14.444445 | 28.754431 |
| 2023-12-27 08:00:00.515483640 | 66.355141 | 20.618557 | 11.428572 | 32.07547 | 23.364487 | 60.550457 | 32.710281 | 43.137257 | 22.115385 | 27.35849 | 19.417477 | 15.384615 | 31.209684 |
| 2023-12-27 08:00:01.573123864 | 57.798164 | 15.053763 | 11.538462 | 28.431372 | 75 | 55.882355 | 33.980583 | 45.794392 | 21.698112 | 27.450981 | 20.952381 | 15.217391 | 34.066498 |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
| | | | | | | | | | | | | | |
2、python代码如下
import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.pyplot as plt
import mplcursors # 导入mplcursors库
from matplotlib import ticker
# 设置正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# 正常显示负号
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
# 设置字体大小
plt.rcParams.update({'font.size': 16})
data = pd.read_excel('./Cpu_Load.xlsx', header=0, index_col=0)
print(data)
print(data.columns)
x = data['DATE'].tolist()
# print(x)
y1 = data['CPU0'].tolist()
for value in y1:
if(value==100.0):
print("CPU0 负载达到100%")
break
y2 = data['CPU1'].tolist()
y3 = data['CPU2'].tolist()
y4 = data['CPU3'].tolist()
y5 = data['CPU4'].tolist()
y6 = data['CPU5'].tolist()
y7 = data['CPU6'].tolist()
y8 = data['CPU7'].tolist()
y9 = data['CPU8'].tolist()
y10 = data['CPU9'].tolist()
y11 = data['CPU10'].tolist()
y12 = data['CPU11'].tolist()
# print(y1)
# print(y2)
# print(y3)
# 使用plt.figure函数的num参数指定Figure的名称
# fig = plt.figure(num='IM', figsize=(32, 16))
fig, ax = plt.subplots(num='IM',figsize=(32, 16))
plt.title('Tatol Cpu Load', fontsize=26, loc='center', color='k') # 折线图标题
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示汉字
plt.xlabel('date') # x轴标题
plt.ylabel('百分比') # y轴标题
plt.plot(x, y1, marker='o', markersize=3, color='blue', label='CPU0')
plt.plot(x, y2, marker='o', markersize=3, color='green', label='CPU1')
plt.plot(x, y3, marker='o', markersize=3, color='red', label='CPU2')
plt.plot(x, y4, marker='o', markersize=3, color='purple', label='CPU3')
plt.plot(x, y5, marker='o', markersize=3, color='orange', label='CPU4')
plt.plot(x, y6, marker='o', markersize=3, color='brown', label='CPU5')
plt.plot(x, y7, marker='o', markersize=3, color='pink', label='CPU6')
plt.plot(x, y8, marker='o', markersize=3, color='gray', label='CPU7')
plt.plot(x, y9, marker='o', markersize=3, color='cyan', label='CPU8')
plt.plot(x, y10, marker='o', markersize=3, color='magenta', label='CPU9')
plt.plot(x, y11, marker='o', markersize=3, color='olive', label='CPU10')
plt.plot(x, y12, marker='o', markersize=3, color='teal', label='CPU11')
# plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1), ncol=4)
#
# for a, b in zip(x, y1):
# plt.text(a, b, b, ha='center', va='bottom', fontsize=10) # 设置数据标签位置及大小
# for a, b in zip(x, y2):
# plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
# for a, b in zip(x, y3):
# plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
# for a, b in zip(x, y4):
# plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
#
# plt.legend(['CPU0', 'CPU1', 'CPU2', 'CPU3']) # 设置折线名称
# plt.legend(['CPU0', 'CPU1', 'CPU2', 'CPU3'], loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=4)
plt.legend(['CPU0', 'CPU1', 'CPU2', 'CPU3','CPU4', 'CPU5', 'CPU6', 'CPU7','CPU8', 'CPU9', 'CPU10', 'CPU11'], loc='upper center', bbox_to_anchor=(0.5, 1), ncol=12)
# plt.legend(['CPU0', 'CPU1', 'CPU2', 'CPU3'], loc='lower center') # 设置图例水平放置
# plt.savefig('./figure.jpg', bbox_inches='tight')
# x轴的打印
plt.xticks(rotation=90, fontsize=8)
#设置打印的密度
tick_spacing = 1
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
# 画完折线图后添加以下代码
cursor = mplcursors.cursor(hover=True) # 创建mplcursors的cursor对象
@cursor.connect("add")
def on_add(sel):
sel.annotation.set_text(f"{sel.artist.get_label()}: {sel.target[0]:.2f}:{sel.target[1]:.2f}") # 设置鼠标悬停时显示的文本
# # sel.annotation.get_bbox_patch().set(fc="white", alpha=0.9, lw=0.72) # 设置注释框样式,白色背景,透明度0.9
# @cursor.connect("add")
# def on_add(sel):
# x, y = sel.target
# text = f"X: {x:.2f}\n"
# for artist in ax.get_lines():
# label = artist.get_label()
# y_value = artist.get_ydata()[sel.target.index]
# text += f"{label}: {y_value:.2f}\n"
# sel.annotation.set_text(text)
plt.show() # 显示折线图
3、生成的效果图如下,具体的参数调节需要自己调节