2017-09-15 66 views
0
from matplotlib.lines import Line2D 
import numpy as np 

fig = plt.figure(figsize=(6,6)) 

plt.plot([1, 2, 4, 8, 12, 16, 20, 24], color='black', marker=None) 

labels = ['1', '2', '4', '8', '12', '16', '20', '24'] 
xticks = [1,2,3,4,5,6,7,8] 
nthreads = [1,2,4,8,12,16,20,24] 

plt.xticks(xticks, labels) 
plt.yticks(nthreads, labels) 

plt.show() 

我試圖產生f(x)= x的圖,但我無法擺脫行中的彎曲。還有一個X軸刻度標籤的右移。Matplotlib xtick ytick

如何通過點(1,1),(2,2),...,(24,24)繪製直線並固定x軸標籤移位?

Plot generated by the code above

我試過的nthreadsxticks其他所有排列爲plt.xticks()plt.yticks(),分別,沒有結果的期待接近我想要的東西。

回答

2

當您沒有設置x數組matplotlib時,使用默認列表[0,1,2,3,4,5,6,7]。因此你不會得到一條直線。您必須指定陣列xy。在你的情況下,他們必須是相同的。

如果您想放置標籤,請使用此示例中的位置和標籤。移動當前軸(plt.gca())的座標系集xlimylim

from matplotlib.lines import Line2D 
import matplotlib.pyplot as plt 
import numpy as np 
# plot y=x 
fig = plt.figure(figsize=(6,6)) 
x = [1, 2, 4, 8, 12, 16, 20, 24] 
plt.plot(x,x, color='black', marker=None) 
# put labels for all ticks 
labels = np.arange(1,25,1) 
plt.xticks(labels, labels) 
plt.yticks(labels, labels) 
# set limits of axis 
ax = plt.gca() 
ax.set_xlim([1,24]) 
ax.set_ylim([1,24]) 

plt.show() 

enter image description here

相關問題