[PYTHON] Stackable bar plot with matplotlib

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

Stackable bar plot with matplotlib
2-axis plot with Matplotlib
3D plot with matplotlib
Create plot animation with Python + Matplotlib
Continuously color with matplotlib scatter plot
Lognormal probability plot with Python, matplotlib
Animation with matplotlib
Japanese with matplotlib
Animation with matplotlib
Histogram with matplotlib
Animate with matplotlib
Plot ROC Curve for Binary Classification with Matplotlib
Create 3D scatter plot with SciPy + matplotlib (Python)
Heatmap with Python + matplotlib
Band graph with matplotlib
2D plot in matplotlib
Learn with Cheminformatics Matplotlib
Real-time drawing with matplotlib
Various colorbars with Matplotlib
Time series plot / Matplotlib
Adjust axes with matplotlib
How to add color bars in matplotlib bar plot
[Python] How to draw a scatter plot with Matplotlib
Candle chart plot with plotly
Graph Excel data with matplotlib (1)
Try using matplotlib with PyCharm
Graph drawing method with matplotlib
Reformat the timeline of the pandas time series plot with matplotlib
Graph Excel data with matplotlib (2)
3D scatter plot with PyQtGraph
Gradient color selection with matplotlib
Animate multiple graphs with matplotlib
A python graphing manual with Matplotlib.
Inference & result display with Tensorflow + matplotlib
Japaneseize Matplotlib with Alpine using Docker
[Python] font family and font with matplotlib
Draw Japanese with matplotlib on Ubuntu
Draw a loose graph with matplotlib
Versatile data plotting with pandas + matplotlib
Heatmap with Dendrogram in Python + matplotlib
Easy Japanese font setting with matplotlib
Easy to draw graphs with matplotlib
Cases using pandas plot, cases using (pure) matplotlib plot
Draw Lyapunov Fractal with Python, matplotlib
When matplotlib doesn't work with python2.7
Easy animation with matplotlib (mp4, gif)
Write a stacked histogram with matplotlib
Implement "Data Visualization Design # 2" with matplotlib
Plot black and white graphs suitable for papers with matplotlib or pylab
How to draw a bar graph that summarizes multiple series with matplotlib
Plot CSV of time series data with unixtime value in Python (matplotlib)
Plot multiple maps and data at the same time with Python's matplotlib