ploting two subplots in one plot using matplotlib - python

I want to plot two subplot in in one plot using matplotlib
my code is as following:
fig, axes = plt.subplots(ncols=2)
df5.plot(ax=axes[0], kind='bar' ,stacked=True)
ax[0,0].set_title("metagenome data")
plt.xticks(r1, names1)
plt.xlabel("Sample")
plt.legend(loc='upper left', bbox_to_anchor=(1,1), ncol=1)
df_b5.plot(ax=axes[1], kind='bar', stacked=True)
ax[0,1].set_title("Amplicon data")
plt.xticks(r2, names2)
plt.xlabel("Sample")
plt.legend(loc='upper left', bbox_to_anchor=(1,1), ncol=1)
and only the first one is at the plot
what am I doing wrong?
any help will be grate!
TNX!

Related

How to change the order of categorical x-axis in interaction plot in python?

So I am generating the interaction plot by using statsmodels package.
Here in the figure below, you can see my current result.
I would like to have first "Block" and second "Alternating" in my x-axis. I was wondering how to make this change?
Here is my code:
from statsmodels.graphics.factorplots import interaction_plot
import matplotlib.pyplot as plt
fig = interaction_plot(x=d['Group'], trace=d['Condition'], response=d['Asynch'],
colors=['blue','yellow','green','gray'])
plt.rc("figure")
plt.rc('xtick', labelsize=24)
plt.rc('ytick', labelsize=24)
plt.xlabel('Condition', fontsize=28)
plt.ylabel('IRI (ms)', fontsize=28)
sns.set(font_scale = 1.4)
plt. legend(loc='upper center', prop={'size': 24})
plt.legend(loc='upper right')
plt.ylabel("Asynchrony (ms)")
plt.show()
# plt.savefig('interaction.png', dpi=300)
fig.savefig('interaction.png', dpi=600)
and here is the result:
My interaction plot

Matplotlib legend does not match seaborn bubble plot sizes

I have created a bubble plot using seaborn, and used matplotlib to draw the legend to the right of my seaborn plots. I specified the sizing of the bubbles in my seaborn code using sizes=(1,900) but the scaling on my matplotlib legend does not reflect what the plots show. The legend reads from 0 to 45 but the actual data in my plots range from 0 to 900
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(11,4))
sns.scatterplot(y="Min", x="Max",
size="Count", sizes=(1,900), alpha=0.5,
color='r', data=code1, ax=ax1, legend=False)
sns.scatterplot(y="Min", x="Max", alpha=0.5,
color='b', size="Count", sizes=(1,900),
data=code2, ax=ax2, legend=False)
sns.scatterplot(y="Min", x="Max", alpha=0.5,
color='g', size="Count", sizes=(1,900),
data=code3, ax=ax3)
ax3.legend(loc='upper right', bbox_to_anchor=(1.7,1), labelspacing=2,
fontsize=14, frameon=False, markerscale=1)
Here is my plot
I was unable to figure out how seaborn structures the legend output for ingestion by matplotlib. I did learn that my data (code1, code2, and code3) had different min and max values which should have been specified under seaborn's sizes argument. For code1, sizes=(1,900); for code2, sizes=(1,300); for code3, sizes=(1,45). Because I was using matplotlib to draw the legend to the right of code3's plot, the scaling was specific to the rightmost plot rather than for all 3 plots. In the end, I ended up using matplotlib's legend_elements as follows:
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12,4))
scatter = ax1.scatter(y=code1["Min"], x=code1["Max"],
s=code1["Count"],
color='r', alpha=0.5)
ax2.scatter(y=code2["Min"], x=code2["Max"],
color='b', s=code2["Count"], alpha=0.5)
ax3.scatter(y=code3["Min"], x=code3["Max"],
color='g', s=code3["Count"], alpha=0.5)
kw = dict(prop="sizes", num=[10,100,500,900])
legend = ax3.legend(*scatter.legend_elements(**kw), title="Count", fontsize=12,
loc='upper right', bbox_to_anchor=(1.5,1), labelspacing=2,
frameon=False)

adding space between bars of bars on both subplots using matplotlib

I have been using this piece of code for drawing 2 subplots on the same figure. I tried many things for adding space between bars of barh in matplotlib so the labels of y-axis would be readable but I could not fix it:
plt.figure(figsize=(30, 120))
fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0].barh(names, gaps, align='edge', color='green',height=1)
axes[1].barh(names, mems, align='edge', color='blue',height=1)
axes[0].invert_xaxis()
axes[0].set_yticklabels(names, fontsize=5)
axes[0].yaxis.tick_right()
for ax in axes.flat:
ax.margins(0.01)
ax.grid(True)
fig.tight_layout()
fig.subplots_adjust(wspace=0.37)
My current figure looks like this:
my current figure
Do you know how I can make the ylabels readable?

Plots different columns of different dataframe in one plot as scatter plot

I am trying to plot different columns (longitude & latitude ) from different dataframes in one plot. But they are being plotted in different figures separately.
Here is the code I am using
fig,ax=plt.subplots()
cells_final.plot.scatter(x='lon',y='lat')
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red')
plt.show()
How can I plot this in one single figure?
Use the axes instance (ax) created by
fig, ax = plt.subplots()
And pass it as the ax parameter of pandas.DataFrame.plot,
fig,ax=plt.subplots()
cells_final.plot.scatter(x='lon',y='lat', ax=ax)
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax)
plt.show()
Or if you'd rather have the plots on different subplots in the same figure you can create multiple axes
fig, (ax1, ax2) = plt.subplots(1, 2)
cells_final.plot.scatter(x='lon',y='lat', ax=ax1)
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax2)
plt.show()
You need specify the axis:
fig,ax=plt.subplots(1,2, figsize=(12, 8))
cells_final.plot.scatter(x='lon',y='lat', ax=ax=[0])
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax[1])
plt.show()
Thanks #William Miller.......!

How do I increase the width between xaxis ticks in a seaborn facetgrid plot?

I have a seaborn Facetgrid, stripplot
m=sns.FacetGrid(group, col='myGroupCol' , size=15, aspect=0.9, sharex=False, sharey=True)
m.map(sns.stripplot,'myX','myY',hue='myColorBy',data=pandas.groupby(), order=order_list, jitter=0.4, hue_order=\
['T','C','TT','TC','CT','CC'],palette="Set1", split=True, size=15, linewidth=2, edgecolor="gray").set(ylim=(-2,6))
for ax,title in zip(m.axes.flat, sorted(titles.iterkeys())):
ax.tick_params(axis='x', which='major', pad=15)
ax.grid(True)
ax.legend(bbox_to_anchor=(1.05, 1),loc=2)
I have tried to changes the spacing between the sticks using tick params but I don't see any change in width.
Any idea what I am doing wrong?
Well just adjusted the aspect ratio and jitter appropriately and got the desired plot.

Categories