我有這個代碼,我可以控制屬性如: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()
此代碼返回這個數字:
我希望能夠控制所有這些屬性,但創建多個次要情節,我嘗試這是在這裏:
#!/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()
此代碼返回該圖中:
此代碼具有限制,因爲我無法設置在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")
問題:
- 是否相似表存在,但對於情節和次要情節之間的所有相應的方法呢?
- 可能是更多的哲學問題;爲什麼兩個不同的命名約定?
- 我通過運行
ipdb
下的代碼,敲擊選項卡並記下方法的熟悉名稱,創建了此表作爲試用錯誤。有沒有更好的辦法?我可以想象一些將比較方法返回類型,參數等的東西。 - 如何從另一個方法中找到另一種方法?
- 最後,如何在子圖下旋轉x標籤?
@WakanTanka沒有這個回答你的問題:你可以通過任何讓他們? – hitzg
感謝您的回覆,並對您的答覆感到抱歉。這部分回答了這個問題。如果情節與情節之間沒有區別,是否意味着每個情節必須至少有一個情節?另外,如果我有一個子圖,那麼我可以通過兩種方式訪問它(一個通過繪圖,另一個通過軸):'plt.xlim(x_min,x_max)'或'ax.set_xlim(x_min,x_max)'?你在第四次回覆中的意思是「你也可以用一個!」,我不是母語英語的人,對不起。你是什麼意思的幫助功能?是不是應該直接使用函數,而是使用其他函數? –