I am facing problems for plotting figures using polycollection. The code below should do:
1- set two triangles: one with vertices at [0,0 0,1 1,0] and the other at [1,0 0,1 1,1] 2- give two random values to them 3- add these polygons to polycollection and set their values 4- plot the triangles edges only with triplot and save the eps 4.5- plot again with triplot and save the eps (I will explain why I am doing this) 5- plot the image with the values (using add_collection) and save as eps 6- set two new random values to these triangles (this step is not needed to reproduce the error) 7- plot again the image (using add_collection) and save as eps Steps 1 to 6 seem to be ok. The problem is that on step 7, the triangles are displaced in x and y directions. The graph is shown correctly on screen, however this displacement appears on the output file. This displacement does not appears when I plot twice the mesh with triplot. Only when I plot using add_collection. Thanks. Fernando. my settings are: matplotlib version: 1.0.1 matplotlib obtained from: deb http://ppa.launchpad.net/valavanisalex/matplotlib/ubuntu natty main system: Linux kalman 2.6.38-10-generic #46-Ubuntu SMP Tue Jun 28 15:07:17 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux no customizations on matplotlibrc running python with --verbose-helpful option returns $HOME=/home/fernando CONFIGDIR=/home/fernando/.matplotlib matplotlib data path /usr/share/matplotlib/mpl-data loaded rc file /etc/matplotlibrc matplotlib version 1.0.1 verbose.level helpful interactive is False units is False platform is linux2 Using fontManager instance from /home/fernando/.matplotlib/fontList.cache backend TkAgg version 8.5 findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=medium to DejaVu Sans (/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf) with score of 0.100000 #------------------------------- # CODE - CODE - CODE #------------------------------- # -*- coding: utf-8 -*- import os import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.tri as tri from matplotlib.collections import PolyCollection import numpy as np class triangles_mesh(): def __init__(self,coords,topology,colormap=cm.gray): values=np.random.rand(topology.shape[0]) self.triangles=self.build_triangles(coords,topology,values,colormap) def build_triangles(self,coords,topology,image_rho,map_colors=cm.jet): (n_elem,n_nodes_local)=topology.shape lista_tri=[] colors=[] values_rho=[] for j in range(0,n_elem): verts = [(coords[i,0], coords[i,1]) for i in topology[j,:]] lista_tri.append(verts) values_rho.append(image_rho[j]) norma = cm.colors.Normalize(vmin=0, vmax=3) poligonos = PolyCollection(lista_tri,lw=0.4,cmap=map_colors,norm=norma) poligonos.set_array(np.array(values_rho)) return poligonos class mesh_2D(): def __init__(self): self.topology=np.array([[0,1,2],[1,3,2]]) self.coords=np.array([[0,0],[1,0],[0,1],[1,1]]) self.roi=np.array([1,2]) self.elem=triangles_mesh(self.coords,self.topology) def plot_mesh(self,file_name=None,flag_show_image=1): fig=plt.figure() my_axis=fig.add_subplot(111,aspect='equal') my_axis.triplot(self.coords[:,0],self.coords[:,1], self.topology,color=[0, 0,0 ],lw=0.4) plt.yticks([]) plt.xticks([]) my_axis.axis('off') x_max,y_max=self.coords.max(0) x_min,y_min=self.coords.min(0) my_axis.axis([x_min, x_max, y_min, y_max]) plt.draw() if file_name!=None: plt.savefig(file_name,transparent=True,format="eps",bbox_inches="tight") if flag_show_image==1: plt.show() plt.close() def plot_values(self,file_name=None,flag_show_image=1): fig=plt.figure() my_axis=fig.add_subplot(111,aspect='equal') my_axis.add_collection(self.elem.triangles) #plt.yticks([]) #plt.xticks([]) #my_axis.axis('off') x_max,y_max=self.coords.max(0) x_min,y_min=self.coords.min(0) my_axis.axis([x_min, x_max, y_min, y_max]) fig.canvas.draw() if file_name!=None: plt.savefig(file_name,transparent=True,format="eps",bbox_inches="tight") if flag_show_image==1: plt.show() plt.close() malha_pd=mesh_2D() malha_pd.plot_mesh("mesh.eps",flag_show_image=0) malha_pd.plot_mesh("mesh1.eps",flag_show_image=0) malha_pd.plot_values("values.eps",flag_show_image=1) # set new values () new_values=np.random.rand(malha_pd.topology.shape[0]) malha_pd.elem.triangles.set_array(np.array(new_values)) #plot again: here the problem appears malha_pd.plot_values("values1.eps",flag_show_image=1) #-------------------------- # END CODE - END CODE #-------------------------- ------------------------------------------------------------------------------ 5 Ways to Improve & Secure Unified Communications Unified Communications promises greater efficiencies for business. UC can improve internal communications as well as offer faster, more efficient ways to interact with customers and streamline customer service. Learn more! http://www.accelacomm.com/jaw/sfnl/114/51426253/ _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users