我有这个代码,我可以控制以下属性: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标签?