我可以使用以下方法制作热图:
Index= [np.arange(0, 1, 1/5)]
Cols = ['A', 'B', 'C', 'D']
df = DataFrame(abs(np.random.randn(5, 4)), index=Index, columns=Cols)
plt.pcolor(df)
plt.yticks(np.arange(0.5, len(df.index), 1), df.index)
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.show()
给出:
如何将y轴标签更改为'0.0 0.5 1.0'?
答案 0 :(得分:0)
您可以在调用imshow的过程中使用关键字自变量range来标记x和y轴。
extent : scalars (left, right, bottom, top), optional, default: None
Data limits for the axes. //The default assigns zero-based row,
column indices to the `x`, `y` centers of the pixels.
作为示例,您需要使用
从pylab导入所有数据from pylab import *
input! = rand(5,5)
figure(1)
imshow(input1, interpolation='nearest')
grid(True)
left = 7.5
right = 9.5
bottom = 7.5
top = -0.5
extent = [left, right, bottom, top]
figure(2)
imshow(input1, interpolation='nearest', extent=extent)
grid(True)
show();
答案 1 :(得分:0)
我找到了使yticks工作的方法
len_I = 50
Index= [np.arange(0, 1, 1/len_I)]
Cols = ['A', 'B', 'C', 'D']
df = DataFrame(abs(np.random.randn(len_I, 4)), index=Index, columns=Cols)
plt.pcolor(df)
plt.yticks(np.arange(0, (len(df.index)+1), len(df.index)/2), np.arange(0, len(df.index), 0.5))
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.show()
这给出了:
答案 2 :(得分:0)
pcolor
在x和y位置给定的网格点之间绘制彩色矩形。您需要多于一行和一列的值。您可以将x网格设置在4列的0、1、2、3、4位置,刻度位置恰好位于网格点之间。 y轴可以是常规轴,其中MultipleLocator
可以将刻度位置设置为所需的倍数。
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
import pandas as pd
num_rows = 600
cols = ['A', 'B', 'C', 'D']
num_cols = len(cols)
df = pd.DataFrame(abs(np.random.randn(num_rows, num_cols).cumsum(axis=0)), columns=cols)
plt.pcolor(range(num_cols+1), np.linspace(0, 1, num_rows+1), df.to_numpy(), cmap='hot')
plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)
plt.gca().yaxis.set_major_locator(MultipleLocator(0.5))
plt.show()