如何将ymin和ymax重置为主要刻度值?

时间:2019-07-08 20:50:58

标签: python plot

  

我想重置y轴的最大值和最小值以对应   轴2的主要刻度值。

     

如果我分手代码,我可以在Jupyter中完成它,但不能作为一个整体来完成。

t=np.array(x)
data1 = np.array(y)
data2 = np.array(y)


%matplotlib inline
fig, ax = plt.subplots()
title1='This is the Title'
color1='black'
color2='blue'
color3='green'

ylabel1='y-axis 1'
ylabel2='y-axis 2'
ylabel3='y-axis 3'


#fig.set_picker()

ax.set_title(title1)
ax.set_ylabel(ylabel1, color=color1)
ax.tick_params(axis = 'y', labelcolor = color1)
ax.spines['right'].set_color(color1)
ax.plot(t,t,color=color1)


ax2 = ax.twinx()
ax2.plot(t,data1,color=color2)
ax2.yaxis.set_label_position("right")
ax2.set_ylabel(ylabel2, color = color2)
ax2.tick_params(axis = 'y', colors=color2)
ax2.spines['right'].set_color(color2)


ax3 = ax.twinx()
ax3.plot(t,data1**2,color=color3)
ax3.set_ylabel(ylabel3, color=color3)
ax3.spines['right'].set_position(("axes",1.2))
ax3.spines['right'].set_color(color3)
ax3.yaxis.set_label_position("right")
ax3.tick_params(axis = 'y', labelcolor = color3)


#Setting the y axis to the min and max major tick labels
#Getting the major tick labels from the textobject
majorticks = [z.get_text().replace('−','-') for z in ax2.get_yticklabels()]

#Parsing the list to contain only what can be converted to a float object
ticklist = []

for item in majorticks:
    try:
        print(item)
        ticklist.append(float(item))
    except ValueError:
        continue

#Setting the New Limits
ax2.set_ylim(min(ticklist),max(ticklist))
  

我希望输出重新定向y轴,以将数据限制为   (-2和-1)的边界

0 个答案:

没有答案