0

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!

Alex Rebell
  • 465
  • 3
  • 15
  • matplotlib [`matshow`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.matshow.html): read the csv's into a numpy array and display the difference – Stef Jun 16 '22 at 15:49
  • @Stef, I was thinking something like this: [link](https://stackoverflow.com/questions/12414619/creating-a-3d-plot-from-a-3d-numpy-array) but I just can't figure out how to upload my csv file ( – Alex Rebell Jun 16 '22 at 15:57
  • load it like `a1 = np.loadtxt('a1.csv', dtype=int, delimiter=';')` where np comes from `import numpy as np` – Stef Jun 16 '22 at 16:07
  • how do I represent it in this expression? `ax.scatter(x, y, -z, zdir='z', c= 'red')` tell me, please. – Alex Rebell Jun 16 '22 at 16:38

1 Answers1

1

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')
Stef
  • 28,728
  • 2
  • 24
  • 52
  • That's great! Could you tell me please, is it possible somehow in the first example, where 3D graphs, to display everything on one graph, i.e. `a1` and `a2` in one graph, only in different colors? – Alex Rebell Jun 16 '22 at 20:37
  • maybe there is an opportunity to try the `plot_wireframe` graph type? both separately and together. Could you tell me please – Alex Rebell Jun 16 '22 at 20:48
  • My last question to you, and I will definitely fall behind. Could you tell me please, is it possible to somehow display these two arrays on a 3D histogram, as shown here: [link](https://stackoverflow.com/a/6873956/13023647) only here it's not 3D, but just shows a way to combine the two values – Alex Rebell Jun 16 '22 at 21:11
  • 1
    for the first two questions see updated answer, for the last question (3D histogram) please ask a separate question – Stef Jun 17 '22 at 07:49