python plot绘制折线图(cpu 负载举例子)

1、绘制图形需要的数据,需要自己新建一个Cpu_Load.xlsx,数据如下

DATECPU0CPU1CPU2CPU3CPU4CPU5CPU6CPU7CPU8CPU9CPU10CPU11CPU ave
2023-12-27 07:59:49.694527896100305.55555522.2222211006010033.3333325.2631589.5238095.5555552541.371136
2023-12-27 07:59:49.87307586410027.77777994.44444329.41176494.4444435010045005.88235322.22222147.431915
2023-12-27 07:59:50.82295192890.32257833.6734798.93617234.04255394.68085554.25531810036.2637376.255.15463911.2244923.15789448.996811
2023-12-27 07:59:51.87139397649.50495131.77570211.88118826.47058941.5841655.14018697.08737936.2745092.9411761.9230779.52380916.49484431.716795
2023-12-27 07:59:52.92500415251.85185228.44036744.11764525.96153835.92232957.79816444.33962233.3333327.6923074.85436913.13131324.50980430.996056
2023-12-27 07:59:54.13722690461.98347131.74603110.16949229.20353915.65217457.530.17241337.6068382013.0081359.32203328.18181832.878826
2023-12-27 07:59:55.19436610465.42056327.3584912.529.52380922.7722786029.41176444.54545677.66990710.18518522.33009722.91666635.386185
2023-12-27 07:59:56.28205605668.62744928.82882910.57692331.06796124.03846259.63302631.42857242.99065466.0377358.57142823.36448719.14893534.526203
2023-12-27 07:59:57.34056962460.95238120.2020219.80392231.13207621.35922258.65384732.0754746.7889920.19230821.56862820.75471716.30434829.982332
2023-12-27 07:59:58.40307500058.09523818.5567029.70873827.27272822.85714358.87850630.39215743.80952520.75471728.03738420.3883517.02127629.647705
2023-12-27 07:59:59.45666172056.7307718.75830.39215725.68807458.87850630.47619143.2692317.47572923.3009717.64705814.44444528.754431
2023-12-27 08:00:00.51548364066.35514120.61855711.42857232.0754723.36448760.55045732.71028143.13725722.11538527.3584919.41747715.38461531.209684
2023-12-27 08:00:01.57312386457.79816415.05376311.53846228.4313727555.88235533.98058345.79439221.69811227.45098120.95238115.21739134.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、生成的效果图如下,具体的参数调节需要自己调节

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值