[英]Matplotlib not plotting line between data points
I have a set of snowfall accumulation data that I am plotting from a csv that looks like so:我有一组从 csv 绘制的降雪累积数据,如下所示:
I am attempting to plot the GFS data against the RAP data and the RAP will plot flawlessly with points and lines that connected them.我正在尝试 plot GFS 数据与 RAP 数据和 RAP 将 plot 完美地与连接它们的点和线。 However, the GFS will only plot as point data:但是,GFS 只会将 plot 作为点数据: and I, for the life of me, have not found a way to to plot the GFS data with a line to connect that points.而我,对于我的一生,还没有找到一种方法来 plot 用一条线连接这些点的 GFS 数据。 Here is the code that I have been working with:这是我一直在使用的代码:
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()
Any help and guidance would be greatly appreciated!任何帮助和指导将不胜感激!
To expand on @ImportanceOfBeingErnest's answer, you can remove missing values like this:要扩展@ImportanceOfBeingErnest 的答案,您可以像这样删除缺失值:
Replace代替
ax2.plot(fh,gfs,'ob-')
with和
ax2.plot(fh[~np.isnan(gfs)],gfs[~np.isnan(gfs)],'ob-')
UPDATE:更新:
The above method likely results in changing the order of the x axis.上述方法可能会导致改变 x 轴的顺序。 Here is a workaround:这是一种解决方法:
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.)')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.