working version with matplotlib