OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to set new ticks for axes in heatmap?

  • Thread starter Thread starter Katan Katalan
  • Start date Start date
K

Katan Katalan

Guest
I have the following code where parameter1, 2, and alpha_values take decimal values. These are stored as numpy array. I wanna use heatmap but for axes, I am going to use integer values. However, when I add values for ticks in x-axis, I face the following figure. I am wondering how I should set customized ticks and also reverse the order of values in y-axis such that the maximum value is on top and the smallest value is on the bottom.

[![enter image description here][1]][1]

Code:
import matplotlib.colors as mcolors

# Create a DataFrame to store the data
data = pd.DataFrame({
    r'$a$': np.round(parameter1,2),
    r'$\mathbb{E}(S)$': np.round(parameter2,2),
    'alpha_values': alpha_values
})
# Pivot the DataFrame to create a matrix suitable for a heatmap
heatmap_data = data.pivot(index=r'$a$', columns=r'$\mathbb{E}(S)$', values='alpha_values')
cmap = mcolors.LinearSegmentedColormap.from_list("gray_scale", ["white", "blue"])


x_axis_labels = np.arange(-4, 13, 2)
xticklabels = [str(c) for c in x_axis_labels]
y_axis_labels = np.arange(0, 13, 2)

ax = sns.heatmap(heatmap_data, cmap=cmap, annot=False, fmt=".2f",  vmin=0, vmax=1, xticklabels=xticklabels)


#ax.set_yticklabels(yticks[::-1])  # Reverse the y-axis labels

# Add a frame around the figure
for _, spine in ax.spines.items():
    spine.set_visible(True)
    spine.set_linewidth(1)

'''


  [1]: https://i.sstatic.net/IYKNcAFW.png
<p>I have the following code where parameter1, 2, and alpha_values take decimal values. These are stored as numpy array.
I wanna use heatmap but for axes, I am going to use integer values. However, when I add values for ticks in x-axis, I face the following figure. I am wondering how I should set customized ticks and also reverse the order of values in y-axis such that the maximum value is on top and the smallest value is on the bottom.</p>
<p>[![enter image description here][1]][1]</p>
<pre><code>import matplotlib.colors as mcolors

# Create a DataFrame to store the data
data = pd.DataFrame({
r'$a$': np.round(parameter1,2),
r'$\mathbb{E}(S)$': np.round(parameter2,2),
'alpha_values': alpha_values
})
# Pivot the DataFrame to create a matrix suitable for a heatmap
heatmap_data = data.pivot(index=r'$a$', columns=r'$\mathbb{E}(S)$', values='alpha_values')
cmap = mcolors.LinearSegmentedColormap.from_list("gray_scale", ["white", "blue"])


x_axis_labels = np.arange(-4, 13, 2)
xticklabels = [str(c) for c in x_axis_labels]
y_axis_labels = np.arange(0, 13, 2)

ax = sns.heatmap(heatmap_data, cmap=cmap, annot=False, fmt=".2f", vmin=0, vmax=1, xticklabels=xticklabels)


#ax.set_yticklabels(yticks[::-1]) # Reverse the y-axis labels

# Add a frame around the figure
for _, spine in ax.spines.items():
spine.set_visible(True)
spine.set_linewidth(1)

'''


[1]: https://i.sstatic.net/IYKNcAFW.png
</code></pre>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top