问题出在第63行的plt.plot函数中,x和y的维度不匹配。x的维度为(9,),而y1使用了np.repeat(y1, 10)扩展成了(900,)。解决方法是在plt.plot函数中使用x和y1[:9]来保持维度的一致性。

修改后的代码如下:

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

x = np.arange(0, 90, 10)
y1 = np.random.randint(130, 160, 90)
y2 = np.random.randint(80000, 88000, 90)

plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=(8, 6))
plt.xlim(0, 90)
plt.ylim(0, 90000)
plt.xticks(np.arange(0, 100, 10), ["0", "10", "20", "30", "40", "50", "60", "70", "80", "90"])
plt.yticks(np.arange(0, 100000, 10000), ["0", "1万", "2万", "3万", "4万", "5万", "6万", "7万", "8万", "9万"])
plt.xlabel("Variation", labelpad=10, fontsize='xx-large', color='#70AD47', fontweight='bold')
plt.ylabel("Capacity", labelpad=10, fontsize='xx-large', color='#70AD47', fontweight='bold')
plt.grid(b=True, linestyle="dashed", linewidth=1)
plt.title(label="SKU1 Variation与Capacity变化关系", loc="center")

for a, b in zip(x, y1):
    plt.text(a, b, str(b), ha='center', va='bottom', fontsize=11)

for a, b in zip(x, y2):
    plt.text(a, b, str(b), ha='center', va='bottom', fontsize=11)

plt.plot(np.arange(0, 90, 10), y1[:9], color="k", linestyle="solid", linewidth=1,
         marker="o", markersize=3, label='折线图')

width = 3
plt.bar(x - width / 2, y2, width=width, color="k", label='柱形图')

plt.legend(loc="upper left")
plt.legend(ncol=2)

plt.annotate("服务器宕机了", xy=(60, 1), xytext=(70, 1), arrowprops=dict(facecolor='black', arrowstyle='->'))

plt.savefig("sku1.png")
plt.show()

这样修改后的代码应该可以正常运行并生成图片。

用python完成以下需求请示例代码:import numpy as npimport pandas as pdimport matplotlibpyplot as pltx = nparange0 90 10y1 = nprandomrandint130 160 90y2 = nprandomrandint80000 88000 90pltrcParamsfontsans-serif = Kai

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

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