Dear users,
I was trying to figure out 2 things for my below script.
1) How do I put a correct legend in below plot for both the bar and line?
Also if I wanted add another set of bars(for comparison) side by side with a
different color is ut possible with proper legend?
2) Is there a way to keep the position of text ( eg.
ax.text(np.mean(x),20,"N-SAMPLES: "+str(npts))) in below plot independent of
data based location- but dependent on plot size ie npoints from left of the
plot and mpoints from bottom of the plot? this way it is possible to keep it at
same place even though I use data with different range.
Please help
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Created on Sun Dec 9 16:00:33 2012
@author: sjo"""
from matplotlib import pyplot as p
import sys
import os
import Image,ImageDraw
import numpy as np
in_file='HYC_SLA_MIN_AVISO_WEEKLY.dat'
print "filename =",in_file
stem=os.path.splitext(os.path.basename(in_file))[0]
#make a new figure
fig = p.figure()
img = Image.new("RGB", (300,300), "#FFFFFF")
draw = ImageDraw.Draw(img)
ax = fig.add_subplot(211)
yr=stem
xlab=os.path.join('SLA BINS-' + yr )
x=[ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.,
22., 24., 26., 28., 30.]
y=[0.,24.078,20.158, 15.078, 10.872, 7.751, 5.542,
4.023, 2.974, 2.225, 1.674, 1.269, 0.96 , 0.734,
0.555, 2.105]
z=[ 0., 714200., 597923., 447222., 322480., 229896.,
164373., 119331., 88223., 66005., 49658., 37648.,
28484., 21779., 16469., 62448.]
yc=np.cumsum(y)
rng=['
','00-02','02-04','04-06','06-08','08-10','10-12','12-14','14-16','16-18','18-20','20-22','22-24','24-26','26-28','>28']
group_labels=rng
ax.set_xticks(x)
npts=np.sum(z)
# add a bar plot to the axis, ax.
fc1=ax.bar(x,y,width=1,align='center')
ax.set_xticklabels(group_labels,rotation='80')
ax.yaxis.grid(color='gray', linestyle='dashed')
ax.xaxis.grid(color='gray', linestyle='dashed')
ax.set_ylabel('% OF DATA POINTS',fontsize=12)
ax.set_xlabel(xlab,fontsize=12)
ax.text(np.mean(x),20,"N-SAMPLES: "+str(npts))
ax2 = ax.twinx()
csm1=ax2.plot(x, yc, 'r-')
ax2.set_ylabel('CUM % DATA',fontsize=12)
p.legend((fc1, csm1), ('histogram', 'cumulative'))
for tick in ax.xaxis.get_major_ticks():
tick.label.set_fontsize(14)
for tick in ax.yaxis.get_major_ticks():
tick.label.set_fontsize(14)
outf=os.path.join(stem +'.png')
p.show()
print outf
***************************************************************
Sudheer Joseph
Indian National Centre for Ocean Information Services
Ministry of Earth Sciences, Govt. of India
POST BOX NO: 21, IDA Jeedeemetla P.O.
Via Pragathi Nagar,Kukatpally, Hyderabad; Pin:5000 55
Tel:+91-40-23886047(O),Fax:+91-40-23895011(O),
Tel:+91-40-23044600(R),Tel:+91-40-9440832534(Mobile)
E-mail:[email protected];[email protected]
Web- http://oppamthadathil.tripod.com
***************************************************************
------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users