Chapter 14: Graphics Using C

Example 1, Page Number : GUC-4

In [7]:
a = int(raw_input("Enter the first number : "))
b = int(raw_input("Enter the second number : "))

if a > b:
    print a, " IS THE BIGGEST NUMBER"
else:
    print b, " IS THE BIGGEST NUMBER"
    
    
Enter the first number : 5
Enter the second number : 3
5  IS THE BIGGEST NUMBER

Example 2, Page Number : GUC-10

In [2]:
%matplotlib inline

import pylab
import matplotlib.pyplot as plt

x1 = int(raw_input("Enter start point coordinate : "))
y1 = int(raw_input())

x2 = int(raw_input("Enter end point coordinate : "))
y2 = int(raw_input())


pylab.plot([x1,y1],[x2,y2])
Enter start point coordinate : 100
200
Enter end point coordinate : 150
200
Out[2]:
[<matplotlib.lines.Line2D at 0x9feae48>]

Example 3, Page Number : GUC-11

In [3]:
%matplotlib inline


import pylab
import matplotlib.pyplot as plt
#Tkinter package is used for graphics
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection

e = Rectangle(xy=(35, -50), width=80, height=80)
fig = plt.gcf()
fig.gca().add_artist(e)
#e.set_clip_box(ax.bbox)
e.set_alpha(0.7)
pylab.xlim([20, 50])
pylab.ylim([-65, -35])
Out[3]:
(-65, -35)

Example 4, Page Number : GUC-13

In [5]:
%matplotlib inline

import pylab
import matplotlib.pyplot as plt


pylab.plot([20,50],[40,100])
pylab.plot([50,70],[100,40])
pylab.plot([20,70],[40,40])
Out[5]:
[<matplotlib.lines.Line2D at 0xa289e80>]

Example 5, Page Number: GUC-17

In [6]:
%matplotlib inline


import pylab
import matplotlib.pyplot as plt
#Tkinter package is used for graphics
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection

e = Rectangle(xy=(35, -50), width=80, height=80)
fig = plt.gcf()
fig.gca().add_artist(e)
#e.set_clip_box(ax.bbox)
e.set_alpha(0.7)
pylab.xlim([20, 50])
pylab.ylim([-65, -35])
Out[6]:
(-65, -35)

Example 6, Page Number : GUC-18

In [7]:
%matplotlib inline


import pylab
import matplotlib.pyplot as plt
#Tkinter package is used for graphics
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection

e = Rectangle(xy=(35, -50), width=80, height=80)
fig = plt.gcf()
fig.gca().add_artist(e)
#e.set_clip_box(ax.bbox)
e.set_alpha(0.7)
pylab.xlim([20, 50])
pylab.ylim([-65, -35])
Out[7]:
(-65, -35)

Example 7: Page Number: GUC-22

In [8]:
%matplotlib inline

import pylab
cir = pylab.Circle((.5,.5),radius = 0.25)

ax =pylab.axes(aspect=1)
ax.add_patch(cir)
Out[8]:
<matplotlib.patches.Circle at 0x9d8cd68>

Example 8, Page Number:GUC-24

In [9]:
%matplotlib inline

import pylab
cir = pylab.Circle((.5,.5),radius = 0.25)
rect = pylab.Rectangle([5,5],1,1)

ax =pylab.axes(aspect=1)
ax.add_patch(cir)
ax.add_patch(rect)
Out[9]:
<matplotlib.patches.Rectangle at 0x9ffc4a8>

Example 9, Page Number : GUC-25

In [10]:
%matplotlib inline

import pylab
cir = pylab.Circle((.5,.5),radius = 0.5)

ax =pylab.axes(aspect=1)
ax.add_patch(cir)
Out[10]:
<matplotlib.patches.Circle at 0x9f3f4a8>

Example 10, Page Number : GUC-26

In [12]:
import turtle

turtle.left(90)
turtle.forward(50)
i=0
while i < 180:
    turtle.forward(1)
    turtle.left(1)
    i+=1
    
turtle.forward(150)
turtle.left(90)
turtle.forward(250)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(140)
#you will see similar output as shown below

Example 11, Page Number : GUC-28

In [4]:
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt

OX = [1,2,3,4]
OY = [200,590,670,435]

fig = plt.figure()

width = .35
ind = np.arange(len(OY))
plt.bar(ind, OY)
plt.xticks(ind + width / 2, OX)

fig.autofmt_xdate()

Example 12, Page Number : GUC-30

In [5]:
%matplotlib inline

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r'], [200,590,670,435]):
    xs = np.arange(20)
    ys = np.random.rand(20)
    ax.bar(xs, ys, zs=z)
    
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

Example 13, Page Number : GUC-31

In [6]:
%matplotlib inline

from pylab import *

# make a square figure and axes
figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])

# The slices will be ordered and plotted counter-clockwise.

sales = [200, 590, 670, 435]
explode=(0, 0.05, 0, 0)

pie(sales, explode=explode,
                autopct='%1.1f%%', shadow=True, startangle=90)
                # The default startangle is 0, which would start
                # the Frogs slice on the x-axis.  With startangle=90,
                # everything is rotated counter-clockwise by 90 degrees,
                # so the plotting starts on the positive y-axis.



show()

Example 14, Page Number : GUC-34

In [9]:
import turtle

i=0
while i < 360:
    turtle.forward(3)
    turtle.left(3)
    i+=3
    
turtle.penup()
turtle.forward(200)

turtle.pendown()
i=0
while i < 360:
    turtle.forward(3)
    turtle.left(3)
    i+=3
    
turtle.penup()
turtle.forward(100)
turtle.left(90)
turtle.forward(50)
turtle.pendown()
turtle.forward(100)
turtle.left(90)
turtle.forward(400)
turtle.left(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(150)
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(400)
turtle.right(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
#you will see similar output as shown below
In [ ]: