【发布时间】:2021-07-19 00:46:35
【问题描述】:
我的代码:
#importing required libraries
import numpy as np
from matplotlib.patches import Polygon
import matplotlib.pyplot as plt
#plotting data
def func(x):
return (x-3)*(x+2)*(3*x+5)+25
a,b=2,9
x=np.linspace(0,10,30)
y=func(x)
fig,ax=plt.subplots(1,1,dpi=135)
ax.plot(x,y,linewidth=2.5,color='c')
ix=np.linspace(a,b)
iy=func(ix)
verts=[(a,0),*zip(ix,iy),(b,0)]
poly=Polygon(verts,facecolor='0.7',edgecolor='0.9')
ax.add_patch(poly)
ax.set_ylim(bottom=0)
ax.text(6.5,150,r'$\int_a^b f(x)$',horizontalalignment='center',fontsize=15)
ax.set_xlabel('X')
ax.set_ylabel('Y',rotation=0)
ax.set_xticks((a,b))
ax.set_xticklabels(('a','b'))
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.set_yticks([])
plt.show()
以上代码给出:
我的问题:
如您所见,ylabel 'Y' 与 y 轴相接触,xlabel 'X' 和 x 轴之间有空间
如何设置坐标轴和标签之间的空间(就像 xlabel 和 x 轴之间有空间一样)?
我的尝试:
在上面的代码中我注释了set_ylabel()方法并尝试了text()方法
#ax.set_ylabel('Y',rotation=0)
fig.text(0.1,0.5,'Y')
#this code creating a little space between y label and y-axis but I want the same amount of space that is between x label an x axis
预期输出:
【问题讨论】:
标签: python numpy matplotlib data-visualization