python子图命名_python – 用于子图的plt.setp替代或如何在x轴...
我有這個代碼,我可以控制以下屬性:x軸范圍,標題,xlabel,ylabel,圖例,網格,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)
# https://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標簽?
總結
以上是生活随笔為你收集整理的python子图命名_python – 用于子图的plt.setp替代或如何在x轴...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华云·云场景应用详解 | 场景体验——桌
- 下一篇: 新华三 VDI java,鱼和熊掌兼得: