Python 当我为多点图中的不同子图设置不同的Xticklabel时,只使用最后一个
我需要创建一个带有两组不同XTicklabel的多点图。当我使用这个MWE时Python 当我为多点图中的不同子图设置不同的Xticklabel时,只使用最后一个,python,matplotlib,Python,Matplotlib,我需要创建一个带有两组不同XTicklabel的多点图。当我使用这个MWE时 import numpy as np import matplotlib.pyplot as plt arr1 = np.random.random((3,3)) arr2 = np.random.random((3,3)) arr3 = np.random.random((3,3)) arr4 = np.random.random((3,3)) set1_labels = ['Label1', 'Label2',
import numpy as np
import matplotlib.pyplot as plt
arr1 = np.random.random((3,3))
arr2 = np.random.random((3,3))
arr3 = np.random.random((3,3))
arr4 = np.random.random((3,3))
set1_labels = ['Label1', 'Label2', 'Label3']
set2_labels = ['Label4', 'Label5', 'Label6']
fig, ax = plt.subplots(nrows=2,ncols=2, sharex=True, sharey=True, figsize=(18,8))
ax[0,0].plot(arr1.T,lw=3)
ax[0,1].plot(arr3.T,lw=3)
ax[1,0].plot(arr2.T,lw=3)
ax[1,0].set_xticks([0,1,2])
ax[1,0].set_xticklabels(set1_labels)
[a,b,c] = ax[1,1].plot(arr4.T,lw=3)
ax[1,1].legend([a,b,c],['a','b','c'], prop={'size':16})
ax[1,1].set_xticks([0,1,2])
ax[1,1].set_xticklabels(set2_labels)
fig.tight_layout()
plt.savefig('mwe.png')
plt.show()
我得到:
只有set2_标签出现在列的底部,我希望高亮显示的标签显示为“Label1”、“Label2”和“Label3”。我也尝试过使用在上给出的方法,我得到了同样的结果。我做错了什么?谢谢 删除
子批中的sharex
选项:
#this:
fig, ax = plt.subplots(nrows=2,ncols=2, sharey=True, figsize=(18,8))
输出:
删除子批次中的sharex
选项
:
#this:
fig, ax = plt.subplots(nrows=2,ncols=2, sharey=True, figsize=(18,8))
输出:
将sharex=True
更改为sharex='col'
,如下所示:
fig,ax=plt.subplot(nrows=2,ncols=2,sharex='col',sharey='all',figsize=(18,8))
结果如下:
将sharex=True
更改为sharex='col'
,如下所示:
fig,ax=plt.subplot(nrows=2,ncols=2,sharex='col',sharey='all',figsize=(18,8))
结果如下:
但是子批次的顶行没有与底行相同的XTICK。@BigBen我想OP可以分别为每个子批次插入set_xtick
,不只是最下面的子批次:-)。但是子批次的最上面一行的XTICK与最下面一行的XTICK不一样。@BigBen我想OP可以分别为每个子批次插入设置XTICK
,而不仅仅是最下面的子批次:-)。