Loading

pandas learning

pandas learning

matplotlib

设置中文显示

matlotlib默认不支持中文字符,默认英文字体无法显示汉字

查看inux/mac下面支持的字体:fc-list :lang=zh

# windows、linux、macos系统通用设置
# 导入font_manager => 实例化FontProperties对象my_font => 在设置字体的地方使用这个对象,比如xticks
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="/System/Lirary/Fonts/PingFang.ttc")
lt.xticks(range(2, 25, 1), fontproperties=my_font)

折线图

from matplotlib import pyplot as plt
from matplotlib import font_manager
# 实例化FontProperties
my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
kwargs = {
    "fontproperties": my_font
}
# x,y值,一一对应
x = range(2, 30, 3)
y1 = [15, 13, 14.5, 17, 20, 25, 26, 26, 24, 22]
y2 = [14, 22, 19.6, 12, 23, 24, 28, 26, 22, 18]
# 设置图像尺寸和像素大小
plt.figure(figsize=(20, 8), dpi=90)
# 设置线条样式:颜色-color,风格-linestyle,粗细-linewidth,透明度-alpha
# linestyle样式有:- -- : -.
plt.plot(x, y1, label="小明", color="orange", linestyle=":", linewidth=5, alpha=0.5)
plt.plot(x, y2, label="小狗", color="cyan", linestyle="-.", linewidth=1, alpha=0.6)
# 设置轴刻度,刻度轴可以使等间距固定,用于标识x和y的数据范围
# 指定刻度轴x-label
x_ticks = range(2, 30, 2)
labels = [f"测试{i}" for i in x_ticks] 
# 参数:rotation-逆时针旋转角度
# 参数:fontproperties-字体属性设置
plt.xticks(x_ticks, labels, rotation=90, **kwargs)
plt.yticks(range(0, 30, 5), **kwargs)
# 设置描述信息:X轴描述、Y轴描述、标题描述
plt.xlabel("ID")    
plt.ylabel("AGE")
plt.title("这是一个测试标题", **kwargs)
# 绘制网格
# alpha-透明度,0~1:完全透明~不透明
plt.grid(alpha=0.4)
# 添加图例,这里需要注意,图例的信息转向的使plt.plot里面的label参数信息,且设置中文字体参数是prop
plt.legend(prop=my_font)
# 输出文件
plt.savefig("./output/demo.svg")
# 绘图显示
plt.show()

散点图

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")
x_3 = range(1, 30)
x_10 = range(34, 63)
y_3 = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22]
y_10 = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 11, 15, 5, 13, 17, 10, 11]
plt.figure(figsize=(20, 8), dpi=100)
# 使用scatter方法绘制散点图
plt.scatter(x_3, y_3, label="3月份")
plt.scatter(x_10, y_10, label="10月份")

_x = list(x_3) + list(x_10)
x_tick_labels = [f"3月{i}日" for i in x_3]
x_tick_labels += [f"10月{i - 40}日" for i in x_10]
plt.xticks(_x[::3], x_tick_labels[::3], fontproperties=my_font, rotation=60)
plt.legend(loc="upper left", prop=my_font)
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度", fontproperties=my_font)
plt.title("标题", fontproperties=my_font)
plt.show()

条形图

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")

a = ["A", "B", "C", "D", "E"]
b = [56, 16, 87, 11, 66]
plt.figure(figsize=(16, 9), dpi=80)
# 条形图竖向
_x = list(range(len(a)))
plt.bar(_x, b, width=0.4)
plt.xticks(_x, [f"电影_{i}" for i in a], fontproperties=my_font, rotation=30)

# 条形图横向,横向条形图
_y = list(range(len(a)))
plt.barh(_y, b, height=0.4)
plt.yticks(_y, [f"电影_{i}" for i in a], fontproperties=my_font)

