2013-01-18 13 views
60

पर एक प्लॉट के शीर्ष पर एक्स-अक्ष को स्थानांतरित करना this question about heatmaps in matplotlib पर आधारित, मैं एक्स-अक्ष शीर्षक को साजिश के शीर्ष पर ले जाना चाहता था।matplotlib

import matplotlib.pyplot as plt 
import numpy as np 
column_labels = list('ABCD') 
row_labels = list('WXYZ') 
data = np.random.rand(4,4) 
fig, ax = plt.subplots() 
heatmap = ax.pcolor(data, cmap=plt.cm.Blues) 

# put the major ticks at the middle of each cell 
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False) 
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False) 

# want a more natural, table-like display 
ax.invert_yaxis() 
ax.xaxis.set_label_position('top') # <-- This doesn't work! 

ax.set_xticklabels(row_labels, minor=False) 
ax.set_yticklabels(column_labels, minor=False) 
plt.show() 
हालांकि

, matplotlib's set_label_position बुला (जैसा कि ऊपर notated) वांछित प्रभाव के लिए प्रतीत नहीं होता।

enter image description here

क्या मैं गलत कर रहा हूँ: यहाँ मेरी उत्पादन है?

उत्तर

75

उपयोग:

ax.xaxis.set_ticks_position('top') # the rest is the same 

यह मैं देता । आदेश

ax.set_xlabel('X LABEL')  
ax.xaxis.set_label_position('top') 

लेबल को प्रभावित करता है, टिक टिक नहीं।

import matplotlib.pyplot as plt 
import numpy as np 
column_labels = list('ABCD') 
row_labels = list('WXYZ') 
data = np.random.rand(4, 4) 
fig, ax = plt.subplots() 
heatmap = ax.pcolor(data, cmap=plt.cm.Blues) 

# put the major ticks at the middle of each cell 
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False) 
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False) 

# want a more natural, table-like display 
ax.invert_yaxis() 
ax.xaxis.tick_top() 

ax.set_xticklabels(column_labels, minor=False) 
ax.set_yticklabels(row_labels, minor=False) 
plt.show() 

enter image description here

+0

क्या आप कृपया मुझे बता सकते हैं कि एक्स अक्ष को बी और सी के बीच कैसे रखा जाए? मैंने पूरे दिन कोशिश की लेकिन कोई सफलता – DaniPaniz

19

आप set_ticks_position बजाय set_label_position हैं:

ax.xaxis.tick_top() 

छवि के शीर्ष पर टिक के निशान जगह

enter image description here

+0

क्या आप कृपया मुझे बता सकते हैं कि एक्स अक्ष को बी और सी के बीच कैसे रखा जाए? मैंने पूरे दिन कोशिश की लेकिन कोई सफलता – DaniPaniz

1

आप कुछ अतिरिक्त मालिश करने के लिए मिल गया है अगर आप टिक (नहीं लेबल) ऊपर और नीचे (न केवल ऊपर) पर दिखाना चाहते हैं। एक ही रास्ता मैं ऐसा कर सकता है unutbu के कोड में एक मामूली परिवर्तन के साथ है:

import matplotlib.pyplot as plt 
import numpy as np 
column_labels = list('ABCD') 
row_labels = list('WXYZ') 
data = np.random.rand(4, 4) 
fig, ax = plt.subplots() 
heatmap = ax.pcolor(data, cmap=plt.cm.Blues) 

# put the major ticks at the middle of each cell 
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False) 
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False) 

# want a more natural, table-like display 
ax.invert_yaxis() 
ax.xaxis.tick_top() 
ax.xaxis.set_ticks_position('both') # THIS IS THE ONLY CHANGE 

ax.set_xticklabels(column_labels, minor=False) 
ax.set_yticklabels(row_labels, minor=False) 
plt.show() 

आउटपुट:

enter image description here

+0

क्या आप कृपया मुझे बता सकते हैं कि एक्स अक्ष को बी और सी के बीच कैसे रखा जाए? मैंने पूरे दिन कोशिश की लेकिन कोई सफलता नहीं मिली – DaniPaniz

8

tick_params टिक गुण स्थापित करने के लिए बहुत उपयोगी है। लेबल को शीर्ष पर ले जाया जा सकता है:

ax.tick_params(labelbottom='off',labeltop='on')