1

I would like to create a 3D surface with the data I got in an analysis. I have 18 samples (t0, t1, ...) to which an absorbance signal is linked at each wavelength (from 300 to 800nm approximately).

My problem is that while entering the data through pandas I can't plot anything and I get an error ms:

shape mismatch: objects cannot be broadcast to a single shape

What am I supposed to do or where am I wrong?

(ps. I am attaching the image with data series to make the problem clearer)

** I ruled out the nan problem in the last column by writing the array by hand so that's not the problem, but if you know how to remove the last n values from an array, I'd like to know too.

image:

enter image description here

Code:

import pandas as pd
import ipywidgets as widgets

f="3d-chart.xlsx"
data = pd.ExcelFile(f) 

shet=widgets.Dropdown(
    options=data.sheet_names,
    description='Data sheet:',
    disabled=False,
)
display(shet)

data=pd.read_excel(f,sheet_name=shet.value)
data #for show data database

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np

fig, ax = plt.subplots(subplot_kw={"projection": "3d"}, figsize=(15,15))

# Make data.
X = np.array(data[data.columns[0]])
#Y = np.array(data[data.columns[19]])
#X, Y = np.meshgrid(X, Y)
Z = np.array([(data[data.columns[i]])for i in range (1,18)])

# Plot the surface.
surf = ax.bar(X, Y, Z, cmap=cm.coolwarm,linewidth=3, antialiased=False)

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

and the error was:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

onlyphantom
  • 8,606
  • 4
  • 44
  • 58
  • 1
    You should upload your images to a public image sharing service if you don't have enough privilege to include the image on SO yet. I would do the edits for you and include those images, but your google drive links are restricted access, so there's no way to help. – onlyphantom Feb 14 '21 at 10:35
  • Yes, I don't know this problem but I listen your idea, then I change the URLs image. – Matteo Griot Feb 14 '21 at 11:24
  • Does [this](https://stackoverflow.com/questions/9170838/surface-plots-in-matplotlib) answer your question ? – Jay Patel Feb 16 '21 at 03:07
  • I have already tried to read other stackoverflow questions and answers similar to mine but in my case I don't have three columns of data (X, Y, Z) but I have two columns (X, Y) and then a matrix with the values of Z already computed for X and Y. Also all the examples I've read use `np.linearspace()` but I don't want to create new data, I want to use exactly what I have. Should I attach the database to better understand what I want to do? – Matteo Griot Feb 16 '21 at 07:31
  • I attach image of database: https://ibb.co/brf5HYM – Matteo Griot Feb 16 '21 at 07:42

0 Answers0