子批次使用matplotlib.pyplot.plot\

2024-09-28 20:34:49 发布

您现在位置:Python中文网/ 问答频道 /正文

import numpy as np
import pandas as pan
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import csv 
import datetime 

timestamp1 = []
for t in ts_temp1:
    timestamp1.append(mdates.datestr2num(t))
formatter = mdates.DateFormatter('%Y-%b-%d')

plt.plot_date(timestamp1,xgtemp1,'r.',label='X GALVO 1',lw=2)
plt.plot_date(timestamp1,ygtemp1,'b.',label='Y GALVO 1',lw=2)

ax = plt.gcf().axes[0] 
ax.xaxis.set_major_formatter(formatter)
plt.gcf().autofmt_xdate(rotation=25)
plt.ylabel('Galvo Temperatures (°C)')
plt.grid()
plt.legend(loc='upper right')
plt.show()

我正在尝试创建一个包含3行和1列绘图的子图。 我有3块“绘图”代码与上面的代码相同。 目标是让子图中的所有绘图共享同一x轴。 我不确定如何处理这个子批次,因为我以前的许多尝试都没有成功。在

旁注:我尝试过用其他方法绘制,但这是唯一一个正确绘制时间戳的方法。在


Tags: import绘图dateplotmatplotlibformatterasplt
1条回答
网友
1楼 · 发布于 2024-09-28 20:34:49

尝试使用:

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)

ax1.plot_date(timestamp1,xgtemp1,'r.',label='X GALVO 1',lw=2)
ax1.plot_date(timestamp1,ygtemp1,'b.',label='Y GALVO 1',lw=2)

ax1.xaxis.set_major_formatter(formatter)
fig.autofmt_xdate(rotation=25)

ax.set_ylabel('Galvo Temperatures (°C)')
ax.grid()
ax.legend(loc='upper right')

fig.show()

我认为直接使用Axes对象(ax1ax2ax3)而不是让pyplot来计算它或者提取当前的Axes和{}对象。对于其他子批次,请使用ax2ax3或执行以下操作:

fig, axn = plt.subplots(3, 1, sharex=True)

并在axn上循环。在

另外,如果您正在使用pandas,您可以将plot_date命令替换为df['column'].plot(ax=ax1, lw=2),并跳过时间戳准备工作。在

相关问题 更多 >