Matplotlib画布
In [1]:
# 引入包
import matplotlib.pyplot as plt
import numpy as np
In [5]:
# 创建画布
plt.figure()
# 生成数据
data = [2, 4, 6, 7, 11]
# 可视化结果
plt.plot(data) # plot是绘制折线的
plt.show()
设置画布大小
In [4]:
# 创建画布
plt.figure(figsize=(20,5))
#生成数据
random_data = np.random.randn(100)
plt.plot(random_data)
Out[4]:
[<matplotlib.lines.Line2D at 0x219a7eb2b80>]
注意:代码块要放在一个cell中
In [6]:
# 创建画布
plt.figure(figsize=(20,5))
Out[6]:
<Figure size 1440x360 with 0 Axes>
<Figure size 1440x360 with 0 Axes>
In [7]:
#生成数据
random_data = np.random.randn(100)
plt.plot(random_data)
Out[7]:
[<matplotlib.lines.Line2D at 0x219a7f73a00>]
散点图
第七课 数据可视化
第五节 Matplotlib画布
In [1]:
# 引入包
import matplotlib.pyplot as plt
import numpy as np
In [5]:
# 创建画布
plt.figure()
# 生成数据
data = [2, 4, 6, 7, 11]
# 可视化结果
plt.plot(data) # plot是绘制折线的
plt.show()
设置画布大小
In [4]:
# 创建画布
plt.figure(figsize=(20,5))
#生成数据
random_data = np.random.randn(100)
plt.plot(random_data)
Out[4]:
[<matplotlib.lines.Line2D at 0x219a7eb2b80>]
注意:代码块要放在一个cell中
In [6]:
# 创建画布
plt.figure(figsize=(20,5))
Out[6]:
<Figure size 1440x360 with 0 Axes>
<Figure size 1440x360 with 0 Axes>
In [7]:
#生成数据
random_data = np.random.randn(100)
plt.plot(random_data)
Out[7]:
[<matplotlib.lines.Line2D at 0x219a7f73a00>]
散点图和柱状图的绘制
In [8]:
# 引入包
import matplotlib.pyplot as plt
import numpy as np
散点图
In [9]:
plt.figure()
# 生成一组数据
x = np.random.randn(50)
y = x * 2
# 可视化结果
plt.scatter(x, y)
plt.show()
In [14]:
plt.figure()
# 生成两组数据
x = np.random.randn(50)
y1 = x ** 2
y2 = x * 2
# 可视化结果
plt.scatter(x, y1)
plt.scatter(x, y2)
plt.show()
In [15]:
# 使用参数
plt.figure()
plt.scatter(x, y1, s=100, c='r', marker='X')
plt.scatter(x, y2, s=20, c='g',marker='s')
plt.show()
柱状图
In [16]:
# 单组数据
x = [1, 2, 3, 4, 5]
data = [5,2,7,8,2]
plt.bar(x,data)
plt.show()
In [18]:
# 多组数据
x1 = [1, 3, 5, 7, 9]
data1 = [5, 2, 7, 8, 2]
plt.bar(x1, data, color='r')
x2 = np.array(x1) + 1 # 需要设置不同的横坐标,避免重叠
data2 = [8, 6, 2, 5, 6]
plt.bar(x2, data2, color='g')
plt.show()
直方图的绘制
In [19]:
# 引入包
import matplotlib.pyplot as plt
In [20]:
# 准备数据
population_ages = [22, 55, 62, 45, 21, 22, 34, 42, 42, 4, 89, 82, 80, 80, 75, 65, 54, 44, 43, 42, 48]
In [21]:
# 指定分组个数
bins = 5
plt.hist(population_ages, bins)
plt.show()
In [22]:
# 指定分组边界
bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
plt.hist(population_ages,bins,histtype='bar',rwidth=0.8)
plt.show()
矩阵绘图
In [24]:
# 引入包
import matplotlib.pyplot as plt
In [25]:
# 准备数据
m = np.random.rand(10,12)
In [29]:
m
Out[29]:
array([[4.82497939e-01, 6.58595606e-01, 5.29636788e-01, 7.59201397e-01, 1.33073167e-01, 9.93762828e-01, 6.66063777e-01, 4.92600577e-01, 9.81715790e-01, 2.49355664e-01, 3.49586402e-01, 3.06316251e-01], [9.53481907e-01, 4.67694588e-01, 2.87130633e-01, 4.67775924e-01, 5.58346921e-02, 6.10602106e-01, 9.63198001e-01, 8.11622840e-01, 7.84802447e-01, 4.36730675e-01, 6.33357315e-01, 3.97960708e-02], [3.21576871e-01, 4.80229812e-01, 2.32467994e-02, 9.85824004e-01, 9.49431266e-01, 3.42500033e-01, 9.50137427e-01, 2.08583444e-01, 7.95918837e-01, 6.90879817e-01, 1.99394711e-01, 3.99497023e-01], [8.46680982e-01, 6.17400861e-01, 1.94009819e-01, 9.70356857e-01, 6.42827033e-01, 2.07527912e-01, 9.96606362e-01, 5.00168970e-01, 9.44282686e-01, 4.32875891e-01, 3.24981958e-01, 7.94810313e-01], [1.68429458e-01, 6.36668059e-01, 9.59119653e-01, 2.87063147e-02, 1.82063308e-01, 7.02967761e-02, 6.01004149e-01, 6.02693093e-01, 6.10768913e-04, 6.10493956e-01, 5.28847344e-01, 4.68372652e-01], [8.64604664e-01, 4.51500769e-01, 3.20204688e-01, 1.97945445e-01, 9.23318656e-02, 4.34661316e-01, 1.48312024e-01, 1.25832658e-01, 5.97147486e-01, 9.03495213e-01, 2.65067117e-01, 8.19019632e-02], [1.67965053e-01, 6.73622188e-01, 2.40925083e-01, 2.41758949e-01, 3.07732600e-02, 6.77955313e-01, 1.76961761e-01, 3.38677666e-01, 4.59505637e-01, 1.77622397e-01, 7.72796138e-01, 5.63426444e-01], [6.17223267e-01, 2.32799465e-02, 7.06598959e-01, 6.87743941e-01, 4.31546623e-02, 4.43991413e-01, 1.62990101e-01, 8.31071662e-02, 2.03937859e-01, 4.77063262e-01, 9.20136477e-01, 5.34024388e-01], [4.17235917e-01, 9.43970181e-01, 6.17588978e-01, 3.55142862e-01, 1.58740304e-01, 1.95284602e-01, 3.26333383e-01, 5.95722378e-01, 2.81643611e-01, 1.84627341e-01, 8.77314869e-01, 6.57509041e-01], [5.48874259e-01, 3.11349560e-01, 4.47210389e-02, 2.40539336e-01, 3.27060399e-01, 2.68838703e-02, 6.78411900e-01, 7.54066431e-01, 4.97500736e-01, 1.50960929e-02, 3.26890534e-01, 7.94260960e-01]])
In [26]:
plt.imshow(m)
plt.colorbar()
plt.show()
In [27]:
# 使用颜色主题
plt.imshow(m,cmap=plt.cm.gnuplot)
plt.colorbar()
plt.show()
In [28]:
# 使用颜色主题
plt.imshow(m,cmap=plt.cm.ocean)
plt.colorbar()
plt.show()
子图的使用
In [30]:
import matplotlib.pyplot as plt
import numpy as np
In [31]:
fig, subplot_arr = plt.subplots(2, 2, figsize=(8, 8))
subplot_arr[0, 0].scatter(np.random.randn(50),np.random.randn(50) * 2)
subplot_arr[0, 1].bar([1, 2, 3, 4, 5],[5, 2, 7, 8, 2])
subplot_arr[1, 0].hist(np.random.randn(50),bins=10, rwidth=0.8)
subplot_arr[1, 1].imshow(np.random.rand(5, 5))
plt.show()
In [33]:
# 共享x轴
fig,subplot_arr = plt.subplots(2, 2, figsize =(8, 8),sharex=True)
subplot_arr[0, 0].scatter(np.random.randn(50),np.random.randn(50) * 2)
subplot_arr[0, 1].bar([1, 2, 3, 4, 5],[5, 2, 7, 8, 2])
subplot_arr[1, 0].hist(np.random.randn(50),bins=10, rwidth=0.8)
subplot_arr[1, 1].imshow(np.random.rand(5, 5))
plt.show()
In [34]:
# 共享y轴
fig,subplot_arr = plt.subplots(2, 2, figsize =(8, 8),sharey=True)
subplot_arr[0, 0].scatter(np.random.randn(50),np.random.randn(50) * 2)
subplot_arr[0, 1].bar([1, 2, 3, 4, 5],[5, 2, 7, 8, 2])
subplot_arr[1, 0].hist(np.random.randn(50),bins=10, rwidth=0.8)
subplot_arr[1, 1].imshow(np.random.rand(5, 5))
plt.show()
Matplotlib 颜色、标记、线型
In [35]:
import matplotlib.pyplot as plt
import numpy as np
In [38]:
fig, subplot_arr = plt.subplots(2, 2, figsize=(8, 8))
data = np.random.randn(20)
subplot_arr[0, 0].plot(data, 'r.--')
subplot_arr[0, 1].plot(data, 'gv:')
subplot_arr[1, 0].plot(data, 'b<-')
subplot_arr[1, 1].plot(data, 'ys-.')
plt.show()
Matplotlib 坐标刻度、标签、图例、标题
In [39]:
import matplotlib.pyplot as plt
import numpy as np
In [40]:
# 生成数据
data1 = np.random.randn(1000).cumsum()
data2 = np.random.randn(1000).cumsum()
data3 = np.random.randn(1000).cumsum()
基本显示
In [41]:
plt.figure()
plt.plot(data1)
plt.plot(data2)
plt.plot(data3)
plt.show()
使用plt 配置
In [42]:
plt.figure()
#设置刻度范围
plt.xlim([0,500])
# 设置显示的刻度
plt.xticks([0, 100, 200],['x1','x2','x3'])
#设置坐标轴标签
plt.xlabel('Number')
plt.ylabel('Random')
# 设置标题
plt.title('Example')
#图解
plt.plot(data1)
plt.plot(data2, label='line2')
plt.plot(data3, label='line3')
plt.legend(loc='best')
plt.show()
使用ax显示
In [47]:
fig, ax = plt.subplots(1)
ax.plot(data1, label='line1')
ax.plot(data2, label='line2')
ax.plot(data3, label='line3')
# 设置刻度
ax.set_xlim([0, 800])
# 设置显示的刻度
ax.set_xticks([0, 100, 200, 300, 400, 500])
# 设置刻度标签
ax.set_xticklabels(['x1', 'x2', 'x3', 'x4', 'x5'])
# 设置坐标轴标签
ax.set_xlabel('Number')
ax.set_ylabel('Random')
# 设置标题
ax.set_title('Example2')
#图例
ax.legend(loc='best')
plt.show()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-47-4de5fca93d38> in <module> 11 12 # 设置刻度标签 ---> 13 ax.set_xticklabels(['x1', 'x2', 'x3', 'x4', 'x5']) 14 15 # 设置坐标轴标签 C:\software\ANACONDA\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in wrapper(self, *args, **kwargs) 61 62 def wrapper(self, *args, **kwargs): ---> 63 return get_method(self)(*args, **kwargs) 64 65 wrapper.__module__ = owner.__module__ C:\software\ANACONDA\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs) 449 "parameter will become keyword-only %(removal)s.", 450 name=name, obj_type=f"parameter of {func.__name__}()") --> 451 return func(*args, **kwargs) 452 453 return wrapper C:\software\ANACONDA\Anaconda3\lib\site-packages\matplotlib\axis.py in _set_ticklabels(self, labels, fontdict, minor, **kwargs) 1791 if fontdict is not None: 1792 kwargs.update(fontdict) -> 1793 return self.set_ticklabels(labels, minor=minor, **kwargs) 1794 1795 @cbook._make_keyword_only("3.2", "minor") C:\software\ANACONDA\Anaconda3\lib\site-packages\matplotlib\axis.py in set_ticklabels(self, ticklabels, minor, **kwargs) 1712 # remove all tick labels, so only error for > 0 ticklabels 1713 if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0: -> 1714 raise ValueError( 1715 "The number of FixedLocator locations" 1716 f" ({len(locator.locs)}), usually from a call to" ValueError: The number of FixedLocator locations (6), usually from a call to set_ticks, does not match the number of ticklabels (5).