我正在尝试创建一个网格,其中只有2个数字,1或2&用绿色或红色表示。 我的数据如下
data = [[1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0], [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0], [1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0], [1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0], [1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0]]
这是我试图用来获取中心的x轴刻度标签的代码
ax.xaxis.set_major_formatter(ticker.NullFormatter())
# Customize minor tick labels
ax.xaxis.set_minor_locator(ticker.FixedLocator([0.5,1.5,2.5,3.5,4.5,5.5,6.5]))
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(column_labels))
ax.set_xticklabels(column_labels, minor=True)
ax.set_yticklabels(row_labels, minor=False)
但我最终得到的this图像不在中心位置。
任何帮助将不胜感激
编辑后编辑 - Image
这是整个代码
#!/usr/bin/env python3
#importing necessary libraries
import csv, os, glob, re
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
import matplotlib.ticker as ticker
#initializing and loading database
cwd = os.getcwd()
dbbiofilm = cwd
dbbiofilm+='/Database/Biofilm_Genes.csv'
with open(dbbiofilm,'r') as input_file:
reader = csv.reader(input_file)
row_str1 = []
for row in reader:
row_str1.append(row[0])
#print (row_str1)
#print(cwd)
#Converting txt files to csv files
directory = cwd
output = cwd
txt_files = os.path.join(directory, '*.txt')
names = []
for txt_file in glob.glob(txt_files):
with open(txt_file, "r") as input_file:
in_txt = csv.reader(input_file, delimiter='=')
names.append( os.path.splitext(os.path.basename(txt_file))[0])
filename = os.path.splitext(os.path.basename(txt_file))[0] + '.csv'
with open(os.path.join(output, filename), 'w') as output_file:
out_csv = csv.writer(output_file)
out_csv.writerows(in_txt)
#Initializing the csv files
file_list = []
extension = 'csv'
file_list = [j for j in glob.glob('*.{}'.format(extension))]
#print (file_list)
matrix = []
#For loop for looping through the different csv files
for name in file_list:
#Reading each csv file
with open(name,'r') as input_file:
row_str2 = []
for row in input_file:
row_data = re.split(',|;', row) #Delimiting csv files with both , and ; delimiters
row_str2.append(row_data[5])
#print (row_str2)
#(Re)Initializing loop variable
i = 2.0
flag = 2
l = 0
vector = []
#Number-based (1 or 2) martix construction
for const in row_str1:
while(l < len(row_str2)):
for var in row_str2:
l = l+1
if const == var:
flag = 1
if (flag == 1):
i = 1.0
flag = 2
vector.append(i)
i = 2.0
l = 0
matrix.append(vector)
vector = []
print (matrix)
#Initializing colour map (green & red)
cmap = colors.ListedColormap(['green', 'red'])
bounds = [0.5, 1.5, 2.5]
norm = colors.BoundaryNorm(bounds, len(bounds)-1)
#Initializing of plot
fig, ax = plt.subplots()
im = ax.imshow(matrix, cmap=cmap, norm=norm)
#Construction of colour bar and legend
cbar = plt.colorbar(im)
cbar.ax.get_yaxis().set_ticks([])
for j, lab in enumerate(['Present', 'Absent']):
cbar.ax.text(.5, (2 * j + 1) / 4.0, lab, ha='center', va='center', rotation=270)
cbar.ax.get_yaxis().labelpad = 15
cbar.ax.set_ylabel('Status of Gene', rotation=270)
#Grid construction
ax.grid(which ='major', axis = 'both', linestyle = '-', color = 'k', linewidth = 2)
ax.set_xticks(np.arange(-.5, len(matrix[0]), 1));
ax.set_yticks(np.arange(-.5, len(matrix), 1));
#Labelling the axes
column_labels = row_str1
row_labels = names
# Hide major tick labels
#ax.xaxis.set_major_formatter(ticker.NullFormatter())
# Customize minor tick labels
#ax.xaxis.set_minor_locator(ticker.FixedLocator([0.5,1.5,2.5,3.5,4.5,5.5,6.5]))
#ax.xaxis.set_minor_formatter(ticker.FixedFormatter(column_labels))
#
#ax.set_xticklabels(column_labels, minor=True)
ax.set_xticklabels('') #Clears current labels
x_ticks = [i + 0.5 for i in range(len(column_labels))] #This list creates new positions of your x_labels
ax.set_xticks(x_ticks, minor=True) #Sets new labels positions
ax.set_xticklabels(column_labels, minor=True)
ax.set_yticklabels(row_labels, minor=False)
plt.show()
This是带有示例文件的代码的github代码
答案 0 :(得分:0)
试试这个:
ax.set_xticklabels('') #Clears current labels
ax.set_xticks(range(len(column_labels)), minor=True) #Sets new labels positions
ax.set_xticklabels(column_labels, minor=True) #Sets the labels on the new position