Matplotlib线型图由于d中的NaN值而向上启动

2024-09-24 02:23:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在dataframe中有几个列有NaN值。因此,当我试图创建一个包含多个列的线图时,这些图会突然开始。我如何避免这种情况?用零填充数据集中的NaN值?或者还有别的办法吗?在

enter image description here

看红线!在

代码

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(70,40)) 
ax = fig.add_subplot(1, 1, 1)
ax.tick_params(direction='out', length=10, width=3,labelsize=35)
group_combined.plot(ax=ax,x='Date',y=['column1','collumn2'],linewidth=7.0)
ax.set_xlabel("date",size=40)
ax.set_ylabel("Number of orders",size=40)
ax.set_title("Distribution of orders over the month",size=50)

在我的数据中,没有NaN。对于一些专栏,对于最初的几个日期,NaN's可能会出现。一旦价值开始出现,就不会有任何问题。在

Python版本:3.6 Matplotlib版本:2.0.0


Tags: of数据版本dataframesizefig情况plt
1条回答
网友
1楼 · 发布于 2024-09-24 02:23:19

以下是使用numpy删除nan的方法:

from matplotlib import pyplot as plt
import numpy as np

fix,axes = plt.subplots(1,2)

#setting up a simple function
x = np.linspace(0,1,100)
y = np.exp(-x)

#inserting nan values at random places
#to get 'measurement data'
z = np.random.random(100)
y[z>0.9] = np.nan

#showing original
axes[0].plot(x,y)

#removing nans and replotting:
x = x[~np.isnan(y)]
y = y[~np.isnan(y)]
axes[1].plot(x,y)

plt.show()

结果如下: plot with nans and nans removed 左图显示了带有nan的数据,而在右侧,它们被删除了。在

相关问题 更多 >