基于python的数据可视化学习笔记(入门级)

1、高质量作图工具——matplotlib
(1)基本例子

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 100)
y = np.cos(np.pi * x)
# g表示选择绿色,o表示选择点的方式显示
plt.plot(x, y, "go")
# 这里使用的是LaTex格式
plt.title(r"$y=\cos(\pi\times x)$")
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
# import matplotlib as mpl
# mpl.rcParams["font.sans-serif"]=["Microsoft YaHei"]
# mpl.rcParams["axes.unicode_minus"]=False
x = np.linspace(-2, 2, 100)
y1 = np.cos(np.pi * x)
y2 = np.sin(np.pi * x)

# g表示选择绿色,o表示选择点的方式显示
# linewidth代表线条或者垫的粗细程度
plt.plot(x, y1, "go", label=r"$y1=\cos(\pi\times x)$", alpha=0.8, linewidth=0.7)
plt.plot(x, y2, "r-", label=r"$y2=\cos(\pi\times x)$", alpha=0.8, linewidth=0.7)

plt.annotate("Important Point", (0, 1), xytext=(-1.5, 1.1), arrowprops=dict(arrowstyle="->"))
plt.xlabel("x-axis")
plt.ylabel("y-axis")
# 设置坐标范围
plt.axis([-2.1, 2.1, -1.2, 1.2])
# 设置标签
plt.legend()
# alpha参数代表透明度,由0-1表示颜色逐渐加深
plt.grid(alpha=0.4)
plt.title("两条曲线", color=(0.1, 0.3, 0.5))
plt.show()

在这里插入图片描述
(2)进阶使用——绘制子图

import numpy as np
import matplotlib.pyplot as plt

plt.style.use("ggplot")
x = np.linspace(-2, 2, 100)#绘制风格
y1 = np.sin(np.pi * x)
y2 = np.cos(np.pi * x)
y3 = np.tan(np.pi * x)
y4 = x
plt.subplot(221)
plt.plot(x, y1)
plt.subplot(222)
plt.plot(x, y2)
plt.subplot(223)
plt.plot(x, y3)
plt.subplot(224)
plt.plot(x, y4)
plt.show()

在这里插入图片描述
(3)绘制填充图

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1, 500)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)
fig, ax = plt.subplots()
ax.fill(x, y)
plt.show()

在这里插入图片描述
(4)各类图(pandas与matplotlib)

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


df = pd.DataFrame(np.random.randn(100, 4), columns=["A", "B", "C", "D"])


# df.plot.scatter(x="A",y="B",s=df["C"]*200)
# df.plot.barh(stacked=True)
# df.plot.bar()

# df = df.cumsum()
# df.plot(alpha=0.7, linewidth=1.5)

# df.iloc[:, 0:3].boxplot()
# df.hist(bins=20)

plt.title("Pandas-plot")
plt.show()

df = df.cumsum()
在这里插入图片描述
df.iloc[:, 0:3].boxplot()
在这里插入图片描述
df.hist(bins=20)
在这里插入图片描述
df.plot.bar()
在这里插入图片描述
df.plot.barh(stacked=True)
在这里插入图片描述
df.plot.scatter(x=“A”,y=“B”,s=df[“C”]*200)
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_37411471/article/details/89081584
今日推荐