最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pandas - Same python script generates different plot for "similar" csv files - Stack Overflow

programmeradmin3浏览0评论

I had prepared a python script to generate some automatic plots from some csv files. But recently I came across an issue when passing one of the newer csv files, and generated plots change a lot. Besides changes in size etc, there are 2 noticeable changes. One, one of the subplot is generated blank (which is solved by either plotting the "WindSpeeds" at the end, or by commenting twinx() off for the 2nd subplot (why?!!)). And 2nd, the x-ticks. I was not able to get rid of default x-ticks and had made peace with it, but now they are gone and x-ticks appear as I would like them to be. I'm really curious to understand what changed in the data or elsewhere that caused the plot to be generated differently and how can I plot in a more "controlled" way? And for that sake, I'm also providing link to get the csv files here. Here is the relevant part of my script

PS : ts in the snippet below is the timestamp that appears in the name of the csv

wind_df = pd.read_csv(os.path.join(csv_folder, wind_csv), index_col="Timestamp")
ship_df = pd.read_csv(os.path.join(csv_folder, ship_csv), index_col="Timestamp")
wind_df.index = pd.to_datetime(wind_df.index)
ship_df.index = pd.to_datetime(ship_df.index)

plt_ts = pd.to_datetime(ts, format="%y%m%d_%H%M%S")

fig, ax = plt.subplots(2, sharex=True)
fig.subplots_adjust(hspace=0.08)

wind_df[["WindSpeedSB+(m/s)","WindSpeedPS+(m/s)", "WindSpeedRelative+(m/s)"]].interpolate().plot(ax=ax[0])
ship_df["SpeedOverGround+(kn)"].interpolate().plot(ax=ax[0])
ship_df["SpeedThroughWater+(kn)"].interpolate().plot(ax=ax[0])
ax[0].hlines(25, xmin=0, xmax=1, transform=ax[0].get_yaxis_transform(),linestyles=":", colors='black')
ax[0].legend(bbox_to_anchor=(-0.03, 0.8))


ax1 = ax[1].twinx()
ship_df["CourseTrue+(deg)"].interpolate().plot(ax=ax1, c="red")
ship_df["HeadingTrue+(deg)"].interpolate().plot(ax=ax1, c="purple")
wind_df[["WindDirectionSB+(deg)","WindDirectionPS+(deg)","WindAngleRelative+(deg)"]].interpolate().plot(ax=ax[1])
ax[1].legend(bbox_to_anchor=(-0.03, 0.8))
ax1.legend(bbox_to_anchor=(1.14, 0.8))

for i in range(len(ax)):
    ax[i].vlines(plt_ts, ymax=1, ymin=0, transform=ax[i].get_xaxis_transform(), linestyles=":", colors='black')
x_ticks_times = pd.date_range(
    wind_df.index.min(),
    wind_df.index.max(),
    freq="30s"
)
x_ticks_labels = [time.strftime("%H:%M:%S") for time in x_ticks_times]
ax[1].set_xticks(x_ticks_times)
ax[1].set_xticklabels(x_ticks_labels, rotation=90)

plt.show()

发布评论

评论列表(0)

  1. 暂无评论