দয়া করে মনে রাখবেন লেখক seabornশুধুমাত্র চান seaborn.heatmap শ্রেণীগত dataframes সঙ্গে কাজ করতে। এটি সাধারণ নয়।
যদি আপনার সূচক এবং কলামগুলি সংখ্যাসূচক এবং / অথবা তারিখের মান হয় তবে এই কোডটি আপনাকে ভালভাবে পরিবেশন করবে।
Matplotlib তাপ-ম্যাপিং ফাংশন pcolormeshপ্রয়োজন বিন পরিবর্তে সূচকের , তাই আপনার dataframe সূচকের থেকে বিল্ড বিন কিছু অভিনব কোড (এমনকি যদি আপনার সূচক সমানভাবে ব্যবধানে নয়!)।
বাকীটি সহজ np.meshgridএবং plt.pcolormesh।
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def conv_index_to_bins(index):
"""Calculate bins to contain the index values.
The start and end bin boundaries are linearly extrapolated from
the two first and last values. The middle bin boundaries are
midpoints.
Example 1: [0, 1] -> [-0.5, 0.5, 1.5]
Example 2: [0, 1, 4] -> [-0.5, 0.5, 2.5, 5.5]
Example 3: [4, 1, 0] -> [5.5, 2.5, 0.5, -0.5]"""
assert index.is_monotonic_increasing or index.is_monotonic_decreasing
# the beginning and end values are guessed from first and last two
start = index[0] - (index[1]-index[0])/2
end = index[-1] + (index[-1]-index[-2])/2
# the middle values are the midpoints
middle = pd.DataFrame({'m1': index[:-1], 'p1': index[1:]})
middle = middle['m1'] + (middle['p1']-middle['m1'])/2
if isinstance(index, pd.DatetimeIndex):
idx = pd.DatetimeIndex(middle).union([start,end])
elif isinstance(index, (pd.Float64Index,pd.RangeIndex,pd.Int64Index)):
idx = pd.Float64Index(middle).union([start,end])
else:
print('Warning: guessing what to do with index type %s' %
type(index))
idx = pd.Float64Index(middle).union([start,end])
return idx.sort_values(ascending=index.is_monotonic_increasing)
def calc_df_mesh(df):
"""Calculate the two-dimensional bins to hold the index and
column values."""
return np.meshgrid(conv_index_to_bins(df.index),
conv_index_to_bins(df.columns))
def heatmap(df):
"""Plot a heatmap of the dataframe values using the index and
columns"""
X,Y = calc_df_mesh(df)
c = plt.pcolormesh(X, Y, df.values.T)
plt.colorbar(c)
এটি ব্যবহার করে কল করুন heatmap(df)এবং এটি ব্যবহার করে দেখুন plt.show()।
