Blog Archive

Thursday, September 13, 2012

How to make your own custom bar graph

import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
from matplotlib.transforms import Affine2D
 
 
#Different sets of dots
#=========================================================
#standard vertices for bar
#------------
#bottom
#leftside
#top
#rightside
#-----------
verts = [
    (0., 0.),
    (1.,0.),
    (1., 2.),
    (0., 2.),
    (0., 0.),
    ]
plt.clf() 
fig = plt.figure()
ax = fig.add_subplot(111)

step=0 #set first step at zero
heights=[1,2,3,3,1] #set list of heights for each step
for h in heights:
    #Move and Scale the Verts
    height=h
    translated_verts=Affine2D().translate(step,0).transform(verts)
    translated_verts=Affine2D().scale(1,height).transform(translated_verts)
    step=step+1
    #How we will Connect the Dots
    #==========================================================
    #How we will link together these vertices
    codes = [Path.MOVETO,
             Path.LINETO,
             Path.LINETO,
             Path.LINETO,
             Path.CLOSEPOLY,
             ]
 
    #Creating the paths from dot to dot
    #===========================================================
    path = Path(verts, codes)
    trspath=Path(translated_verts,codes)
 

    #plot shapes created by connecting dots
    #============================================================

    translatedpatch=patches.PathPatch(trspath,facecolor='green',alpha=0.9,lw=3,edgecolor='black')
    ax.add_patch(translatedpatch)
    
plt.ylabel("amount of apples")
plt.xlabel("height of tree")
plt.title("Harvest")
ax.set_xlim(0,6)
ax.set_ylim(0,10)

No comments:

Post a Comment