简体   繁体   中英

Plotting a heat map of discrete values in python

I have a few related questions:

1) I was wondering if there is way of making a heatmap of discrete values in python similar to the example given here .

Once the heatmap is created, I'll need some additional controls:

2) Control the number of row and column labels. Let say, I have two variables x and y where 0 <= x,y <= 1. I somehow compute a function f of x and y (f(x,y)) where the output value of the function is only 1, 2 or 3. Next, I create a nested loop on x and y to compute f for different values of x and y. See the sample pseudocode below:

from __future__ import division
import bumpy as np
    def f(x,y):
        ### Compute the value of f for a given x and y. The output value will be 1, 2 or 3 ###
        f = np.random.randint(1,4)

    data = {}
    for x in [i/100 for i in range(101)]
        for y in [i/100 for i in range(101)]
            data[(x,y)] = f(x,y)

Now, I have a 100x100 matrix of data for which I will make a heatmap s. If I want to show the row and column labels on the heatmap though, 100 points for each row and column will be shown (0, 0.01, 0.02, ..., 1), which makes the figure too crowded. I would like to determine the increment on each axis, eg, if the increment is 0.2, then the row and column labels should be 0, 0.2, 0.4, 0.6, 0.8 and 1. While this can be easily done for line plots, I was wondering if the same thing can be applied to a heatmap.

3) Control the font size and face of labels.

4) Add gridlines for the horizontal and vertical axis.

Try heatmap from seaborn .

To actually store tabular data I wouldn't use a dictionary. A list of list would be better, but the best way to go is using NumPy arrays (see also: numpy - evaluate function on a grid of points ).

import numpy as np
import pandas as pd
import seaborn as sns

X = np.linspace(0, 1, 101)
Y = np.linspace(0, 1, 101)

data = np.zeros((101, 101))

# or use meshgrid instead
for i, x in enumerate(X):
    for j, y in enumerate(Y):
        data[i, j] = some_function(x, y)

df = pd.DataFrame(data, index=Y, columns=X)

sns.heatmap(df)

Alternatively, you can use imshow (see: imshow when you are plotting data, not images. Realtion between aspect and extent? ) - then it will show only some ticks on the axes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM