Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The trick to both of your questions is understanding that bar graphs in Matplotlib expect each series (G1, G2) to have a total width of "1.0", counting margins on either side. Thus, it's probably easiest to set margins up and then calculate the width of each bar depending on how many of them there are per series. In your case, there are two bars per series.</p> <p>Assuming you left align each bar, instead of center aligning them as you had done, this setup will result in series which span from 0.0 to 1.0, 1.0 to 2.0, and so forth on the x-axis. Thus, the exact center of each series, which is where you want your labels to appear, will be at 0.5, 1.5, etc. </p> <p>I've cleaned up your code as there were a lot of extraneous variables. See comments within.</p> <pre><code>import matplotlib.pyplot as plt import numpy as np plt.figure(figsize=(7,7), dpi=300) groups = [[1.04, 0.96], [1.69, 4.02]] group_labels = ["G1", "G2"] num_items = len(group_labels) # This needs to be a numpy range for xdata calculations # to work. ind = np.arange(num_items) # Bar graphs expect a total width of "1.0" per group # Thus, you should make the sum of the two margins # plus the sum of the width for each entry equal 1.0. # One way of doing that is shown below. You can make # The margins smaller if they're still too big. margin = 0.05 width = (1.-2.*margin)/num_items s = plt.subplot(1,1,1) for num, vals in enumerate(groups): print "plotting: ", vals # The position of the xdata must be calculated for each of the two data series xdata = ind+margin+(num*width) # Removing the "align=center" feature will left align graphs, which is what # this method of calculating positions assumes gene_rects = plt.bar(xdata, vals, width) # You should no longer need to manually set the plot limit since everything # is scaled to one. # Also the ticks should be much simpler now that each group of bars extends from # 0.0 to 1.0, 1.0 to 2.0, and so forth and, thus, are centered at 0.5, 1.5, etc. s.set_xticks(ind+0.5) s.set_xticklabels(group_labels) </code></pre> <p><img src="https://i.stack.imgur.com/uR3Ig.png" alt="Output from my code."></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload