Python Matplotlib未在数据点之间绘制直线

Python Matplotlib未在数据点之间绘制直线,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一组降雪累积数据,我正在从csv绘制,看起来是这样的: 我试图根据RAP数据绘制GFS数据,RAP将完美地绘制连接点和线。然而,GFS将只作为点数据进行绘制:而我,就我的一生而言,还没有找到一种方法来绘制GFS数据,并用一条线连接这些点。以下是我一直使用的代码: gfs = df['GFS'] rap = df['RAP'] fig2, ax2 = plt.subplots(figsize=(10,8)) ax2.plot(fh,gfs,'ob-') ax2.plot(fh,rap,mar

我有一组降雪累积数据,我正在从csv绘制,看起来是这样的:

我试图根据RAP数据绘制GFS数据,RAP将完美地绘制连接点和线。然而,GFS将只作为点数据进行绘制:而我,就我的一生而言,还没有找到一种方法来绘制GFS数据,并用一条线连接这些点。以下是我一直使用的代码:

gfs = df['GFS']
rap = df['RAP']
fig2, ax2 = plt.subplots(figsize=(10,8))
ax2.plot(fh,gfs,'ob-') 
ax2.plot(fh,rap,marker='x')
ax2.tick_params(which='major',labelsize='12')
ax2.grid(which='major', color='#CCCCCC', linestyle='-')
plt.xticks(rotation='90')
plt.xlabel('Forecast Run')
plt.ylabel('Snowfall Accumulation (in.)')
plt.legend()
任何帮助和指导都将不胜感激


使用~np.isnan()编辑的图形:

要展开@ImportanceOfBeingErnest的答案,可以删除缺少的值,如下所示:

替换

ax2.plot(fh,gfs,'ob-') 

更新:

上述方法可能会改变x轴的顺序。以下是一个解决方法:

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

# make up some fake data
df = pd.DataFrame({"GFS": [np.nan, np.nan, 1, 2, np.nan, 
                          2, 3, np.nan, np.nan, 4], 
                    "RAP": [-2.45832646,  0.56266567, -0.4453474 , 
                            -0.85447845, -1.34830127,
                            -0.38113925, -0.41400397,  
                            np.nan, -0.78764545, -0.02807674]})
fh = np.array(["Fri 4 am", "Fri 6 am","Fri 8 am","Fri 10 am",
                "Fri 6 pm","Fri 10 pm","Sat 4 am","Sat 6 am",
                "Sat 8 am","100az 10 am"
                ])
gfs = df['GFS']
rap = df['RAP']


fig2, ax2 = plt.subplots(figsize=(10,8))
# workaround to set the order of xlabels
ax2.plot(fh, [np.nan]*len(fh)) 
# remove nan's  so that the points are connected
ax2.plot(fh[~np.isnan(gfs)], gfs[~np.isnan(gfs)], "ob-") 
ax2.plot(fh[~np.isnan(rap)],rap[~np.isnan(rap)],marker='x')
ax2.tick_params(which='major',labelsize='12')
ax2.grid(which='major', color='#CCCCCC', linestyle='-')
plt.xticks(rotation='90')
plt.xlabel('Forecast Run')
plt.ylabel('Snowfall Accumulation (in.)')

您需要删除没有数据的点。请不要发布代码或数据的图像。复制并粘贴为文本,然后将其格式化为代码(选择它并键入
ctrl-k
)我尝试使用.ffill()和.dropna()删除“NaN”值,但结果仍然与使用~np.isnan()函数仅打印点的结果相同。然后,使用~np.isnan()函数会导致像上面新编辑的图形一样的问题。使用
~np.isnan()
时,x轴顺序会更改。我已经在更新中发布了解决该问题的方法。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# make up some fake data
df = pd.DataFrame({"GFS": [np.nan, np.nan, 1, 2, np.nan, 
                          2, 3, np.nan, np.nan, 4], 
                    "RAP": [-2.45832646,  0.56266567, -0.4453474 , 
                            -0.85447845, -1.34830127,
                            -0.38113925, -0.41400397,  
                            np.nan, -0.78764545, -0.02807674]})
fh = np.array(["Fri 4 am", "Fri 6 am","Fri 8 am","Fri 10 am",
                "Fri 6 pm","Fri 10 pm","Sat 4 am","Sat 6 am",
                "Sat 8 am","100az 10 am"
                ])
gfs = df['GFS']
rap = df['RAP']


fig2, ax2 = plt.subplots(figsize=(10,8))
# workaround to set the order of xlabels
ax2.plot(fh, [np.nan]*len(fh)) 
# remove nan's  so that the points are connected
ax2.plot(fh[~np.isnan(gfs)], gfs[~np.isnan(gfs)], "ob-") 
ax2.plot(fh[~np.isnan(rap)],rap[~np.isnan(rap)],marker='x')
ax2.tick_params(which='major',labelsize='12')
ax2.grid(which='major', color='#CCCCCC', linestyle='-')
plt.xticks(rotation='90')
plt.xlabel('Forecast Run')
plt.ylabel('Snowfall Accumulation (in.)')