2015-07-12 21 views
1

我有這個代碼,我可以控制屬性如:x軸的範圍,標題,xlabel,ylabel,圖例,網格,旋轉x上的標籤文字:plt.setp替代sublot或如何在x軸上設置文本旋轉的子圖

#!/usr/bin/python 
import datetime 
import numpy as np 
import matplotlib 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Slider 
from matplotlib.dates import DateFormatter 
############################################ 
# generate data 
x = np.arange(0,10) 
y0 = np.arange(0,10) 
y1 = np.random.rand(10) 
y2 = np.random.rand(10) 
############################################ 
# initialize (sub)plot(s) 
fig, ax = plt.subplots() 
############################################ 
# width of x axis 
x_min_index = 0 
x_max_index = 3 

x_min = x[x_min_index] 
x_max = x[x_max_index] 
############################################ 
# create (sub)plot 
l, = plt.plot(x,y0, label="plot1a") 
l, = plt.plot(x,y1, label="plot1b") 
l, = plt.plot(x,y2, label="plot1c") 

# (sub)plot - set values 
plt.title("plot1") 
plt.xlabel('xlabel') 
plt.ylabel('ylabel') 
plt.legend() 
plt.grid() 
############################################ 
# apply for all (sub)plot(s) 
plt.subplots_adjust(bottom=0.25) 
############################################ 
# (sub)plot - get values 
# plt.axis(x_min, x_max, y_min, y_max) 
y_min = plt.axis()[2] 
y_max = plt.axis()[3] 
print y_min 
print y_max 
############################################ 
# (sub)plot - set values 
# change only x values 
# y values are set to same value 
# plt.axis([x_min, x_max, y_min, y_max]) 

# (sub)plot - set values 
# change only x values 
plt.xlim(x_min, x_max) 
############################################ 
# (sub)plot - get values 
locs, labels = plt.xticks() 
print locs 
print labels 
############################################ 
# (sub)plot - set values 
plt.setp(labels, rotation=45) 

plt.show() 

此代碼返回這個數字: enter image description here

我希望能夠控制所有這些屬性,但創建多個次要情節,我嘗試這是在這裏:

#!/usr/bin/python 
import datetime 
import numpy as np 
import matplotlib 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Slider 
from matplotlib.dates import DateFormatter 
############################################ 
# generate data 
x = np.arange(0,10) 
y0 = np.arange(0,10) 
y1 = np.random.rand(10) 
y2 = np.random.rand(10) 
############################################ 
# initialize (sub)plot(s) 
fig, axarr = plt.subplots(2) 

# http://stackoverflow.com/questions/6541123/improve-subplot-size-spacing-with-many-subplots-in-matplotlib 
fig.tight_layout() 
############################################ 
# width of x axis 
x_min_index = 0 
x_max_index = 3 

x_min = x[x_min_index] 
x_max = x[x_max_index] 
############################################ 
# 1st (sub)plot 
line0 = axarr[0].plot(x,y1, label="plot1a") 
line0 = axarr[0].plot(x,y2, label="plot1b") 
line0 = axarr[0].legend() 
line0 = axarr[0].set_title('plot1') 
line0 = axarr[0].set_xlabel('xlabel1') 
line0 = axarr[0].set_ylabel('ylabel1') 
line0 = axarr[0].grid() 
############################################ 
# 2st (sub)plot 
line1 = axarr[1].plot(x,y0, label="plot2", color="red") 
line1 = axarr[1].legend() 
line1 = axarr[1].set_title('plot2') 
line1 = axarr[1].set_xlabel('xlabel2') 
line1 = axarr[1].set_ylabel('ylabel2') 
line1 = axarr[1].grid() 
############################################ 
# apply for all (sub)plot(s) 
plt.subplots_adjust(bottom=0.25) 
############################################ 
# OLD CODE 
# (sub)plot - get values 
# plt.axis(x_min, x_max, y_min, y_max) 
# y_min = plt.axis()[2] 
# y_max = plt.axis()[3] 
# print y_min 
# print y_max 

# NEW ALTERNATIVE 
l0_x_min, l0_x_max = axarr[0].get_xlim() 
l0_y_min, l0_y_max = axarr[0].get_ylim() 

l1_x_min, l1_x_max = axarr[1].get_xlim() 
l1_y_min, l1_y_max = axarr[1].get_ylim() 

print l0_x_min 
print l0_x_max 
print l0_y_min 
print l0_y_max 

print l1_x_min 
print l1_x_max 
print l1_y_min 
print l1_y_max 
############################################ 
# OLD CODE 
# (sub)plot - set values 
# change only x values 
# y values are set to same value 
# plt.axis([x_min, x_max, y_min, y_max]) 

# (sub)plot - set values 
# change only x values 
# plt.xlim(x_min, x_max) 

# NEW ALTERNATIVE 
axarr[0].set_xlim(x_min, x_max) 
############################################ 
# OLD CODE 
# (sub)plot - get values 
# locs, labels = plt.xticks() 
# print locs 
# print labels 

