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()

I'm trying to create a subplot with 3 rows and 1 column of plots. I have 3 blocks of "plotting" code identical to the one above. The goal is to have all the plots within the subplot to share the same x-axis. I'm unsure how to approach this subplot as my many previous attempts haven't worked at all.

Sidenote: I've tried plotting with other methods but this is the only one that correctly plots the timestamps.

1 Answer 1

1

Try using:

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()

I think it is better to work directly with the Axes objects (ax1, ax2, ax3) instead of getting pyplot to figure it out or extracting the current Axes and Figure objects. For your other subplots use ax2 and ax3 or do this instead:

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

and loop over axn.

Also, if you're using pandas anyways you can replace the plot_date commands with df['column'].plot(ax=ax1, lw=2) and skip the time stamp prep work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.