2. 日月光华 Python数据分析-Matplotlib
2023-06-28 本文已影响0人
薛东弗斯
1 绘制线型图
import matplotlib.pyplot as plt
%matplotlib inline
x = [1, 2, 3, 4]
y = [2, 5, 6, 8]
plt.plot(x, y)
image.png
plt.plot(y) # 为指定x轴坐标,就会以默认的0/1/2/3/4...为x轴的坐标,第一个点是0
image.png
x = [1, 2, 3, 4]
y = [2, 5, 6, 8]
y1 = [3, 4, 7, 8]
plt.plot(x, y, label='A')
plt.plot(x, y1, label='B')
plt.legend() # 打开图例
plt.show()
image.png
import numpy as np
data1 = np.random.randn(10).cumsum() # cumsum 累加运算
data2 = np.random.randn(10).cumsum()
plt.plot(data1, label='data1')
plt.plot(data2, label='data2')
image.png
(12条消息) matplotlib 线型linestyle的高级设置_aquapisces的博客-CSDN博客
Python可视化|matplotlib03-一文掌握marker和linestyle使用 - 知乎 (zhihu.com)
x = np.linspace(0, 7, 100)
y = np.sin(x)
plt.plot(x, y)
image.png
2 设置线型,属性,标签
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
y = np.random.randn(10).cumsum()
y1 = np.random.randn(10).cumsum()
plt.style.use('ggplot')
plt.plot(y, c='r', ls='-.', lw=1, marker='2', label='stu') # c=color; lw=linewidth
plt.xlabel('age')
plt.ylabel('height')
plt.title('STUDENT', y=1.03) # title的高度是1.01倍y轴的高度
plt.xlim(0, 10) # 设置x轴的取值范围。 默认范围是让图形全部显示对应的取值范围
plt.ylim(0, 5)
plt.xticks([0, 4, 8], ['born', 'baby', 'child']) # 设置刻度,第一个参数是告诉在哪里设置刻度,此处是[0, 4, 8]; 第2个参数表示标注的刻度内容['born', 'baby', 'child']
plt.yticks([1, 3, 5], ['height1', 'height2', 'height3'])
plt.legend(loc=2) # 显示图例。 位置是2
plt.grid()
image.png
matplotlib.pyplot.legend — Matplotlib 3.7.1 documentation
x = np.linspace(-np.pi, np.pi, 100)
y = np.sin(x)
plt.plot(x, y, 'k-.')
plt.xticks([-np.pi, 0, np.pi], [r'$-\pi$', 0, r'$+\pi$'])
plt.style.use('ggplot')
image.png
条形图
x = [1, 3, 5, 7]
y = [2, 6, 3, 9]
plt.bar(x, y, color='b')
image.png
plt.barh(x, y)
image.png
x1 = [2, 4, 6, 8]
y1 = [2, 4, 1, 6]
x = [1, 3, 5, 7]
y = [2, 6, 3, 9]
plt.bar(x, y)
plt.bar(x1, y1)
image.png
堆积柱状图
x1 = [2, 4, 6, 8]
y1 = [2, 4, 1, 6]
x = [1, 3, 5, 7]
y = [2, 6, 3, 9]
plt.bar(x, y)
# plt.bar(x1, y1)
plt.bar(x, y1,bottom=y)
image.png
直方图
data = np.random.randn(1000)
# np.max(data), np.min(data)
plt.figure(figsize = (5, 5)) # 初始化画布大小
plt.hist(data, bins=40) # 分成40份
image.png
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(1000)
plt.hist(data)
image.png
数据特征不是很明显,如果改成箱线图,则数据分布特征就会比较明显
箱线图
plt.boxplot(data)
plt.grid()
image.png
data = np.random.randn(1000)
plt.boxplot(data)
离群点用圆圈表示
image.png
散点图
x1 = [1,2,3,4,5]
y1 = [3,2,1,4,7]
x2 = [2,4,6,8,9]
y2 = [6, 1, 4, 2, 8]
plt.scatter(x1, y1, color='r', marker='>', label='one', s=100)
plt.scatter(x2, y2, color='b', marker='o', label='two', s=200)
plt.legend()
image.png
堆积图
import matplotlib.pyplot as plt
import numpy as np
comp = [1, 2, 3, 4]
y1 = [24, 27, 35, 43]
y2 = [30, 35, 17, 12]
y3 = [27, 13, 15, 8]
y4 = [19, 25, 33, 37]
labels = ['apple', 'micro', 'yahoo', 'google']
plt.stackplot(comp, y1, y2, y3, y4, labels=labels)
plt.legend()
plt.xticks([1,2,3,4],['2011', '2012', '2013', '2014'])
plt.title('market')
image.png
饼图
labels = ['apple', 'micro', 'yahoo', 'google']
size = [15, 30, 45, 10]
explode = (0.2, 0, 0, 0) # 第一个图形跃出中小0.2
plt.figure(figsize=(8, 8)) # 图形尺寸
plt.pie(size, labels=labels, autopct='%1.1f%%', explode=explode, startangle=90, shadow=True)
image.png
绘制多图
import matplotlib.pyplot as plt
import numpy as np
x = [1,2,3,4]
y1 = [4, 5, 8, 9]
y2 = [2, 5, 3, 8]
plt.subplot(2, 1, 1)
plt.plot(x, y1)
plt.subplot(2, 1, 2)
plt.plot(x, y2)
plt.suptitle('y1&y2')
image.png
plt.subplot(2, 3, 1)
plt.plot(x, y1)
plt.subplot(2, 3, 2)
plt.bar(x, y1)
plt.subplot(2, 3, 3)
plt.barh(x, y1)
plt.subplot(2, 3, 4)
plt.scatter(x, y1)
plt.subplot(2, 3, 5)
plt.boxplot(y1)
image.png
plt.subplot(2, 3, 1) # 2行3列第1个子图
plt.plot(x, y1)
plt.subplot(2, 3, 2) # 2行3列第2个子图
plt.bar(x, y1)
plt.subplot(2, 3, 3) # 2行3列第3个子图
plt.barh(x, y1)
plt.subplot(2, 1, 2) # 2行1列第2个子图
plt.plot(x, y2, 'b--')
image.png
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
data = np.random.randn(1000)
fig, ax = plt.subplots()
ax.hist(data, label = 'ax')
ax.set_title('title is here')
ax.set_xlabel('this is x')
ax.legend(loc=2)
ax.set_xticks([-3, 0, 3])
ax.set_xticklabels(['low', 'median', 'high'])
ax.grid(True)
image.png
fig, ax = plt.subplots(2, 2, figsize=(10,8))
ax[0][0].hist(data)
ax[0][1].boxplot(data)
plt.tight_layout()
image.png
np.shape(ax)
(2, 2)
ax
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000001DE26D9EB70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x000001DE26DD40B8>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x000001DE26DF95F8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x000001DE26E23B70>]],
dtype=object)
import numpy as np
np.random.randint(55,101,(3,40))
显示中文
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(1000)
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文
plt.rcParams['axes.unicode_minus']=False
plt.figure(figsize=(10, 8), dpi=300)
plt.hist(data)
plt.title('某某公司')
plt.xticks([-3,-2,-1,0,1,2,3],
['2012-10-01','2012-10-02','2012-10-03','2012-10-04',
'2012-10-05','2012-10-06','2012-10-07'],
rotation=30) # 旋转30°
plt.savefig('D:/w3c/lesson22.jpg')
plt.show()
image.png
image.png
image.png