In [1]:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
In [2]:
def listAttr(name, s = None):
if not s:
return [item for item in dir(name) if not item.startswith("_")]
return [item for item in dir(name) if (not item.startswith("_")) and s.lower() in item.lower()]
pass
In [3]:
plt.scatter([1, 2, 3, 4], [2, 5, 6, 4])
plt.xlabel('X axis')
plt.ylabel('Y axis')
#plt.show()
plt.savefig('scatter.png')
In [4]:
plt.bar([1, 2, 3, 4], [2, 5, 6, 4])
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
In [5]:
plt.bar([1, 2, 3, 4], [2, 5, 6, 4], width=0.2)
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
In [6]:
plt.bar([1, 2, 3, 4], [2, 5, 6, 4], width=0.2, align='edge')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
In [7]:
#help(np.linspace)
In [8]:
np.linspace(-10, 10, 25)
Out[8]:
In [9]:
x = np.linspace(-10, 10, 25)
y1 = x ** 2
y2 = - x ** 2 + 50
In [10]:
print(x)
print(y1)
print(y2)
In [11]:
fig, ax = plt.subplots()
ax.plot(x, y1)
ax.plot(x, y2)
Out[11]:
In [12]:
fig
Out[12]:
In [13]:
ax
Out[13]:
In [14]:
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(x, y1)
ax.plot(x, y2)
Out[14]:
In [15]:
ax
Out[15]:
In [16]:
fig, ax = plt.subplots(figsize=(6, 4))
#matplotlib.pyplot.step(x, y, *args, where='pre', data=None, **kwargs)
ax.step(x, y1)
ax.step(x, y2)
Out[16]:
In [17]:
fig, axes = plt.subplots( nrows=2, ncols=3)
In [18]:
fig
Out[18]:
In [19]:
axes
Out[19]:
In [20]:
# plot a line, implicitly creating a subplot(111)
plt.plot(x)
plt.figure(figsize=(6, 3))
# now create a subplot which represents the top plot of a grid
# with 2 rows and 1 column. Since this subplot will overlap the
# first, the plot (and its axes) previously created, will be removed
plt.subplot(211)
Out[20]:
In [21]:
# plot a line, implicitly creating a subplot(111)
plt.plot(x)
plt.figure(figsize=(6, 3))
plt.subplot(221)
Out[21]:
In [22]:
plt.subplot(221)
# equivalent but more general
ax1=plt.subplot(2, 2, 1)
# add a subplot with no frame
ax2=plt.subplot(222, facecolor='blue')
# add a polar subplot
ax3=plt.subplot(223, projection='polar')
# add a red subplot that shares the x-axis with ax1
ax4= plt.subplot(224, sharex=ax1, facecolor='red')
In [24]:
plt.subplot(221)
# equivalent but more general
ax1=plt.subplot(2, 2, 1)
# add a subplot with no frame
ax2=plt.subplot(222, facecolor='blue')
# add a polar subplot
ax3=plt.subplot(223, projection='polar')
# add a red subplot that shares the x-axis with ax1
ax4= plt.subplot(224, sharex=ax1, facecolor='red')
# delete ax2 from the figure
#plt.delaxes(ax3)
# add ax2 to the figure again
#plt.subplot(ax3)
In [25]:
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(figsize=(15, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.subplot(132)
plt.scatter(names, values)
Out[25]:
In [26]:
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(figsize=(15, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.suptitle('Categorical Plotting')
#plt.show()
plt.savefig('test.png')
In [27]:
x = np.arange(-5, 6, 1)
y1 = 20 * x - 50
y2 = 100 - 20 * x
y3 = x ** 3 - x ** 2
In [28]:
#create Figure and Axis
fig, ax = plt.subplots()
#in the Axis, plot graphs
ax.plot(x, y1, color="blue", label="y(x)")
ax.plot(x, y2, color="red", label="y'(x)")
ax.plot(x, y3, color="green", label="y''(x)")
#set Axis x and y labels
ax.set_xlabel("x")
ax.set_ylabel("y")
#add legend for the graph
ax.legend()
Out[28]:
In [29]:
fig.savefig("xv-plotting.pdf")
In [30]:
fig.savefig("xv-plotting.png")
In [31]:
y = [1,4,3,8,10,6,13,14,16,10]
x = np.arange(10)
plt.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()
fig.savefig('plot.png')
In [32]:
y = [1,4,3,8,10,6,13,14,16,10]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()
fig.savefig('plot.png')
In [33]:
x = [1, 2, 4]
y = [2, 4, 4.5]
#plt.figure(figsize=(10, 5))
plt.plot(x, y)
plt.savefig("out.png")