# NEW ALTERNATIVE 
line0_xticks = axarr[0].get_xticks() 
line0_labels = axarr[0].get_xticklabels() 
print line0_xticks 
print line0_labels 

line1_xticks = axarr[1].get_xticks() 
line1_labels = axarr[1].get_xticklabels() 
print line1_xticks 
print line1_labels 
############################################ 
# OLD CODE 
# (sub)plot - set values 
# plt.setp(labels, rotation=45) 

# NEW ALTERNATIVE 
############################################ 
plt.show() 

此代碼返回該圖中: enter image description here

此代碼具有限制,因爲我無法設置在x軸上的標籤的旋轉。我無法找到合適的方法來完成此操作。到目前爲止,我已經想通了以下方法對應於對方:

PLOT         SUBPLOTS 
plt.axis()        axarr[0].get_xlim(), axarr[0].get_ylim() 
plt.axis([x_min, x_max, y_min, y_max]) axarr[0].set_xlim(x_min, x_max), axarr[0].set_ylim(y_min, y_max) 
plt.xlim(x_min, x_max)     axarr[0].set_xlim(x_min, x_max) 
plt.xticks()       axarr[0].get_xticks(), axarr[0].get_xticklabels() 
plt.setp(labels, rotation=45)   ??? 
plt.title("plot1")      axarr[0].set_title('plot1') 
plt.grid()        axarr[0].grid() 
plt.legend()       axarr[0].legend() 
plt.xlabel('xlabel')     axarr[0].set_xlabel('xlabel') 
plt.ylabel('xlabel')     axarr[0].set_ylabel('xlabel') 
plt.plot(x,y, label="plot")    axarr[0].plot(x,y, label="plot") 

問題:

  1. 是否相似表存在,但對於情節和次要情節之間的所有相應的方法呢?
  2. 可能是更多的哲學問題;爲什麼兩個不同的命名約定?
  3. 我通過運行ipdb下的代碼,敲擊選項卡並記下方法的熟悉名稱,創建了此表作爲試用錯誤。有沒有更好的辦法?我可以想象一些將比較方法返回類型,參數等的東西。
  4. 如何從另一個方法中找到另一種方法?
  5. 最後,如何在子圖下旋轉x標籤?

回答

1

我會盡量用1 1,但在此之前回答你的問題,你要明白,有一個情節和次要情節之間沒有區別。而通常指圖中的單個軸,子圖指的是圖中多個軸中的一個。但是對於matplotlib,它們都是軸實例。

plt通常指的是pyplot模塊(如果導入爲import matplotlib.pyplot as plt)。該模塊具有許多功能,例如您列出的功能以及許多繪圖功能(例如,plot,scatter等)。但是,這些僅僅是爲了方便,並在當前軸實例上調用相應的功能。以非常簡單的方式plt.plot建成這樣的:

def plot(*args, **kwargs): 
    ax = plt.gca() 
    return ax.plot(*args, **kwargs) 

不幸的是,命名是不相符的。但通常軸的方法使用set_*get_*,而plt功能則不使用。

這意味着您可以使用fig, ax = plt.subplots(1)創建您發佈的第一個繪圖,然後繼續使用座標軸方法:ax.plot(...)等。

至於你的問題:

  1. 我不這麼認爲。但作爲一般建議,我會嘗試使用軸方法,因爲它們提供所有功能,而plt 等效是有限的。
  2. matplotlib受到Matlab繪圖功能的啓發。這就是plt功能的來源。
  3. 我不確定是否有gerenal的做法。通常情況下可以通過名稱來猜測(例如plt.title - >ax.set_title)。使用matplotlib通常也意味着擁有手頭的文檔。
  4. 再次,由經驗或名稱。但你不必這樣做。你也可以使用一個!
  5. plt.setp是一個輔助功能。它爲藝術家列表設置了一個屬性(因此它的名字)。因此plt.setp(labels, rotation=45)確實是這樣的:

    for i in labels: 
        i.set_rotation(45) 
    

    的問題是,你是如何得到的參考標籤列表。

    labels = plt.xticklabels() 
    

    labels = ax.get_xticklabels() 
    
+0

@WakanTanka沒有這個回答你的問題:你可以通過任何讓他們? – hitzg

+0

感謝您的回覆,並對您的答覆感到抱歉。這部分回答了這個問題。如果情節與情節之間沒有區別,是否意味着每個情節必須至少有一個情節?另外,如果我有一個子圖,那麼我可以通過兩種方式訪問​​它(一個通過繪圖,另一個通過軸):'plt.xlim(x_min,x_max)'或'ax.set_xlim(x_min,x_max)'?你在第四次回覆中的意思是「你也可以用一個!」,我不是母語英語的人,對不起。你是什​​麼意思的幫助功能?是不是應該直接使用函數,而是使用其他函數? –