0

I have a dictionary of lists:

dictA = {"A": [1,2,3], "B": [4,5,6], "C": [7,8,9]}

I want to display the data as a heatmap using the Matplotlib imshow. A, B and C run vertically (y), and the values are the corresponding cell value running left to right. This would result in a 3x3 plot.

Ideally, I would like to keep this native to core Python and avoid verbose data-type wrangling via packages like NumPy, but if it can't be helped, it can't be helped - I just want it to work.

The basics would be:

import matplotlib.pyplot as plt

dictA = {"A": [1,2,3], "B": [4,5,6], "C": [7,8,9]}
SOMETHING = #wrangling my dictionary to be "array-like"

plt.imshow(SOMETHING)
plt.show()
halfer
  • 19,824
  • 17
  • 99
  • 186
Anthony Nash
  • 834
  • 1
  • 9
  • 26
  • 1
    An approach using seaborn and pandas: `import pandas as pd; import seaborn as sns; sns.heatmap(your_dict, annot=True)`. Please avoid using the build-in name `dict` as a variable name, as that would make the built-in name inaccessible. – JohanC Jul 26 '23 at 12:21
  • Forgive my typo - corrected. Thanks for the suggestion. My preference would be to keep the code as core as possible, avoiding importing data-wrangling packages like pandas and numpy. – Anthony Nash Jul 26 '23 at 12:29

2 Answers2

2

Provided that your dictionary values have been added in the correct order (and that you are working with Python 3.6 or above), the simplest version would be:

import matplotlib.pyplot as plt

dictA = {"A": [1,2,3], "B": [4,5,6], "C": [7,8,9]}
plt.imshow(dictA.values())
plt.show()

This would create an image layout with each dictionary value forming a row, that is:

123
456
789

If the question, instead, was meant in the sense that each dictionary value should form a column, that is:

147
258
369

… then this could be achieved by replacing the plt.imshow() call above with:

plt.imshow(list(zip(*dictA.values())))

Thanks to @mozway for pointing out the ambiguity and providing the corresponding second solution.

simon
  • 1,503
  • 8
  • 16
  • That would not provide the correct orientation, you'd need: `plt.imshow(list(zip(*data_dict.values())))` – mozway Jul 26 '23 at 12:28
  • @mozway I understood it this way: "A, B and C run vertically (y)" meaning A being the fist row, B being the second, C being the third, and "the values are the corresponding cell value running left to right" meaning 1 being left, 2 being center, 3 being right, etc. This should be fulfilled with my solution, right? – simon Jul 26 '23 at 12:33
  • 1
    fair enough, it's indeed ambiguous. That doesn't match the accepted answer so OP should clarify ;) – mozway Jul 26 '23 at 12:34
  • 1
    @mozway: Fully agree, and I only noticed the ambiguity through your comment! – simon Jul 26 '23 at 12:35
1

To create a heatmap using matplotlib with the given dictionary of lists, you can use a nested list comprehension to convert the dictionary into an "array-like" format. While numpy is often used for more complex data manipulations, you can avoid using it for this simple task. Here's a way to achieve the heatmap without numpy:

import matplotlib.pyplot as plt

# Your dictionary of lists
data_dict = {"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}

# Convert the dictionary into an array-like format (list of lists)
array_like_data = [[data_dict[col][row] for col in data_dict] for row in 
range(len(data_dict["A"]))]

# Create the heatmap
plt.imshow(array_like_data, cmap='viridis', aspect='auto', origin='upper')

# Add labels to the axes
plt.xticks(range(len(data_dict)), list(data_dict.keys()))
plt.yticks(range(len(data_dict["A"])), range(1, len(data_dict["A"]) + 1))

# Show the plot
plt.colorbar()
plt.show()
  • Thank you so much. This is precisely what I wanted. Also, thank you for avoiding numpy. Every tutorial on imshow I found used numpy, even for basic examples - that goes against so many software engineering principles. – Anthony Nash Jul 26 '23 at 12:21
  • 1
    @Anthony I have to say I'm quite suspicious that this answer is AI-generated… – mozway Jul 26 '23 at 12:29
  • 3
    @AnthonyNash Numpy is really unavoidable if you want to do more advanced data manipulation or visualization. A library such as matplotlib internally makes heavy use of numpy. Nowadays, numpy is almost considered a core part of Python. It both brings speed and handy array operations. (You are right in trying to better understand how Python works and avoid numpy.) – JohanC Jul 26 '23 at 12:30
  • @mozway it works, so I'm happy. Interestingly, if you suspect this is generated by an AI, then it's not an artificial intelligence - as per the Turing Test. Just a little joke. – Anthony Nash Jul 26 '23 at 12:31
  • @mozway If it is AI-generated, it is quite well done: it takes into account a lot of details (such as using the dict keys for the labeling and using `origin='upper'`). It also doesn't add too much of the blabla that AI usually generates. – JohanC Jul 26 '23 at 12:35
  • @JohanC that's why I said "suspicious" and acknowledged the correctness ;) – mozway Jul 26 '23 at 12:38