1

I have a dataset that looks like this:

       a    b     c     d       e    f      g     h     i       j       k       l  
A   20.5    3.7   2.3   2.2     3.7  2.2    2.4   3.9   2.2     6.4     3.5     3.9
B   14.2    5.5   13.5  14.4    0.8  1.5    12.0  0.5   0.4     1.3     0.2     0.4
C   27.2    0.9   10.2  7.8     6.9  2.6    2.0   15.3  1.0      0.8    2.7     0.6
D   10.8    2.2   1.9   11.6    1.8  6.7    2.6   2.8   0.6      3.5    7.0     4.1
E   19.9    4.8   4.3    2.3    1.1  1.0    0.8   4.9   0.3      1.3    0.9     0.5
F   13.8    12.7  5.0   1.7     2.0  7.9    4.1   1.4   0.5      4.3    0.1     0.5
G   13.3    41.1  4.6   4.7     17.3 4.8    3.4   10.1  35.9    35.1    3.2     3.1
H   24.0    3.8   2.4   1.2      9.0 5.6    3.8    1.3  8.3      1.4    1.4     1.3

(8 row x 12 columns)

How do I plot them using pandas or R in a 8x12 format that would look like a '96-well format' bar chart?

Something like this but better than this excel chart: enter image description here

thanks very much!

  • 1
    Possible duplicate of [ggplot2 3D Bar Plot](https://stackoverflow.com/questions/26794236/ggplot2-3d-bar-plot) – xraynaud Sep 29 '17 at 13:25

2 Answers2

1

To do it easily in R, you need to "melt" your data to long format.

Read data:

dd <- read.table(header=TRUE,row.names=1,
text="
      a    b     c     d       e    f      g     h     i       j       k       l  
A   20.5    3.7   2.3   2.2     3.7  2.2    2.4   3.9   2.2     6.4     3.5     3.9
B   14.2    5.5   13.5  14.4    0.8  1.5    12.0  0.5   0.4     1.3     0.2     0.4
C   27.2    0.9   10.2  7.8     6.9  2.6    2.0   15.3  1.0      0.8    2.7     0.6
D   10.8    2.2   1.9   11.6    1.8  6.7    2.6   2.8   0.6      3.5    7.0     4.1
E   19.9    4.8   4.3    2.3    1.1  1.0    0.8   4.9   0.3      1.3    0.9     0.5
F   13.8    12.7  5.0   1.7     2.0  7.9    4.1   1.4   0.5      4.3    0.1     0.5
G   13.3    41.1  4.6   4.7     17.3 4.8    3.4   10.1  35.9    35.1    3.2     3.1
H   24.0    3.8   2.4   1.2      9.0 5.6    3.8    1.3  8.3      1.4    1.4     1.3
")

Using some tools from the tidyverse (but not using pipes):

library(tidyr)
library(tibble)
library(ggplot2)
ddg <- gather(tibble::rownames_to_column(dd,"row"),
              key=col,value=value,-row)

Now the data look like this:

  row col value
1   A   a  20.5
2   B   a  14.2
3   C   a  27.2
4   D   a  10.8
...

Faceted plot with points (easier to compare magnitudes than in a 3D bar chart):

ggplot(ddg,aes(col,value))+geom_point()+
    facet_wrap(~row,nrow=1)

Still faceted, but using bars rather than points:

ggplot(ddg,aes(col,value))+geom_bar(stat="identity")+
    facet_wrap(~row,nrow=1)

If you want to use intensity (I wouldn't recommend it):

ggplot(ddg,aes(col,row,fill=value))+geom_point(pch=21,size=6)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

Let's try and do it with Python.

First of all, you can use heatmap plot so you get intensity instead of bars with height.
This one can be easily made with Seaborn heatmap:

import pandas as pd
import seaborn as sns
df = pd.read_clipboard()   # just to get this data
sns.heatmap(df)

If you insist on a 3D bar plot (which, if I may express my opinion, is less clear due to plot orientation), you can do the following:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

df = pd.read_clipboard            # just to get the data above

# Now we will need to flat out the matrix and treat it as a 1d vector:
x = list(range(df.shape[0])) * df.shape[1]  # the x axis labels\location
y = list(range(df.shape[1])) * df.shape[0]  # the y axis labels\location
z = [0] * len(X)                            # the initial height of the bars
dx = [1] * len(x)                           # the width of the bar
dy = [1] * len(y)                           # the depth of the bar
dz = df.values.flatten()                    # the bar heights - i.e. the data

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.bar3d(x,y,z,dx,dy,dz)                    # plot in 3d
fig.show()

Hope that helped!

ehudk
  • 510
  • 5
  • 13
  • thanks I will test and let you know, I am very naive to pandas. – Pooran Dewari Sep 29 '17 at 21:26
  • This does not involve high pandas-skills - especially the first solution is quite simple, though the second one does take some understanding in Python's Matplotlib (and its mediocre documentation). Feel free to ask follow ups :-) – ehudk Oct 01 '17 at 20:20