Hans Petter Langtangen 12 роки тому
батько
коміт
49565e0b42
2 змінених файлів з 42 додано та 21 видалено
  1. 10 9
      pysketcher/MatplotlibDraw.py
  2. 32 12
      pysketcher/shapes.py

+ 10 - 9
pysketcher/MatplotlibDraw.py

@@ -324,7 +324,7 @@ self.ax.plot(x, y, linewidth=%d, color='gray',
 
 
     def display(self, title=None):
-        """Display the figure. Last possible command."""
+        """Display the figure."""
         if title is not None:
             self.mpl.title(title)
             if self.instruction_file:
@@ -335,12 +335,13 @@ self.ax.plot(x, y, linewidth=%d, color='gray',
         if self.instruction_file:
             self.instruction_file.write('mpl.draw()\n')
 
-    def savefig(self, filename):
-        """Save figure in file."""
+    def savefig(self, filename, dpi=None):
+        """Save figure in file. Set dpi=300 for really high resolution."""
         # If filename is without extension, generate all important formats
         ext = os.path.splitext(filename)[1]
         if not ext:
-            self.mpl.savefig(filename + '.png', dpi=300)
+            self.mpl.savefig(filename + '.png', dpi=dpi)
+            # Crop the PNG file
             failure = os.system('convert -trim %s.png %s.png' %
                                 (filename, filename))
             if failure:
@@ -352,12 +353,12 @@ self.ax.plot(x, y, linewidth=%d, color='gray',
                 print 'pdfcrop is not installed - needed for cropping PDF files'
             #self.mpl.savefig(filename + '.eps')
             if self.instruction_file:
-                self.instruction_file.write('mpl.savefig("%s.png", dpi=300)\n'
-                                            % filename)
+                self.instruction_file.write('mpl.savefig("%s.png", dpi=%s)\n'
+                                            % (filename, dpi))
                 self.instruction_file.write('mpl.savefig("%s.pdf")\n'
                                             % filename)
         else:
-            self.mpl.savefig(filename, dpi=300)
+            self.mpl.savefig(filename, dpi=dpi)
             if ext == '.png':
                 failure = os.system('convert -trim %s %s' % (filename, filename))
                 if failure:
@@ -368,8 +369,8 @@ self.ax.plot(x, y, linewidth=%d, color='gray',
                     print 'pdfcrop is not installed - needed for cropping PDF files'
 
             if self.instruction_file:
-                self.instruction_file.write('mpl.savefig("%s", dpi=300)\n'
-                                            % filename)
+                self.instruction_file.write('mpl.savefig("%s", dpi=%s)\n'
+                                            % (filename, dpi))
 
 
     def text(self, text, position, alignment='center', fontsize=0,

+ 32 - 12
pysketcher/shapes.py

@@ -626,9 +626,14 @@ class SketchyFunc1(Spline):
     A typical function curve used to illustrate an "arbitrary" function.
     """
     domain = [1, 6]
-    def __init__(self, name=None, name_pos='start'):
-        x = [1, 2,   3,   4, 5,   6]
-        y = [5, 3.5, 3.8, 3, 2.5, 2.4]
+    def __init__(self, name=None, name_pos='start',
+                 xmin=1, xmax=6, ymin=2.4, ymax=5):
+        x = array([1, 2,   3,   4, 5,   6])
+        y = array([5, 3.5, 3.8, 3, 2.5, 2.4])
+        # Scale x and y
+        x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
+        y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
+
         Spline.__init__(self, x, y)
         self.shapes['smooth'].set_linecolor('black')
         if name is not None:
@@ -639,10 +644,15 @@ class SketchyFunc3(Spline):
     A typical function curve used to illustrate an "arbitrary" function.
     """
     domain = [0, 6]
-    def __init__(self, name=None, name_pos='start'):
-        x = [0, 2,   3,   4, 5,   6]
-        y = [2, 3.5, 3.8, 2, 2.5, 2.6]
-        y = [0.5, 3.5, 3.8, 2, 2.5, 3.5]
+    def __init__(self, name=None, name_pos='start',
+                 xmin=0, xmax=6, ymin=0.5, ymax=3.8):
+        x = array([0, 2,   3,   4, 5,   6])
+        #y = array([2, 3.5, 3.8, 2, 2.5, 2.6])
+        y = array([0.5, 3.5, 3.8, 2, 2.5, 3.5])
+        # Scale x and y
+        x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
+        y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
+
         Spline.__init__(self, x, y)
         self.shapes['smooth'].set_linecolor('black')
         if name is not None:
@@ -654,9 +664,14 @@ class SketchyFunc4(Spline):
     Can be a companion function to SketchyFunc3.
     """
     domain = [1, 6]
-    def __init__(self, name=None, name_pos='start'):
-        x = [0, 2,   3,   4, 5,   6]
-        y = [1.5, 1.3, 0.7, 0.5, 0.6, 0.8]
+    def __init__(self, name=None, name_pos='start',
+                 xmin=0, xmax=6, ymin=0.5, ymax=1.8):
+        x = array([0, 2,   3,   4, 5,   6])
+        y = array([1.5, 1.3, 0.7, 0.5, 0.6, 0.8])
+        # Scale x and y
+        x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
+        y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
+
         Spline.__init__(self, x, y)
         self.shapes['smooth'].set_linecolor('black')
         if name is not None:
@@ -667,13 +682,18 @@ class SketchyFunc2(Shape):
     A typical function curve used to illustrate an "arbitrary" function.
     """
     domain = [0, 2.25]
-    def __init__(self, name=None, name_pos='end'):
+    def __init__(self, name=None, name_pos='end',
+                 xmin=0, xmax=2.25, ymin=0.046679703125, ymax=1.259375):
 
         a = 0; b = 2.25
         resolution = 100
         x = linspace(a, b, resolution+1)
         f = self  # for calling __call__
         y = f(x)
+        # Scale x and y
+        x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
+        y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
+
         self.shapes = {'smooth': Curve(x, y)}
         self.shapes['smooth'].set_linecolor('black')
 
@@ -2244,7 +2264,7 @@ def rolling_wheel(total_rotation_angle):
     w1 = Wheel(center=center, radius=radius, inner_radius=0.5, nlines=7)
     for i in range(int(total_rotation_angle/angle)):
         w1.draw()
-        print 'XXXXXXXXXXXXXXXXXXXXXX BIG PROBLEM WITH ANIMATE!!!'
+        print 'XXXX BIG PROBLEM WITH ANIMATE!!!'
         display()