0

I have the following data:

keys = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']

values = ['111', '222', '333', '444', '555', '666', '777', '888', '222', '888', '222', '333', '999', '444', '555', '666', '777', '888']

I want to create a heatmap as follows:

mydata = pd.DataFrame({x: values, y: keys})
df_new = mydata.set_index(x)[y].astype(str).str.get_dummies().T
fig, ax = plt.subplots(figsize = (20,5))
ax = sns.heatmap(df_new, cbar=False, linewidths=.5)
plt.show()

The only issue is that the values appear as duplicated columns in a heatmap. For example, 222 appears 3 times in the heatmap. How can I push the same value to be in a single column?

Tatik
  • 1,107
  • 1
  • 9
  • 17
  • I don't understand what your expectant output is. So I can't tell you the direction you are supposed to go. – piRSquared May 13 '19 at 16:41
  • 1
    It seems the `DataFrame` you want to plot is `pd.crosstab(mydata[y], mydata[x])`? – ALollz May 13 '19 at 16:42
  • 1
    @piRSquared: If you take my code and paste it into jupyter notebook, you will see what I mean. In X axis the same value may appear several times, while I want each value to appear only one time. – Tatik May 13 '19 at 16:46
  • @ALollz: Yes, absolutely. This is what I want to get, but instead of a table, I want to get a visualization as a heatmap. – Tatik May 13 '19 at 16:47
  • 3
    Put that data in `ax = sns.heatmap(df_new, cbar=False, linewidths=.5)` instead of `df_new`? – Quang Hoang May 13 '19 at 16:48
  • This is definitely an XY problem. Meaning that I'd fix the issue upstream. You could fix your `df_new` by summing across the columns. `sns.heatmap(df_new.sum(axis=1, level=0), cbar=False, linewidth=.5)` Or from the start like this `sns.heatmap(pd.get_dummies(keys).T.dot(pd.get_dummies(values)), cbar=False, linewidth=.5)` – piRSquared May 13 '19 at 16:50
  • If you are so inclined see the `crosstab` portion of this [Q&A](https://stackoverflow.com/a/47152692/2336654) – piRSquared May 13 '19 at 16:51
  • @QuangHoang: This is exactly what I need. Thanks for a short and clear help. – Tatik May 13 '19 at 16:52
  • @piRSquared: Thanks for different alternatives! – Tatik May 13 '19 at 16:53

0 Answers0