Search code examples
rhistogrammatlab-figure

Matlab: plotting 3D Histogram


I have a CSV file which has the following structure:

userID, movieID, rating

12,1,3 
13,1,3 
16,1,5 
18,1,5 
20,1,5 
22,1,5 
29,1,5 
54,1,3 
55,1,4 
57,1,4 
58,1,5 
63,1,5 
72,1,4 
73,1,5 
75,1,4 
78,1,3 
79,1,5 
82,1,4 
84,1,5 
96,1,4 
97,1,5 

This is my humble code:

TrainingData = load('ratings.csv'); 
Z = TrainingData(1:5,:);
figure
bar3(Z)
title('Detached Style')

I want to plot for at least 10 rows consisting of the users, movies, and ratings.

The plot would look something like this:

histogram_firstry

I want, as an example, the users on the y-axis, the movies on the x-axis and the ratings on the z-axis.

Could you give me a hint?


Solution

  • Here is my implementation:

    library(plot3D)
    data <- read.csv('data.csv')
    
    x = c(seq(1, dim(data)[1], by=1))
    y = c(seq(1, dim(data)[2], by=1))
    
    zval = c(paste(unlist(t(data)), collapse=","))
    zval = as.integer(as.list(strsplit(zval, ","))[[1]])
    z = matrix(zval, nrow= dim(data)[1], ncol= dim(data)[2], byrow=TRUE)
    
    hist3D(x,y,z, zlim=c(1,100), theta=120, phi=40, axes=TRUE,label=TRUE, nticks=5, ticktype="detailed", space=0.4, lighting=TRUE, light="diffuse", shade=0.5)
    

    In the above code, x-axis represents the rows, y-axis represents columns in the data and z-axis represents the corresponding values. The leftmost column is the first column in your data, followed by middle one and then the rightmost one.

    Output Image: enter image description here

    There is always a scope of improvement but this will certainly give you an idea of how to do this.

    I hope this will help you.