MichaelA
MichaelA

Reputation: 1986

Create a carpetplot with discrete values

I'd like to create a carpetplot with discrete values. For example I have these values:

import pandas as pd
import pylab as plt
df_data = pd.DataFrame(
    [[1, 2, 1], [1, 1, 3], [2, 2, 5], [3, 2, 1]], index=['n1', 'n2', 'n3', 'n4'], columns=['var1', 'var2', 'var3'])

I have also a dictionary to match these discrete values to a color:

    matcher_dict = {
    1: (236, 99, 92), 2: (75, 129, 196), 3: (244, 153, 97), 5: (135, 104, 180)}

I'd now like to create a carpet plot, and I thought imshow could be a way to work this out, as the documentation for imshow says

cmap : Colormap, optional, default: None

If None, default to rc image.cmap value. cmap is ignored when X has RGB(A) information

So I create a new Dataframe with the colors as entries:

df_color = pd.DataFrame(index=df_data.index, columns=df_data.columns)
for col_index, col in enumerate(df_data.iteritems()):
    for row_index, value in enumerate(col[1]):
        df_color.ix[row_index].ix[col_index] = matcher_dict[
            df_data.ix[row_index].ix[col_index]]

Now I expect this to work:

fig,ax = plt.subplots()
im = ax.imshow(
    df_color.values, interpolation='nearest', aspect='auto')

But all I get is a TypeError: Image data can not convert to float

The result I expected (and was able to create with an terrible inefficient code should look like this Expected Result of the carpetplot

But this just raises an TypeError, Image data can not convert to float.

EDIT: If I use df_data.values directly (instead of df_color.values, it creates a plot, by using the default colormap. Is it possible to create a discrete colormap? (I didn't completly understand the colormap concepts from reading matplotlibs documentation)

Upvotes: 0

Views: 242

Answers (1)

MichaelA
MichaelA

Reputation: 1986

I found a solution to my problem. As assumed an discrete colormap does the trick. How to create one is described in a scipy cookbook, search for discrete_cmap.

So my working code would be this:

import pandas as pd
import pylab as plt
df_data = pd.DataFrame(
[[1, 2, 1], [1, 1, 3], [2, 2, 5], [3, 2, 1]], index=['n1', 'n2', 'n3', 'n4'], columns=['var1', 'var2', 'var3'])
cpool = ['#EC635C', '#4B81C4', '#F49961', '#B45955',
     '#8768B4']
cmap3 = plt.matplotlib.colors.ListedColormap(cpool[0:5], 'indexed')

fig, ax = plt.subplots()
im = ax.imshow(
df_data.values, cmap=cmap3, interpolation='nearest', aspect='auto')
plt.colorbar(mappable=im)

plt.show()

Axes descriptions still needs some fiddeling, but it works basically.

Upvotes: 1

Related Questions