首页 > 解决方案 > 如何用python平滑图形

问题描述

我正在努力使这个情节变得平滑。但我无法做到。尝试使用 splrep 进行插值,但它不起作用。任何帮助将不胜感激。该图是使用电流、电压和工作时间的数据框列绘制的。 图片

图片

df2=data[:2000]
plt.subplot(2,1,1)
plt.plot(df2['Op_Hours_Fcpm'],df2['Current'],'r')
plt.xlabel('Operating hours')
plt.title('Current Fluctuations')

plt.subplot(2,1,2)
plt.plot(df2['Op_Hours_Fcpm'],df2['Voltage'],'y')
plt.xlabel('Operating hours')
plt.title('Voltage Fluctuations')

plt.tight_layout()
plt.show()

我也尝试过另一种方式:

fig, ax = plt.subplots()
ax.plot(x_int, current_int, lw = 5, alpha = 0.30, label = 'current')
ax.plot(x_int, voltage_int, lw = 5, alpha = 0.30, label = 'voltage')

ax.set_xlabel('ripples')
ax.set_ylabel('hrs')
# Set the correct xticks
ax.set_xticks(x_map)
ax.set_xticklabels(x)
fig.legend(bbox_to_anchor=(0.7, 0.3), loc='upper left', ncol=1)
fig.show()

这给出了这个输出

标签: pythondataframecsvmatplotlibnormalization

解决方案


由于您没有提供原始数据,我尝试使用从图像中重新创建一些点

import pandas
print(pandas.__version__)
import matplotlib.pyplot as plt
x = [0.5, 0.5, 1,   1, 2,    2,  2 , 3]
y = [0,   600, 0, 600, 0, 1000, 600, 0]
plt.plot(x,y,'r');
plt.xlabel('Operating hours')
plt.title('Current Fluctuations');
plt.savefig('original.png');

产生

原来的

第一个子图的问题是某些时间包含多个值,包括“零”和非零值。这可以更容易地视为散点图

plt.scatter(x, y);
plt.xlabel('Operating hours')
plt.title('Current Fluctuations');
plt.savefig('scatter.png')

分散

这个可视化问题指出了基础数据的问题。

一种选择是丢弃零数据并仅保留非零数据点。


推荐阅读