import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
y = np.linspace(-1, 1, 5)
y2 = np.linspace(-3, 3, 5)
x = np.arange(5)
fig = plt.figure()
ax = fig.subplots()
line1= ax.plot(x,y)
line2 = ax.plot(x,y2)
ax.legend()
ax.set_title('Random data')
ax.set_ylabel('date')
ax.set_xlabel('height')
ax.axvline(2,ymin=0,ymax=1,color='red',linestyle='--',zorder=1)
ax.axhline(0,color='#FF0099',linestyle=':',xmin=0,xmax=1)
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
<matplotlib.lines.Line2D at 0x21de9148af0>
fig2 = plt.figure()
ax2 = fig2.subplots(3, 2)
plt.tight_layout()
ax2[2,1].plot(x,y)
ax2[0,0].plot(x,y2)
[<matplotlib.lines.Line2D at 0x21de8e63a00>]
y1 = np.arange(0,110.10)
y2 = np.random.random(11)
x = np.arange(11)
<Figure size 432x288 with 0 Axes>
fig3 = plt.figure()
ax3 = fig3.subplots(2,2)
plt.tight_layout()
ax3[0,0].plot(x,y2, color='red', label='2010')
ax3[0,0].plot(x,y2, color='green', label='2011')
ax3[0][1].scatter(x,y2)
ax3[1,0].bar(x,y2)
ax3[1,1].barh(x,y2)
ax3[0,0].legend()
<matplotlib.legend.Legend at 0x21de8b419a0>
people = ['student_a','student_b']
studentA = np.array([90,23,75,45])
studentB = np.array([80,63,71,42])
x = np.arange(len(studentA))
fig,ax = plt.subplots()
width=0.3
sb1 = ax.bar(x-width/2, studentA, width)
sb2 = ax.bar(x+width/2, studentB, width, color='green')
ax.set_ylim([0, 100])
ax.legend(people)
ax.set_title('Student A vs Student B')
ax.set_ylabel('Score')
ax.set_xlabel('Exam')
ax.set_xticks([0, 1, 2, 3])
ax.set_xticklabels(["Maths", "English", "Science", "ICT"])
for i in range(len(studentA)):
sb1[i].set_linewidth(3.5)
sb2[i].set_linewidth(3.5)
if studentA[i]<50:
sb1[i].set_edgecolor('red')
elif studentB[i]<50:
sb2[i].set_edgecolor('red')
fig, ax = plt.subplots()
ax.set_xlim([0, 20])
ax.set_ylim([-10, 10])
start_x = 2.5
start_y = -2.5
width = 5
height = 6
facecolor = 'gray'
rectangle = plt.Rectangle((start_x, start_y), width, height, fc=facecolor)
ax.add_patch(rectangle)
rectangle = plt.Rectangle((5, -2.5), 1, 2, fc='green')
ax.add_patch(rectangle)
radius = 0.5
circle = plt.Circle((4, 1.5), radius=0.5, fc='yellow', ec='black', lw=1)
ax.add_patch(circle)
start_xy = np.array([2.5, 3.5])
end_xy = np.array([7.5, 3.5])
top_xy = ([5, 5])
triangle = Polygon(np.vstack([start_xy, end_xy, top_xy]), color='purple')
ax.add_patch(triangle)
<matplotlib.patches.Polygon at 0x1efdf0a9c70>
fig.savefig('house.jpg')