gpt4 book ai didi

python - 使用 matplotlib.pyplot.plot_date 的子图

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:31 30 4
gpt4 key购买 nike

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 block 与上面相同的“绘图”代码。目标是让子图中的所有图共享相同的 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 对象(ax1ax2ax3)而不是让 pyplot 来计算它或提取当前的 Axes 和Figure 对象。对于其他子图,请使用 ax2ax3 或执行以下操作:

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

并循环axn

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

关于python - 使用 matplotlib.pyplot.plot_date 的子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41023622/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com