# 一张图画多个条形图
b1 = [56, 16, 87, 11, 66]
b2 = [33, 56, 66, 14, 46]
b3 = [22, 22, 77, 18, 56]
width = 0.2
# 连续加一个宽度,可以让同一个x的3个条形图挨在一起
x_1 = list(range(len(a)))
x_2 = [i + width for i in x_1]
x_3 = [i + width * 2 for i in x_1]
plt.bar(x_1, b1, width=width)
plt.bar(x_2, b2, width=width)
plt.bar(x_3, b3, width=width)
# x_2的位置正好在一个x的3个条形图正中间
plt.xticks(x_2, [f"电影_{i}" for i in a], fontproperties=my_font, rotation=5)
plt.show()

直方图

from matplotlib import pyplot as plt

a = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124,
     101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117, 86,
     95, 144, 105, 126, 130, 126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136, 123, 117, 119, 105, 137,
     123, 128, 125, 104, 109, 134, 125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115,
     132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,
     123, 107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127,
     115, 118, 112, 135, 115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154, 136, 100, 118, 119, 133, 134,
     106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126, 114, 140, 103,
     130, 141, 117, 106, 114, 121, 114, 133, 137, 92, 121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113, 134,
     106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146,
     133, 101, 131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150]
# 计算组数
d = 3  # 组数
num_bins = (max(a) - min(a)) // d
plt.hist(a, num_bins)
plt.xticks(range(min(a), max(a), d))
plt.show()

其它绘图库

plotly
seaborn

numpy

一个在Python中做科学计算的基础库,重在数值计算,也是大部分Python科学计算库的基础库,多用于在大型多维数组上执行数值运算

创建numpy数组

import numpy as np

# 几种基本的创建数组方式,使用dtype指定数值类型
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array(range(1, 6), dtype=np.float32)
arr3 = np.arange(1, 6, 0.5)
arr4 = np.array(["1", "2", "3"], dtype=np.string_)
# 数组数值类型获取
print(arr1.dtype)
# 数组数值类型转换,原数组类型不变,返回指定数值类型数组
arr1_1 = arr1.astype(np.int32)
arr1_2 = arr1.astype(arr3.dtype)
arr1_3 =  arr4.astype(np.int32)
print(arr1_1.dtype)
# 数组形状及改变形状
print(arr1.shape)
arr2_1 = arr2.reshape((2, 3))
# 展开成一维数组
arr2_2 = arr2.flatten()

广播原则:如果两个数组的后缘维度(trailing dimension,即从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失和(或)长度为1的维度上进行。

# 数组计算
# 数组与数值计算,广播形式每一个元素都进行相同的运算
arr3_1 = arr3 + 1
arr3_2 = arr3 / 0  # 不报错,出现警告,除以0结果为inf
# 数组与数组计算,如果维度不一样,会以广播形式,小维度的扩展维度成大维度,某一维度一样时候才行

类型 类型代码 说明
int8、uint8 i1、u1 有符号和无符号的8位(1个字节)整型
int16、uint16 i2、u2 有符号和无符号的16位(2个字节)整型
int32、uint32 i4、u4 有符号和无符号的32位(4个字节)整型
int64、unint64 i8、u8 有符号和无符号的64位(8个字节)整型
float16 f2 半精度浮点数
float32 f4或f 标准的单精度浮点数。与C的float兼容
float64 f8或d 标准的双精度浮点数。与C的double和Python的float对象兼容
float128 f16或g 扩展精度浮点数
complex64、complex128、complex256 c8、c16、c32 分别用两个32位、64位或128位浮点数表示的复数
bool ? 存储True和False值的布尔类型
object O Python对象类型
string_ S 固定长度的字符串长度(每个字符1个字节)。例如,要创建一个长度为10的字符串,应使用S10
unicode_ U 固定长度的unicode长度(字节数由平台决定)。跟字符串的定义方式一样(如U10)
posted @ 2023-03-12 22:43  MrSu  阅读(27)  评论(0编辑  收藏  举报