Python Forum
Add space between plots - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Add space between plots (/thread-18392.html)



Add space between plots - AdWill97 - May-15-2019

Hello I am plotting 4 different plots in pandas. 3 of which have the same x axis scale, the last one being different. when i run the code and it shows the plots they all come out the way they should except for that the x axis of the 3rd plot should be visible but is not because it is being printed too close to the 4th one. the image below will give a better idea.

[Image: search?q=4+pandas+subplots&rlz=1C1CHBF_e...GggrqBB-M:]

To use this image as an example my plots are only showing the x axis values Jan, Jul etc. on the last plot but the 3 graphs above it are temperature.
i need enough space between the 3rd and 4th plots so that i can see the x axis values on the 3rd one to represent the first 3. Any help appreciated.
my code is below:

import pandas as pd
import matplotlib.pyplot as plt


df = pd.read_csv('C:\Adam Internship\CT_TESTS\CoolingTowerTest1CSV.csv')

df = df.drop([0,1,2,3])              ##   droppping the first 4 rows as they are empty          

new_header = df.iloc[0]              ##   grabbing row 0
df = df[1:]                          ##   take the data less the header row
df.columns = new_header              ## set header row as the df header

df = df.reset_index()                   # reseting the index
df = df.drop(columns=['index'])         # dropping row that appeared?
df['Time'] = pd.to_datetime(df.Time)                        # Converts Time to a pandas date time 
df['Time'] = pd.Series([val.time() for val in df['Time']])  # Ensures the time is plotted without the date


df['\\\PXI-PC\KWKK\PXI-PC\KWKK_sv\OC_HC_B_T_M__FL'] = df.rename(columns = {'\\\PXI-PC\KWKK\PXI-PC\KWKK_sv\OC_HC_B_T_M__FL':'fl_temp'}, inplace=True)
df['\\\PXI-PC\KWKK\PXI-PC\KWKK_sv\AUX_HC_A_T_M__'] = df.rename(columns = {'\\\PXI-PC\KWKK\PXI-PC\KWKK_sv\AUX_HC_A_T_M__':'amb_temp'}, inplace=True)
df['\\\PXI-PC\KWKK\PXI-PC\KWKK_sv\OC_HC_B_PSET_O__'] = df.rename(columns = {'\\\PXI-PC\KWKK\PXI-PC\KWKK_sv\OC_HC_B_PSET_O__':'voltage'}, inplace=True)
#Above is renaming the variables i need to use to make it easier to plot. a backslash is added as python sees it as escape otherwise

df['fl_temp'] = df['fl_temp'].astype('float64')         # converting feed line temp to a float
df['amb_temp'] = df['amb_temp'].astype('float64')       # converting ambient temp to float
df['voltage'] = df['voltage'].astype('float64')

fig = plt.figure()

ax1 = fig.add_subplot(411)
# =============================================================================
ax2 = fig.add_subplot(412)
ax3 = fig.add_subplot(413)
ax4 = fig.add_subplot(414)
# =============================================================================


ax1.plot(df['Time'], df['fl_temp'])  #,df['Time'], df['amb_temp'])
ax1.plot(df['Time'], df['amb_temp'])
plt.gcf().autofmt_xdate()
ax1.yaxis.set_label_position("left")
plt.xlabel('Time')
ax1.set_ylabel('Temp')
ax1v = ax1.twinx()
ax1v.plot(df['Time'], df['voltage'], color = 'r')
ax1v.set_ylabel('Voltage')
plt.grid(True)


ax2.plot(df['Time'], df['fl_temp'])  #,df['Time'], df['amb_temp'])
ax2.plot(df['Time'], df['amb_temp'])
plt.gcf().autofmt_xdate()
plt.xlabel('Time')
ax2.set_ylabel('Temp')

ax2v = ax2.twinx()
ax2v.plot(df['Time'], df['voltage'], color = 'r')
ax2v.set_ylabel('Voltage')
plt.grid(True)


ax3.plot(df['Time'], df['fl_temp'])  #,df['Time'], df['amb_temp'])
ax3.plot(df['Time'], df['amb_temp'])
plt.gcf().autofmt_xdate()
plt.xlabel('Time')
ax3.set_ylabel('Temp')

ax3v = ax3.twinx()
ax3v.plot(df['Time'], df['voltage'], color = 'r')
ax3v.set_ylabel('Voltage')
plt.grid(True)


ax4.plot(df['amb_temp'], df['voltage'])  #,df['Time'], df['amb_temp'])
plt.gcf().autofmt_xdate()
plt.xlabel('Abient Temp.')
ax4.set_ylabel('Voltage')
plt.grid(True)