0

I am trying to create a scatter sub plot with two different variable for the y-axis. The data is imported from an excel sheet using pandas. The x-axis is time elapsed while one of the y-axes is temperature and the other is pressure. Here is the code that I used:

fig,cx = plt.subplots(nrows = 1, ncols = 4, figsize = (15,5))

cx[0].scatter(Helium1['ΔT(s)'], Helium1['Pref (Bar)'], marker = 'x')
cx[0].set_xlabel('ΔT(s)')
cx[0].set_ylabel('Pref (Bar)')
cx[0].set_title('Uncorrected Pressure')
cx[0].tick_params(axis='y', labelcolor='black')

cx1 = cx.twinx()

cx1[0].scatter(Helium1['ΔT(s)'], Helium1['Temp(°C)'], marker = 'x')
cx1[0].set_xlabel('ΔT(s)')
cx1[0].set_ylabel('Pref (Bar)')
cx1[0].set_title('Uncorrected Pressure')
cx1[0].tick_params(axis='y', labelcolor='black')

cx[1].scatter(Helium1['ΔT(s)'],Helium1['Pref,t (Bar)'], marker = 'v', color = 'orange')
cx[1].set_xlabel('ΔT(s)')
cx[1].set_ylabel('Pref,t (Bar)')
cx[1].set_title('Temperature corrected Pressure')
cx[1].tick_params(axis='y', labelcolor='black')

cx[2].scatter(Helium1['ΔT(s)'],Helium1['Pref,l (Bar)'], marker = 'v', color = 'Yellow')
cx[2].set_xlabel('ΔT(s)')
cx[2].set_ylabel('Pref,l (Bar)')
cx[2].set_title('Temperature corrected Pressure')
cx[2].tick_params(axis='y', labelcolor='black')

cx[2].scatter(Helium1['ΔT(s)'],Helium1['Pref,t (Bar)'], marker = 'v', color = 'Orange')
cx[2].set_xlabel('ΔT(s)')
cx[2].set_ylabel('Pref,t (Bar)')
cx[2].set_title('Leak Correction')
cx[2].tick_params(axis='y', labelcolor='black')

cx[3].scatter(Helium1['ΔT(s)'],Helium1['Pref,l (Bar)'], marker = 'v', color = 'Yellow')
cx[3].set_xlabel('ΔT(s)')
cx[3].set_ylabel('Pref,l (Bar)')
cx[3].set_title('Leak Corrected Pressure')
cx[3].tick_params(axis='y', labelcolor='black')

However when I run the code I get the error message: 'numpy.ndarray' object has no attribute 'twinx'

I would appreciate any help with this.

Ashtu
  • 35
  • 5

1 Answers1

1

numpy.ndarray object has no attribute 'twinx'" indicates that you are trying to use the twinx() method on a NumPy array instead of a matplotlib Axes object. In your code, cx is an array of axes objects, so you need to access each individual axes object to use the twinx() method on it.

use this code code:

import matplotlib.pyplot as plt
import pandas as pd


fig, cx = plt.subplots(nrows=1, ncols=4, figsize=(15, 5))

cx[0].scatter(Helium1['ΔT(s)'], Helium1['Pref (Bar)'], marker='x')
cx[0].set_xlabel('ΔT(s)')
cx[0].set_ylabel('Pref (Bar)')
cx[0].set_title('Uncorrected Pressure')
cx[0].tick_params(axis='y', labelcolor='black')

cx1 = cx[0].twinx()  

cx1.scatter(Helium1['ΔT(s)'], Helium1['Temp(°C)'], marker='x')
cx1.set_ylabel('Temperature (°C)', color='red')  
cx1.tick_params(axis='y', labelcolor='red')

cx[1].scatter(Helium1['ΔT(s)'], Helium1['Pref,t (Bar)'], marker='v', color='orange')
cx[1].set_xlabel('ΔT(s)')
cx[1].set_ylabel('Pref,t (Bar)')
cx[1].set_title('Temperature corrected Pressure')
cx[1].tick_params(axis='y', labelcolor='black')

cx[2].scatter(Helium1['ΔT(s)'], Helium1['Pref,l (Bar)'], marker='v', color='yellow')
cx[2].set_xlabel('ΔT(s)')
cx[2].set_ylabel('Pref,l (Bar)')
cx[2].set_title('Temperature corrected Pressure')
cx[2].tick_params(axis='y', labelcolor='black')

cx[3].scatter(Helium1['ΔT(s)'], Helium1['Pref,t (Bar)'], marker='v', color='orange')
cx[3].set_xlabel('ΔT(s)')
cx[3].set_ylabel('Pref,t (Bar)')
cx[3].set_title('Leak Correction')
cx[3].tick_params(axis='y', labelcolor='black')

cx[3].scatter(Helium1['ΔT(s)'], Helium1['Pref,l (Bar)'], marker='v', color='yellow')
cx[3].set_xlabel('ΔT(s)')
cx[3].set_ylabel('Pref,l (Bar)')
cx[3].set_title('Leak Corrected Pressure')
cx[3].tick_params(axis='y', labelcolor='black')

plt.tight_layout() 
plt.show()

I changed the label color for the temperature y-axis to red to differentiate it from the primary y-axis. The rest of the code remains the same, and plt.tight_layout() is added to improve the spacing between subplots.