On Thu, Sep 10, 2009 at 2:29 PM, Gökhan Sever <gok...@gm...> wrote:
> Hello,
>
> I have a simple bar-chart seen at
> http://img40.imageshack.us/img40/4889/barchart.png
>
> What is the way to plot each bar equally spaced apart from eachother? Any
> simple way without defining custom ticks or manipulating the data?
>
> Homework season has just started here. Lots of matplotting to do...
>
> Thanks.
>
> --
> Gökhan
>
Self replying:
See the resulting image at http://img9.imageshack.us/img9/145/barchart2.png
I am thinking a keyword like "spacing" could be added to the bar(). If it is
set true then an approach similar shown in the 2nd version might followed to
equally space the bars instead of linear or log scaling.
Any other ideas or a hidden keyword to achieve the same plotting?
The code to produce both figures:
import numpy as np
import matplotlib.pyplot as plt
# Load data
diameters, numbers = np.loadtxt('lab1-data', dtype='int8', skiprows=1).T
# 1st version --with linear spacing
width = 1.0
plt.bar(diameters, numbers, width=width, align='edge')
plt.axis(xmin=diameters.min()-2, xmax=diameters.max()+2)
plt.xlabel("Diameter (mm)", fontsize=16)
plt.ylabel("Number of washers (#)", fontsize=16)
plt.xticks(diameters+width/2, diameters)
plt.figure()
# 2nd version --equal spacing
zipped = zip(diameters, numbers)
zipped.sort()
# Unzipping
diameters, numbers = zip(*zipped)
width = 0.4
plt.bar(range(len(diameters)), numbers, width=width, align='edge')
plt.xlabel("Diameter (mm)", fontsize=16)
plt.ylabel("Number of washers (#)", fontsize=16)
plt.xticks(np.arange(len(diameters))+width/2, diameters)
plt.axis(xmin=-width/2, xmax=len(diameters)-width)
plt.show()
#Data
Diameter(mm) Number
11 7
7 44
10 24
51 1
38 2
35 3
21 12
28 16
12 8
16 8
--
Gökhan
|