I am trying to make two subplots as a row in matplot lib.
Here is my code
fig, (ax1, ax2) = plt.subplots(1,2)
ax1 = plt.bar(x="Topic", height='perc', data=df1)
ax2 = plt.bar(x="Topic", height='perc', data=df2)
What is happening is that the barplots overlap eachother and leave col1 empty.
How can I fix this
Related
I've coded two lines that show two y-axis (coffee and tea) and one x-axis(year). And it's all good, I need some tweaking with the headers. however I can't fix the legends. They either get separated or i can see one but not the other. What am I doing wrong?
my code:
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(year, coffee, 'b-')
ax1.set_ylabel("Coffee") ax1.grid()
ax2.plot(year, tea, 'r--')
ax1.set_ylim(6, 12)
ax2.set_ylim(0.1, 0.5)
ax1.legend(["Coffee"], loc=(0.8, 0.9))
ax2.legend(["Tea"], loc=(0.8, 0.8))
ax1.set_facecolor((1.0, 0.8, 0.6))
plt.show()
I was trying to plot 2 seaborn plots near each other, however it results in:
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(20,8))
sns.jointplot(df['target'], df['bedrooms'], alpha=0.5,color='r', ax=ax1)
plt.xlim(0,5)
plt.ylim(0,10)
plt.xlabel('Price')
plt.ylabel('Number of bedrooms')
sns.jointplot(df['target'], df['bathrooms'], alpha=0.5, color='b', ax=ax2)
plt.xlim(0,5)
plt.ylim(0,10)
plt.xlabel('Price')
plt.ylabel('Number of bathrooms')
plt.show()
And as output I have:
Two empty figures and then in column my two plots
I want 3 rows of subplots each of different widths, but which all share the same X-axis, such as in the rough mock-up below. How can I do this? Can I use sharex=True even in GridSpec-adjusted plots?
You can place the axes by hand, or another method is to use an inset_axes:
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(3, 1, constrained_layout=True, sharex=True, sharey=True)
ylim=[-3, 3]
axs[2].plot(np.random.randn(500))
axs[2].set_ylim(ylim)
xlim = axs[2].get_xlim()
ax0 = axs[0].inset_axes([300, ylim[0], xlim[1]-300, ylim[1]-ylim[0]], transform=axs[0].transData)
ax0.set_ylim(ylim)
ax0.set_xlim([300, xlim[1]])
axs[0].axis('off')
ax0.plot(np.arange(300, 500), np.random.randn(200))
ax1 = axs[1].inset_axes([150, ylim[0], xlim[1] - 150, ylim[1]-ylim[0]], transform=axs[1].transData)
ax1.set_ylim(ylim)
ax1.set_xlim([150, xlim[1]])
axs[1].axis('off')
ax1.plot(np.arange(150, 500), np.random.randn(350))
plt.show()
You can pass which axes to use as reference for sharing axes when you create your subplot
fig = plt.figure()
gs = matplotlib.gridspec.GridSpec(3,3, figure=fig)
ax1 = fig.add_subplot(gs[0,2])
ax2 = fig.add_subplot(gs[1,1:], sharex=ax1)
ax3 = fig.add_subplot(gs[2,:], sharex=ax1)
ax1.plot([1,5,0])
I have the following python code to plot a 2x2 set of graphs.
I would like to make the yaxis show its values on both columns (show Duration numbers on the right as well).
I am ok with the X axis being shown only for the lower row.
How can I do that?
import matplotlib.pyplot as plt
builds = ['20191006.1','20191004.1']
totals_10t = [39671486, 39977577]
totals_1t = [9671486, 3977577]
means_10t = [96160,99630]
means_1t = [9160,9630]
fig, axs = plt.subplots(2, 2, sharex=True,sharey=False, squeeze=False)
fig.suptitle('perf results')
axs[0,0].plot(builds, totals_10t)
axs[0,0].set_title('10T Totals')
axs[0,1].plot(builds, totals_1t, 'tab:orange')
axs[0,1].set_title('1T Totals')
axs[0,1].set_ylabel('Duration(ms)')
axs[0,1].yaxis.tick_right()
axs[1,0].plot(builds, means_10t, 'tab:green')
axs[1,0].set_title('10T Means')
axs[1,1].plot(builds, means_1t, 'tab:red')
axs[1,1].set_title('1T Means')
axs[1,1].yaxis.tick_right()
axs[1,1].set_ylabel('Duration(ms)')
for ax in axs.flat:
ax.set(xlabel='Build',ylabel='Duration(ms)')
for ax in axs.flat:
ax.label_outer()
plt.show()
I want to prepare some hexbin plots from Pandas. My initial code is:
fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)
df2.plot(kind='hexbin', x='var1', y='var2', C='var3', reduce_C_function=np.median, gridsize=25,vmin=0, vmax=40,ax=ax1)
ax1.set_xlim([-5,2])
ax1.set_ylim([0,7])
However when I change this to:
fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
And plot create four subplots similar to the first example it turns off the xlabels and xticklabels.
What code to I need to switch them back on? And is this something I can do as a defaults?