admin管理员组文章数量:1360361
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()
本文标签: pandasSame python script generates different plot for quotsimilarquot csv filesStack Overflow
版权声明:本文标题:pandas - Same python script generates different plot for "similar" csv files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743938114a2565058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论