Alex Rebell
Alex Rebell

Reputation: 374

Compare two arrays on a graph

I have two two-dimensional arrays with data, these arrays have the same dimension. Arrays are saved to a file .CSV

Example of an array:

enter image description here

delimiter - ;

Could you tell me please, how can I use some graph to show the difference between these two arrays? You can offer any type of chart. I would like something like 3D graphics.

thank you very much!

Upvotes: 0

Views: 563

Answers (1)

Stef
Stef

Reputation: 30579

I don't think a 3D scatterplot will help you to see the difference between the two data files, but if you nevertheless want to show the data in a scatterplot you can do it as follows:

import matplotlib.pyplot as plt
import numpy as np

a1 = np.loadtxt('a1.csv', dtype=int, delimiter=';')
a2 = np.loadtxt('a2.csv', dtype=int, delimiter=';')

ind = np.indices(a1.shape)  # a1 and a2 have same shape

fig, ax = plt.subplots(ncols=2, subplot_kw=dict(projection='3d'))
ax[0].scatter(ind[0], ind[1], a1.flatten())
ax[1].scatter(ind[0], ind[1], a2.flatten())

enter image description here

However, as I wrote in the comment above, I'd recommend using matshow with a diverging colormap to show the difference of the two arrays:

plt.matshow(a1-a2, cmap='bwr')
plt.colorbar()

enter image description here

Or, if you don't like differences, you can show both arrays side by side:

fig, ax = plt.subplots(ncols=2)

m1 = ax[0].matshow(a1, cmap='bwr')
fig.colorbar(m1, ax=ax[0], location='bottom')
m2 = ax[1].matshow(a2, cmap='bwr')
fig.colorbar(m2, ax=ax[1], location='bottom')

enter image description here


PS: I created the sample data with
np.random.seed(0)
a1 = np.random.randint(20, 50, (13, 8))
a2 = np.random.randint(20, 50, (13, 8))
np.savetxt('a1.csv', a1, '%d', delimiter=';')
np.savetxt('a2.csv', a1, '%d', delimiter=';')


Update as requested in comments below:

  1. a1 and a2 in one plot with different colors
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.scatter(ind[0], ind[1], a1.flatten(), c='C0')
ax.scatter(ind[0], ind[1], a2.flatten(), c='C1')
  1. a1 and a2 in two wireframe plots
fig, ax = plt.subplots(ncols=2, subplot_kw=dict(projection='3d'))
ax[0].plot_wireframe(ind[0], ind[1], a1, color='C0')
ax[1].plot_wireframe(ind[0], ind[1], a2, color='C1')
  1. a1 and a2 in one wireframe plot
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.plot_wireframe(ind[0], ind[1], a1, color='C0')
ax.plot_wireframe(ind[0], ind[1], a2, color='C1')

Upvotes: 1

Related Questions