简体   繁体   English

在 matplotlib 上用两个不同的时间/x 轴绘制数据?

[英]Plotting with two data different time/x-axis on matplotlib?

My aim is to generate something like this我的目标是产生这样的东西目标 , but my try looks like this ,但我的尝试看起来像这样我的输出

The problem is I am using two CSV data and even though their time range is the same, one of them has many time intervals like around 2399 and the other one is 73. I'm using sharex="All" command and whatever I can found from internet as well, but it gives runtime error "Locator attempting to generate 119857 ticks ([-113.5, ..., 2383.5]), which exceeds Locator.MAXTICKS (1000)"问题是我正在使用两个 CSV 数据,尽管它们的时间范围相同,但其中一个有很多时间间隔,比如 2399 左右,另一个是 73。我正在使用 sharex="All" 命令和任何我能也从互联网上找到,但它给出运行时错误“定位器尝试生成 119857 个刻度([-113.5,...,2383.5]),超过 Locator.MAXTICKS(1000)”

My output generating code is following :我的输出生成代码如下:

fig, (ax1, ax2,ax3,ax4) = plt.subplots(4, 1,figsize=(10,7), sharex="all")
fig.subplots_adjust(bottom=0.2)

ax1.plot( df.time, df['B'], color='k')
ax1.plot( df.time, df['Bx'], color='r')
ax1.plot( df.time, df['By'], color='b')
ax1.plot( df.time, df['Bz'], color='g')
ax1.xaxis.grid(True,alpha=0.3)
ax1.set_ylabel('Bx,By,Bz,B[nT]')

ax2.plot(df1.time, df1['v_total'],color='k')
ax2.plot(df1.time, df1['Vx'],color='r')
ax2.plot(df1.time, df1['Vy'],color='b')
ax2.plot(df1.time, df1['Vz'],color='g')
ax2.xaxis.grid(True,alpha=0.3)
ax2.set_ylabel('Vx,Vy,Vz,V[km/s]')

ax3.plot(df1.time, df1['n'],color='k')
ax3.xaxis.grid(True,alpha=0.3)
ax3.set_ylabel('Np[1/cm^3]')

ax4.plot(df1.time, df1['T'],color='k')
ax4.xaxis.grid(True,alpha=0.3)
ax4.set_ylabel('T[k]')

minutes = mdates.MinuteLocator(byminute=np.arange(0,60,15),interval=2)
minutes_fmt = mdates.DateFormatter('%H:%M')
ax4.xaxis.set_major_locator(minutes)

ax4.xaxis.set_major_formatter(minutes_fmt)

ax4.tick_params(axis='x', labelrotation=45)

fig.suptitle('FF shock May 07 2007')
plt.savefig('WindIP.png')
plt.savefig('WindIP.pdf')
plt.show()

My two data are: https://drive.google.com/file/d/1uqmynE78dMM_23ITVBsdu7-5tPbnQ9gC/view?usp=sharing https://drive.google.com/file/d/17kj8MM-agNkS22J5sRGGicdQHCK5EGYL/view?usp=sharing我的两个数据是: https://drive.google.com/file/d/1uqmynE78dMM_23ITVBsdu7-5tPbnQ9gC/view?usp=sharing https://drive.google.com/file/d/17kj8MM-agNkS22J5sRGGicdQHCK5EGYL/view?usp=分享

I used following command to change from epoch to time我使用以下命令从时代更改为时间

df['epoch'] = pd.to_datetime(df.epoch, format='%Y-%m-%dT%H:%M:%S.%f')
df['time'] = df['epoch'].dt.strftime('%H:%M:%S')

df1['epoch'] = pd.to_datetime(df1.epoch, format='%Y-%m-%dT%H:%M:%S.%f')
df1['time'] = df1['epoch'].dt.strftime('%H:%M:%S')

and for T value对于 T 值

m_p=1.6726219*10** -27
K_b=1.38064852*10** -23
df1['nVth']=1000*df1['V_th']
df1['T']=(df1['nVth']**2*m_p)/(2*K_b)

So, how to reduce ticks as well as align two time intervals and show one time interval as x-axis?那么,如何减少刻度以及对齐两个时间间隔并将一个时间间隔显示为 x 轴? And another minor problem is how put red vertical line like in the my aim figure?另一个小问题是如何在我的目标图中放置红色垂直线? I tried axvline, but no success.我试过axvline,但没有成功。 Thanks for your consideration and help.感谢您的考虑和帮助。

Moving to an answer so I can write more clearly, you can merge the dataframes like this (taking from your question that epoch is the same in both and pd.to_datetime() has already been applied):转到一个答案,以便我可以更清楚地写出来,您可以像这样合并数据帧(从您的问题中得出,两者的epoch相同,并且pd.to_datetime()已经应用):

df_merged = df.set_index("epoch").merge(df1.set_index("epoch"),
                                        left_index=True, right_index=True,
                                        how="outer")

Then using df_merged.index as the x-axis, and df_merged["B"] etc. as the columns, everything should align.然后使用df_merged.index作为 x 轴,使用df_merged["B"]等作为列,一切都应该对齐。 (EDIT: thinking this through, it might be easier to merge on the time column, as this will not be so specific, so will not create duplicate rows for the two dataframes for the same day.) (编辑:考虑到这一点,在time列上合并可能更容易,因为这不会那么具体,所以不会为同一天的两个数据框创建重复的行。)

Then again for the line across all charts, the answer from @bb1 here gives the answer, by using:然后再次对于所有图表中的线,@bb1 的答案在这里给出了答案,通过使用:

gs = fig.add_gridspec(3, 2)

# ...

# background axes object for plotting the vertical line
ax =  fig.add_subplot(gs[:, :], sharex = ax1)
# set background color to transparent and turn off the frame
ax.patch.set_alpha(0)
ax.axis("off")
# plot the vertical line
ax.axvline(20, c='r')

You can play around with this to get it to suit your specific requirements.您可以使用它来满足您的特定要求。 You can easily duplicate the last line and use ls="--" to plot the dashed lines too!您也可以轻松复制最后一行并使用ls="--"来绘制虚线!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM