matplotlib.pyplot heatmap with strange range of values - python-3.x

I am plotting a heatmap that have many values set to zero, a few that are very low (0.001 to 0.05) and a few that are high (>0.9 up to 1). Is it possible to set a non-uniform colorbar spacing so the data can be better understood? How can that be done?
data = {(0, 33): 0.002, (0, 34): 0.007, (0, 35): 0.015, (0, 36): 0.014, (0, 37): 0.943, (0, 38): 0.01, (0, 51): 0.007, (1, 33): 0.002, (1, 34): 0.002, (1, 35): 0.005, (1, 36): 0.005, (1, 37): 0.977, (1, 38): 0.006, (1, 51): 0.003, (2, 34): 0.002, (2, 35): 0.003, (2, 36): 0.003, (2, 37): 0.99, (2, 38): 0.002, (3, 33): 0.002, (3, 34): 0.003, (3, 35): 0.005, (3, 36): 0.007, (3, 37): 0.978, (3, 38): 0.005, (3, 51): 0.001, (4, 37): 1.0, (5, 37): 1.0, (12, 33): 0.003, (12, 34): 0.004, (12, 35): 0.018, (12, 36): 0.017, (12, 37): 0.931, (12, 38): 0.013, (12, 49): 0.001, (12, 51): 0.012, (13, 33): 0.003, (13, 34): 0.004, (13, 35): 0.009, (13, 36): 0.013, (13, 37): 0.944, (13, 38): 0.014, (13, 51): 0.012, (14, 34): 0.001, (14, 35): 0.002, (14, 36): 0.003, (14, 37): 0.993, (14, 38): 0.001, (15, 34): 0.001, (15, 35): 0.002, (15, 36): 0.003, (15, 37): 0.992, (15, 38): 0.001, (16, 37): 0.996, (16, 38): 0.004, (17, 37): 1.0, (18, 37): 1.0, (19, 37): 1.0, (20, 37): 1.0, (21, 37): 1.0, (22, 37): 1.0, (23, 37): 1.0, (24, 33): 0.004, (24, 34): 0.007, (24, 35): 0.032, (24, 36): 0.035, (24, 37): 0.903, (24, 38): 0.016, (24, 51): 0.003, (25, 33): 0.004, (25, 34): 0.006, (25, 35): 0.029, (25, 36): 0.034, (25, 37): 0.907, (25, 38): 0.016, (25, 51): 0.003, (26, 35): 0.004, (26, 36): 0.007, (26, 37): 0.972, (26, 38): 0.017, (27, 33): 0.004, (27, 34): 0.005, (27, 35): 0.007, (27, 36): 0.008, (27, 37): 0.961, (27, 38): 0.016, (28, 33): 0.005, (28, 34): 0.006, (28, 35): 0.008, (28, 36): 0.01, (28, 37): 0.961, (28, 38): 0.01, (29, 33): 0.002, (29, 34): 0.003, (29, 35): 0.023, (29, 36): 0.016, (29, 37): 0.943, (29, 38): 0.012, (30, 33): 0.001, (30, 35): 0.01, (30, 36): 0.001, (30, 37): 0.985, (30, 38): 0.004, (35, 37): 1.0, (36, 37): 1.0, (37, 37): 1.0, (38, 38): 1.0, (39, 49): 0.013, (39, 50): 0.012, (39, 51): 0.975, (40, 47): 0.18, (40, 49): 0.02, (40, 50): 0.126, (40, 51): 0.674, (41, 49): 0.012, (41, 50): 0.022, (41, 51): 0.966}
i_set, j_set = set(), set()
for k in data.keys():
i_set.add(k[0])
j_set.add(k[1])
i_set = list(i_set)
i_set.sort()
j_set = list(j_set)
j_set.sort()
# Make array
array = []
for i in i_set:
row = []
for j in j_set:
row.append( data.get((i,j), 0) )
array.append(row)
array = np.array(array)
# plot
fig, ax = plt.subplots()
im = ax.imshow(array)
# We want to show all ticks...
ax.set_xticks(np.arange(len(j_set)))
ax.set_yticks(np.arange(len(i_set)))
# ... and label them with the respective list entries
ax.set_xticklabels(j_set)
ax.set_yticklabels(i_set)
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
for i in range(len(i_set)):
for j in range(len(j_set)):
if array[i,j] != 0:
text = ax.text(j, i, array[i, j],
ha="center", va="center", color="w", fontsize='xx-small')
fig.tight_layout()
plt.show()
Furthermore, the top and bottom rows are "cut in half" instead of shown in full. How can I fix that?
EDIT
The last question, of how to fix the cut in half squares was solved. Still, the question on how to make the colorbar vary non-uniformly is open!

Related

How to convert those deprecated parse commands to Python 3.10?

I have a couple of lines written with old Python 2.7's parser commands that don't seem to have equivalents in Python > 3.10. They are:
st = parser.expr(stringToConvert)
tup = st.totuple()
where stringToConvert is a <type 'unicode'> string such as, for example:
a5*X**5 + a4*X**4 + a3*X**3 + a2*X**2 + a1*X + a0 + log(5.0)
In Python 2.7, the two lines up above yield something like:
(258, (332, (306, (310, (311, (312, (313, (316, (317, (318, (319, (320, (321, (322, (323,
(324, (325, (1, 'a5'))))), (16, '*'), (322, (323, (324, (325, (1, 'X'))), (35, '**'),
(322, (323, (324, (325, (2, '5')))))))), (14, '+'), (321, (322, (323, (324, (325,
(1, 'a4'))))), (16, '*'), (322, (323, (324, (325, (1, 'X'))), (35, '**'), (322, (323,
(324, (325, (2, '4')))))))), (14, '+'), (321, (322, (323, (324, (325, (1, 'a3'))))),
(16, '*'), (322, (323, (324, (325, (1, 'X'))), (35, '**'), (322, (323, (324, (325,
(2, '3')))))))), (14, '+'), (321, (322, (323, (324, (325, (1, 'a2'))))), (16, '*'),
(322, (323, (324, (325, (1, 'X'))), (35, '**'), (322, (323, (324, (325, (2, '2')))))))),
(14, '+'), (321, (322, (323, (324, (325, (1, 'a1'))))), (16, '*'), (322, (323, (324,
(325, (1, 'X')))))), (14, '+'), (321, (322, (323, (324, (325, (1, 'a0')))))), (14, '+'),
(321, (322, (323, (324, (325, (1, 'log')), (327, (7, '('), (335, (336, (306, (310, (311,
(312, (313, (316, (317, (318, (319, (320, (321, (322, (323, (324, (325, (2,
'5.0')))))))))))))))))), (8, ')'))))))))))))))))), (4, ''), (4, ''), (0, ''))
I've been stuck with these two lines for days now. I have no idea what the convoluted tuple above is all about nor to what it is good for, but the code needs it. All I know is that parser is now defunct and so far I've been unable to get along with the new parser(?). The <type 'unicode'> thing may also pose an extra problem, as it is also deprecated (or isn't it?). Any suggestions?
Thanks.

How to print out a dict with nice output

Im wondering how I'm available to print the first 4 words from a list[:4] on the same line and then the next 4 [4:8] on the line under and so on. I have a dictionary with over 1000 words and I don't want 1 word to printed on 1 line because then the vertical output goes on forever. I would like to split them up by 4 on one line.
Any tips?
you can try using pprint module of python
Refer https://docs.python.org/3/library/pprint.html
for the dict:
#let your dict be dict1
val = list(dict1.values())
key = list(dict1.keys())
print(val)
print(key)
for i in range(len(val)):
if i // 4 == (i+1)// 4:
print(dict1[key[i]], end=" ")
else:
print(dict1[key[i]], end="\n")
Given dictionary
d = {i: i * i for i in range(200)}
do
from pprint import pprint
pprint(list(d.items()), compact=True)
which prints
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64),
(9, 81), (10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225),
(16, 256), (17, 289), (18, 324), (19, 361), (20, 400), (21, 441), (22, 484),
(23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841),
(30, 900), (31, 961), (32, 1024), (33, 1089), (34, 1156), (35, 1225),
(36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681),
(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209),
(48, 2304), (49, 2401), (50, 2500), (51, 2601), (52, 2704), (53, 2809),
(54, 2916), (55, 3025), (56, 3136), (57, 3249), (58, 3364), (59, 3481),
(60, 3600), (61, 3721), (62, 3844), (63, 3969), (64, 4096), (65, 4225),
(66, 4356), (67, 4489), (68, 4624), (69, 4761), (70, 4900), (71, 5041),
(72, 5184), (73, 5329), (74, 5476), (75, 5625), (76, 5776), (77, 5929),
(78, 6084), (79, 6241), (80, 6400), (81, 6561), (82, 6724), (83, 6889),
(84, 7056), (85, 7225), (86, 7396), (87, 7569), (88, 7744), (89, 7921),
(90, 8100), (91, 8281), (92, 8464), (93, 8649), (94, 8836), (95, 9025),
(96, 9216), (97, 9409), (98, 9604), (99, 9801), (100, 10000), (101, 10201),
(102, 10404), (103, 10609), (104, 10816), (105, 11025), (106, 11236),
(107, 11449), (108, 11664), (109, 11881), (110, 12100), (111, 12321),
(112, 12544), (113, 12769), (114, 12996), (115, 13225), (116, 13456),
(117, 13689), (118, 13924), (119, 14161), (120, 14400), (121, 14641),
(122, 14884), (123, 15129), (124, 15376), (125, 15625), (126, 15876),
(127, 16129), (128, 16384), (129, 16641), (130, 16900), (131, 17161),
(132, 17424), (133, 17689), (134, 17956), (135, 18225), (136, 18496),
(137, 18769), (138, 19044), (139, 19321), (140, 19600), (141, 19881),
(142, 20164), (143, 20449), (144, 20736), (145, 21025), (146, 21316),
(147, 21609), (148, 21904), (149, 22201), (150, 22500), (151, 22801),
(152, 23104), (153, 23409), (154, 23716), (155, 24025), (156, 24336),
(157, 24649), (158, 24964), (159, 25281), (160, 25600), (161, 25921),
(162, 26244), (163, 26569), (164, 26896), (165, 27225), (166, 27556),
(167, 27889), (168, 28224), (169, 28561), (170, 28900), (171, 29241),
(172, 29584), (173, 29929), (174, 30276), (175, 30625), (176, 30976),
(177, 31329), (178, 31684), (179, 32041), (180, 32400), (181, 32761),
(182, 33124), (183, 33489), (184, 33856), (185, 34225), (186, 34596),
(187, 34969), (188, 35344), (189, 35721), (190, 36100), (191, 36481),
(192, 36864), (193, 37249), (194, 37636), (195, 38025), (196, 38416),
(197, 38809), (198, 39204), (199, 39601)]

How to know the max of this list of tuples? (Python)

I okay so i need the max of this list:
variable=[[(0, 0), (0, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)], [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')], [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')], [(11, '♥'), (13, '♠'), (11, '♣'), (13, '♣')]]
I´m using this line of code to do it but it doesn´t really work
print(max(variable,key=lambda x:x[1]))
The print says [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')] but the one i need are one of the other two ones and i dont know what to do. Thanks in advance and sorry if i messed something up,this is my first time using stack overflow for asking a question. Im using python btw
I believe you need sum of element 0 in your sub-list.
Ex:
variable=[[(0, 0), (0, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)], [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')], [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')], [(11, '♥'), (13, '♠'), (11, '♣'), (13, '♣')]]
print(max(variable, key=lambda x: sum(i for i,_ in x)))
# --> [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')]

Log plots in matplotlib is creating two lines

I was wondering why my loglog plots are creating two lines when it's just the orange one I want and why the labels won't show? Any insight would be greatly appreciated.
import matplotlib.pyplot as plt
data1 = [(3, 5034), (2, 4596), (4, 1469), (5, 1209), (6, 540), (7, 380), (8, 196), (9, 136), (10, 71), (11, 47), (12, 39), (13, 20), (14, 16), (15, 12), (16, 6), (18, 5), (17, 2), (19, 2), (22, 2), (1, 1), (24, 1), (20, 1)]
plt.loglog(data1, basex=2, basey=2, label='N1')
plt.show()

python sqlite3 List

I have a question about sqlite3 in python, it is about the list problem.
Here is the question:
Write a function getMay(dbName) that takes as a parameter the filename of above database and returns two lists, one with the days and one with the temperatures at noon on those days.
Here is My code:
import sqlite3
def getMay(dbName):
conn = sqlite3.connect(dbName)
cur = conn.cursor()
cur.execute('select Day,Temp from May14 where Time= "12:00" order by Day ASC')
print(cur.fetchall())
cur.close()
conn.close()
Here is my output:
[(1, 13.7), (2, 11.1), (3, 12.2), (4, 13.2), (5, 12.9), (6, 12.5), (7,
9.6), (8, 11.6), (9, 13.2), (10, 19.2), (11, 21.7), (12, 15.2), (13, 11.9), (14, 16.4), (15, 12.2), (16, 10.1), (17, 9.8), (18, 16.2), (19, 21.5), (20, 17.8), (21, 17.0), (22, 18.6), (23, 16.5), (24, 21.2), (25, 25.4), (26, 27.8), (27, 27.3), (28, 13.7), (29, 15.0), (30,
22.5), (31, 21.0)]
But the correct output should look like:
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], [13.7, 11.1, 12.2, 13.2,
12.9, 12.5, 9.6, 11.6, 13.2, 19.2, 21.7, 15.2, 11.9, 16.4, 12.2, 10.1,
9.8, 16.2, 21.5, 17.8, 17.0, 18.6, 16.5, 21.2, 25.4, 27.8, 27.3, 13.7,
15.0, 22.5, 21.0])
Anyone know how to solve this problem?
Please help! Thank!
one easy solution could be this:
dayTemp_list = [(1, 13.7), (2, 11.1), (3, 12.2), (4, 13.2), (5, 12.9), (6, 12.5), (7, 9.6), (8, 11.6), (9, 13.2), (10, 19.2), (11, 21.7), (12, 15.2), (13, 11.9), (14, 16.4), (15, 12.2), (16, 10.1), (17, 9.8), (18, 16.2), (19, 21.5), (20, 17.8), (21, 17.0), (22, 18.6), (23, 16.5), (24, 21.2), (25, 25.4), (26, 27.8), (27, 27.3), (28, 13.7), (29, 15.0), (30, 22.5), (31, 21.0)]
days = []
temp = []
for i in dayTemp_list:
days.append(i[0])
temp.append(i[1])
result = (days,temp)
print result

Resources