0

I have a dataframe from CSV data (an matrix) looks like this:

    BS1 BS2 BS3 BS4 BS5 BS6 BS7 BS8 BS9
BS1 0   1   6   1   0   0   0   0   0
BS2 1   0   8   1   0   0   0   0   0
BS3 0   6   0   2   3   1   0   0   0
BS4 0   0   4   0   1   2   0   0   0
BS5 0   1   3   2   0   3   0   0   0
BS6 0   0   0   0   0   0   5   4   2
BS7 0   4   7   3   4   0   0   5   6
BS8 0   0   0   0   0   0   5   0   7
BS9 0   1   5   0   1   0   4   0   0

I want to make a 3D chart based on this CSV matrix data. However, the XY is string. How to do it?

the expected result looks like this link 3D Scatterplot with strings in Python

thank you.

Arief Hidayat
  • 937
  • 1
  • 8
  • 19

1 Answers1

1

For each value to plot, the scatter function needs x-, y- and z-values. Try this:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from io import StringIO
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Assuming data is imported as pandas dataframe
io = StringIO('''    BS1 BS2 BS3 BS4 BS5 BS6 BS7 BS8 BS9
BS1 0   1   6   1   0   0   0   0   0
BS2 1   0   8   1   0   0   0   0   0
BS3 0   6   0   2   3   1   0   0   0
BS4 0   0   4   0   1   2   0   0   0
BS5 0   1   3   2   0   3   0   0   0
BS6 0   0   0   0   0   0   5   4   2
BS7 0   4   7   3   4   0   0   5   6
BS8 0   0   0   0   0   0   5   0   7
BS9 0   1   5   0   1   0   4   0   0''')
df = pd.read_csv(io, sep='\s+')

# Get columns, rows and values from dataframe
xs = df.columns
ys = df.index
zs = df.values

# Prepare coordinates
x = np.linspace(0, len(xs) - 1, len(xs))
y = np.linspace(0, len(ys) - 1, len(ys))
xv, yv = np.meshgrid(x,y)

ax.scatter(xv, yv, zs)
ax.set(xticks=range(len(xs)), xticklabels=xs,
        yticks=range(len(ys)), yticklabels=ys) 
WolfgangK
  • 953
  • 11
  • 18
  • Isn't this hard-coding the axis? – Axois Jul 29 '19 at 08:26
  • Is it possible to use csv data? because I have a lot of CSV matrix data,,, – Arief Hidayat Jul 29 '19 at 08:38
  • @WolfgangK I got this message `could not convert string to float: ` – Arief Hidayat Jul 29 '19 at 09:21
  • @WolfgangK the error is on this part `zs = np.loadtxt('data.csv', delimiter=',')` – Arief Hidayat Jul 29 '19 at 09:22
  • @Arief So if your data is already imported as a pandas dataframe, things become easier – WolfgangK Jul 29 '19 at 10:11
  • @WolfgangK nice, I put this code for my data `df = pd.read_csv('data.csv',index_col=0)` and works perfectly. Is it possible to add `figsize=(20,10)`, `label`, `labelsize=30` and `fontsize=30`? – Arief Hidayat Jul 30 '19 at 01:52
  • 1
    @Arief The primary question of how to create a 3D scatterplot with categorical/string XY axis labels was already solved in the first iteration of the answer. The follow-up problems that were presented afterwards in the comments are specific to your files and formatting needs and should imo not be part of the answer, since these problems are also not reflected in the question. If you need help with these topics, I suggest you give a complete problem description in the first place or open up a new question. – WolfgangK Jul 30 '19 at 08:00