-1

I need a scattter plot containing two different sets of data.

ex:

tc1 = [24,25,26,27]
tc2 = [28,29,31,33]
t   = [1,2,3,4]

I need to plot (t,tc1) and (t,tc2) on a scatter plot with two different legend labels. "thermocouple 1" and "thermocouple 2"

with the axis labels being:

plt.xlabel('Time(seconds)')
plt.ylabel('Temperature(Celsius)')
plt.title('Voltage Over Time')
Paul K
  • 11
  • 3

1 Answers1

1

Matplotlib.pylot

Plot both variables with plt.scatter, add a label parameter, then call the legend with plt.legend:

tc1 = [24,25,26,27]
tc2 = [28,29,31,33]
t   = [1,2,3,4]

plt.scatter(t, tc1, label = 'thermocouple 1', color = 'blue')
plt.scatter(t, tc2, label = 'thermocouple 2', color = 'red')
plt.xlabel('Time(seconds)')
plt.ylabel('Temperature(Celsius)')
plt.title('Voltage Over Time')
plt.legend()
plt.show()

enter image description here


Seaborn:

Seaborn has a nice built in hue feature that does the heavy lifting for you:

import seaborn as sns
df = pd.DataFrame(zip(tc1, tc2, t), columns = ['thermocouple 1', 'thermocouple 2', 'time']).melt('time')
sns.scatterplot(df, x = 'time', y = 'value', hue = 'variable')
plt.xlabel('Time(seconds)')
plt.ylabel('Temperature(Celsius)')
plt.title('Voltage Over Time')
plt.legend(title = '')
plt.show()

enter image description here


Pandas Plot:

Using Pandas, you can use plot.line and pivot to change the color based on the different variable (thermocouple 1 vs thermocouple 2):

(pd.DataFrame(zip(tc1, tc2, t), columns = ['thermocouple 1', 'thermocouple 2', 'time'])
 .melt('time')
 .pivot(columns='variable', index='time', values='value') # pivot is needed to add colors based on the variable
 .plot.line(style = '.', markersize = 20,                 # change the line style to just show markers (dots)
            title = 'Voltage Over Time', 
            xlabel = 'Time(seconds)', 
            ylabel = 'Temperature(Celsius)')
 .legend())                                               # Remove legend title
plt.show()

enter image description here

Michael S.
  • 3,050
  • 4
  • 19
  • 34