A memo when plotting the composition of the amino acid sequence (20xN dimension).
Creating a stacked plot is fairly easy. You can specify bottom = hoge
in bar
.
However, when there were a lot of things coming out, it was quite difficult to adjust the location of the legend. Adjust the width of the plot with get_position
and set_position
→ When using legend
, adjust the position with bbox_to_anchor
.
reference: http://geetduggal.wordpress.com/2011/08/22/grabbing-individual-colors-from-color-maps-in-matplotlib/ http://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot http://matplotlib.org/users/legend_guide.html#plotting-guide-legend
plot_test.py
amino_count = ... # something;Here 20x23 dimensional np.array
accent = get_cmap('accent') #Get a colormap
figsize(10, 8) #With the size of the figure
rcParams['font.size'] = 14 #Make the letters a little larger
total = zeros(23) #Save sum for stacking
ax = subplot(111)
for i in xrange(20): #For each of the 20 amino acids
c = accent(i / 20.0) #colormap returns the corresponding RGBA value if you enter a value between 0 and 1.
if i == 0:
ax.bar(arange(23), amino_count[i], color=c, label=aa[i])
else:
ax.bar(arange(23), amino_count[i], color=c, bottom=total, label=aa[i])
#You can set the lower limit of the bar by specifying bottom.
total += tmd_count[i]
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.80, box.height])
#Set the width of the plot to 80%.
h,l = ax.get_legend_handles_labels()
ax.legend(h[::-1], l[::-1], bbox_to_anchor=(1.25, 1.05))
# get_legend_handles_labels()To get the handle and label of the legend.
#Using this, the legend is shown in reverse order.
#Also bbox_to_The position of the legend is adjusted by using the anchor.
xlabel('Position from the N-terminal side of TMD', fontsize=16)
ylabel('Count', fontsize=16)
Recommended Posts