如何在Matplotlib中为共享x轴使用相同的标签?
我试图将两个单独的图形转换为一个共享x轴的图形。但我还缺少一些东西。对于样式和记号,我通常使用代码如何在Matplotlib中为共享x轴使用相同的标签?,matplotlib,Matplotlib,我试图将两个单独的图形转换为一个共享x轴的图形。但我还缺少一些东西。对于样式和记号,我通常使用代码 plt.xlabel(r'$\mathrm{2\theta\ (deg)}$') plt.ylabel(r'$\mathrm{Intensity\ (a.u)}$') plt.xlim(15,80) plt.legend(loc=4,prop={'size':10}) params = {'legend.fontsize': 18, 'axes.labelsize': 18,
plt.xlabel(r'$\mathrm{2\theta\ (deg)}$')
plt.ylabel(r'$\mathrm{Intensity\ (a.u)}$')
plt.xlim(15,80)
plt.legend(loc=4,prop={'size':10})
params = {'legend.fontsize': 18,
'axes.labelsize': 18,
'axes.titlesize': 18,
'xtick.labelsize' :12,
'mathtext.fontset': 'cm',
'mathtext.rm': 'serif', }
matplotlib.rcParams.update(params)
plt.tick_params(
axis='both', which='both',
right='off', left='off',
top = 'off', bottom = 'off',
labelleft='off')
现在,我需要将其应用于此共享图。其中包括:
- 数字上不会有刻度
- 轴标签将被共享
- 最好在循环中加载文本文件
下面的代码可能更像您想要的
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
params = {'legend.fontsize': 18,
'axes.labelsize': 18,
'axes.titlesize': 18,
'xtick.labelsize' :12,
'mathtext.fontset': 'cm',
'mathtext.rm': 'serif',
"xtick.bottom" : False,
"ytick.left" : False,
}
matplotlib.rcParams.update(params)
f, axes = plt.subplots(nrows=2, sharex=True)
plt.subplots_adjust(hspace=0.001, bottom=0.2)
colors=["blue", "red"]
for i in [0,1]:
data = np.loadtxt("ES{}.txt".format(i+1))
POS = data[:,0]
ESD = data[:,1]
axes[i].plot(POS, ESD, color=colors[i], label="data{}".format(i))
axes[i].legend(loc=4,prop={'size':10})
# make ticks invisble
axes[0].set_yticks([])
axes[1].set_yticks([])
plt.xlabel(r'$\mathrm{2\theta\ (deg)}$')
plt.xlim(15,80)
#create subplot just for placing the ylabel centered on all plots
shadowaxes = f.add_subplot(111, xticks=[], yticks=[], frame_on=False)
shadowaxes.set_ylabel(r'$\mathrm{Intensity\ (a.u)}$')
plt.savefig(__file__ + '.png', dpi=600, bbox_inches='tight')
plt.show()
不清楚你在问什么。您显示的图像是由代码生成的图像吗?如果是这样,我看不出有什么问题。请注意,当询问某些代码的问题时,必须坚持并提供一个@ImportanceOfBeingErnest感谢您的建议。我已经编辑了我的帖子,我希望现在更清楚。没错!我还希望强度(a.u)(ylabel)居中,并从与脚本位于同一目录的.txt文件中读取数据,其中第一列是POS,第二列是该目录中每个文本文件的ESD。感谢您的@importanceofbeingernest此给我键错误:“i”。我的文本文件名为:ES1.txt、ES2.txt。我试图改变[1,3]中I的范围,但没有成功。现在我正在为glob(“*.txt”)中的I使用
:POS,EDS=np.genfromtxt(I,unpack=True)轴**[I]**.plot(POS,ESD,color=colors[I],label=“data{}.format(I))轴**[I]**.legend(loc=4,prop={'size':10})
我可能会使用2作为循环,并为[I]使用适当的值因为我是一个文件名,所以我需要一个整数。因此,如果您的文件是ES1.txt、ES2.txt等,您可以使用np.loadtxt(“ES{}.txt.format(i+1))
。我编辑了答案。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
params = {'legend.fontsize': 18,
'axes.labelsize': 18,
'axes.titlesize': 18,
'xtick.labelsize' :12,
'mathtext.fontset': 'cm',
'mathtext.rm': 'serif',
"xtick.bottom" : False,
"ytick.left" : False,
}
matplotlib.rcParams.update(params)
f, axes = plt.subplots(nrows=2, sharex=True)
plt.subplots_adjust(hspace=0.001, bottom=0.2)
colors=["blue", "red"]
for i in [0,1]:
data = np.loadtxt("ES{}.txt".format(i+1))
POS = data[:,0]
ESD = data[:,1]
axes[i].plot(POS, ESD, color=colors[i], label="data{}".format(i))
axes[i].legend(loc=4,prop={'size':10})
# make ticks invisble
axes[0].set_yticks([])
axes[1].set_yticks([])
plt.xlabel(r'$\mathrm{2\theta\ (deg)}$')
plt.xlim(15,80)
#create subplot just for placing the ylabel centered on all plots
shadowaxes = f.add_subplot(111, xticks=[], yticks=[], frame_on=False)
shadowaxes.set_ylabel(r'$\mathrm{Intensity\ (a.u)}$')
plt.savefig(__file__ + '.png', dpi=600, bbox_inches='tight')
plt.show()