使用 matplotlib.pyplot.plot_date 的子图
Subplot using matplotlib.pyplot.plot_date
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 块 "plotting" 代码与上面的相同。
目标是让子图中的所有图共享相同的 x 轴。
我不确定如何处理这个子图,因为我之前的许多尝试都没有奏效。
旁注:我试过用其他方法绘制,但这是唯一正确绘制时间戳的方法。
尝试使用:
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
对象(ax1
、ax2
、ax3
)而不是让 pyplot 弄清楚或提取当前 Axes
和 Figure
对象。对于您的其他子图,请使用 ax2
和 ax3
或改为:
fig, axn = plt.subplots(3, 1, sharex=True)
并循环 axn
.
此外,如果您正在使用 pandas,您可以将 plot_date
命令替换为 df['column'].plot(ax=ax1, lw=2)
,并跳过时间戳准备工作。
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 块 "plotting" 代码与上面的相同。 目标是让子图中的所有图共享相同的 x 轴。 我不确定如何处理这个子图,因为我之前的许多尝试都没有奏效。
旁注:我试过用其他方法绘制,但这是唯一正确绘制时间戳的方法。
尝试使用:
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
对象(ax1
、ax2
、ax3
)而不是让 pyplot 弄清楚或提取当前 Axes
和 Figure
对象。对于您的其他子图,请使用 ax2
和 ax3
或改为:
fig, axn = plt.subplots(3, 1, sharex=True)
并循环 axn
.
此外,如果您正在使用 pandas,您可以将 plot_date
命令替换为 df['column'].plot(ax=ax1, lw=2)
,并跳过时间戳准备工作。