1

Ive been struggling with this for a while and figured it was time to come here. Essentially I have two subplots being graphed and they're totally fine except for one thing, the x axis. For some reason one subplot's x-axis is coming out perfectly and the other's is not. Here is my Code:

## BAR PLOTS
#expected value vs probability of choosing option1

fig,ax = plt.subplots(1, 2,  dpi=320)




data.plot(kind='bar', y='value_1', ax=ax[0],  color ='red')
data.plot(kind='bar', y='p_1', ax=ax[1],  color ='blue')

#ax.set_xlabel("Trials")
#ax.set_ylabel("Value 1 / P_1")

#plt.xticks(np.arange(0, len('value_1')+1, 5), np.arange(0, len('value_1')+1, 5) ) 

#ticks = range(0, 500, 5)
#labels = ticks
#plt.xticks(ticks, labels)
plt.xticks(np.arange(0, len(data.value_1)+1, 5), np.arange(0, len(data.value_1)+1, 5) ) 
# plt.xticks(np.arange(0, len(data.p_1)+1, 5), np.arange(0, len(data.p_1)+1, 5) ) 
#ax.legend(["Value 1, P_1"])
plt.title(' Expected Vs. Probability')

fig.savefig("figure.pdf")

plt.show()

Here is the output:

enter image description here

BossaNova
  • 1,509
  • 1
  • 13
  • 17
The Don
  • 43
  • 11
  • you are only calling `xticks` on the second plot. insert the same call between your `data.plot` calls, or do `ax[0].set_xticks(...)` – Jody Klymak Jul 13 '20 at 17:54
  • I inserted the same call on in-between the plots like you recommended and it did not do anything. Any idea why? – The Don Jul 13 '20 at 18:04

1 Answers1

1

Try using set_xticks for each ax array:

ax[0].set_xticks(np.arange(0, len(data.value_1)+1, 5))
ax[1].set_xticks(np.arange(0, len(data.value_1)+1, 5))

As you did not provide data I cannot check this, but in principle the set_xticks should work per ax array.

BossaNova
  • 1,509
  • 1
  • 13
  • 17
  • Thanks for the help in advance. Im getting the following error from that: – The Don Jul 13 '20 at 18:57
  • Users/Programming/opt/anaconda3/envs/PST_env/lib/python3.7/site-packages/ipykernel_launcher.py:9: MatplotlibDeprecationWarning: Passing the minor parameter of set_xticks() positionally is deprecated since Matplotlib 3.2; the parameter will become keyword-only two minor releases later. if __name__ == '__main__': – The Don Jul 13 '20 at 18:57
  • The Code: ``` fig,ax = plt.subplots(1, 2, dpi=320) data.plot(kind='bar', y='value_1', ax=ax[0], color ='red') ax[1].set_xticks(np.arange(0, len(data.value_1)+1, 5), np.arange(0, len(data.value_1)+1, 5) ) data.plot(kind='bar', y='p_1', ax=ax[1], color ='blue') plt.xticks(np.arange(0, len(data.p_1)+1, 5), np.arange(0, len(data.p_1)+1, 5) ) # plt.xticks([1,5,10,15,20,25,30,35,45,50]) plt.margins(0.2) plt.subplots_adjust(bottom=0.15) plt.title(' Expected Vs. Probability') fig.savefig("figure.pdf") plt.show() – The Don Jul 13 '20 at 18:58
  • I have version 3.1 for now and you did not provide data for testing, but can I ask why you use a tuple? Why not just one array: np.arange(0, len(data.value_1)+1, 5))? Why do you input 2 arrays? – BossaNova Jul 14 '20 at 05:52
  • ... I edited my answer as per the comments above. Does this work? – BossaNova Jul 14 '20 at 06:14
  • You're the Goat – The Don Jul 14 '20 at 17:39