《封号码罗》数据分析与人工智能之matplotlib(六)

第一部分

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
from matplotlib import pyplot
import time

# 设置plot的风格和样式
# plot语句中支持除x,y以外的参数,以字符串形式存在,来控制颜色、线性、点型等要素
# 语法形式为:plt.plot(x,y,"format",...)
# 点和线的样式
# 颜色 参数color或c

x = np.linspace(0, 2 * np.pi, 20)  # 参数100决定了画的线条是否圆滑
y = np.sin(x)
y2 = np.cos(x)
#          purple紫色
# plt.plot(x, y, color="red")
# plt.plot(x, y, color="#ff0000")
# print(np.random.rand(3))    # [0.32037152 0.28062064 0.66644008]

# 设置坐标轴背景色
# ax = plt.subplot(1, 1, 1, facecolor="green")
# linestyle这个参数是设置线的形状
# 另一种写法 ls = "steps"  阶梯状的线
# ax.plot(x, y, linestyle="--", color="red")
# ax.plot(x, y, ls="dotted", color="red")

# 设置线宽linewidth == lw
# ax.plot(x, y, ls="dotted", color="red", lw=5)

# 不同宽度的破折线  dashes参数 用来设置破折号序列各段的宽度  --. --.
# dashes=[线宽,间距,线宽,间距,...]
# plt.plot(x, y, dashes=[1, 2, 5, 3, 2, 5])  # 破折号序列必须具有偶数个元素

# 线上面点的形状,主要通过marker参数设置
# marker点的个数,与x的形状有关系,x.shape查看即可
# plt.plot(x, y, marker="1", markersize=20)
# plt.plot(x, y, marker="s", markersize=10)
# plt.plot(x, y, marker="|", markersize=10)

# 单条曲线中多个设置
# plt.plot(x, y, c="r", marker="*", markersize=10, markerfacecolor="green", markeredgecolor="blue")

# 在一条语句中农为多个曲线进行设置
# 多个曲线同一设置 属性名声明
# plt.plot(x, y, x, y2, color="r")

# 多个曲线不同设置,无需声明属性,颜色需要使用简写形式 red == r green == g
# plt.plot(x, y, "r-o", x, y2, "g:*")

# 属性的三种设置方式
# 方法一、关键字参数
# plt.plot(x, y2, color="r")

# 方法二、对实例使用一系列的setterr方法 ,plt.plot(x, y)生成一个列表,给参数接收就是列表对象
# line, = plt.plot(x, y)
# line.set_color("#ff00ff")
# line.set_ls("--")

# 方法三、使用setp()方法
# line1, line2 = plt.plot(x, y, x, y2)
# plt是模快方法
from matplotlib.pyplot import setp

# plt.setp(line1, c='r', ls="steps")
# setp(line1, c='r', ls="steps")

# 对坐标轴的刻度自定义    labels的值对应ticks的位置上的值
# plt.plot(x, y)
# plt.xticks(ticks=np.arange(9), labels=list("abcdefghi"))                    # 字体        旋转
# plt.yticks([-1, -0.5, 0, 0.5, 1], ["min", "min_0.5", 0, "max_0.5", "max"], fontsize=10, rotation=30)
# plt.grid(True)
plt.figure(figsize=(9, 6))
plt.plot(x, y)  # ---------------------------希腊字符$\英文$-------0 二分之一π     π      二分之三π   2π
plt.xticks(ticks=np.arange(0, 2 * np.pi + 0.01, np.pi / 2), labels=[0, "$\pi/2$", "$\pi$", "$3\pi/2$", "$2\pi$"])
plt.yticks([-1, -0.5, 0, 0.5, 1], ["min", "min_0.5", 0, "max_0.5", "max"], fontsize=10, rotation=30)
plt.grid(True)

plt.show()

start = time.perf_counter()
print()
end = time.perf_counter()
print(f"程序运行耗时为{(start - end).__round__(20)}")

第二部分

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
from matplotlib import pyplot
import time

#          柱子宽度站刻度之间的50%
# plt.hist(rwith=0.5) 直方图,统计数据在定义区间的出现频次,图形颜色单一
x = np.array([0, 45, 90, 135, 180, 225, 270, 315, 360])
y = np.array([5, 12, 24, 9, 11, 4, 0, 1])
#                   柱子宽度   柱子对齐方式 "edge"边界对齐
# plt.bar(x[:-1], y, width=22.5, align="center", color=np.random.rand(8, 3))

# 极坐标  玫瑰图
plt.axes(polar=True)  # 设置轴的 参数(极)polar为true
# 因为一圈是0到2*π 是0到2*3.14 ,现在x里面是0到360,所以需要重新设置x
x1 = np.arange(0, 2 * np.pi, np.pi / 4)  # 45度是四分之一π
plt.bar(x1, y, width=np.pi / 4, align="center", color=np.random.rand(8, 3))

plt.show()

start = time.perf_counter()
print(np.random.rand(8, 3))
end = time.perf_counter()
print(f"程序运行耗时为{(start - end).__round__(20)}")

第三分部

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
from matplotlib import pyplot
import time
from matplotlib import rcParams

rcParams["font.size"] = 20
# 饼图和散点图
x = np.array([0, 45, 90, 135, 180, 225, 270, 315, 360])
y = np.array([5, 12, 24, 9, 11, 4, 0, 1])  # --------------------------------autopct 设置百分比格式,保留两位小数,加阴影
# plt.pie([0.4, 0.25, 0.15, 0.1, 0.05, 0.03, 0.02], explode=np.linspace(0, 0.2, 7),
#         labels=["cat", "dog", "pig", "a", "b", "c", "d"], autopct="%0.2f%%", shadow=True)  # 这里不能调整字体大小,要设置全局
# plt.axis("equal")  # equal 相等的意思  横坐标和纵坐标轴相等,自然成圆
# plt.scatter(x[:-1], y)
# plt.scatter(np.random.randn(1000), np.random.randn(1000), color=np.random.rand(1000, 3), marker="d")

# 使用figtext()控制图形内的文字
x1 = np.linspace(0, 2 * np.pi, 50)  # 参数100决定了画的线条是否圆滑
y1 = np.sin(x1)
y2 = np.cos(x1)
# plt.plot(x1, y1)
# plt.text(0, 0, s="sin(0)=0")  # 刻度坐标
# plt.figtext(0, 0, s="sin(0)=0")  # 百分比坐标

# plt.figure(figsize=(9, 12))   # 设置画布大小之后,suptitle标题消失了,如果不设置画布大小还需单独设置标题
# plt.figure(figsize=(12, 9)).suptitle("正弦波和余弦波")  # 这个设置之后不用plt.suptitle("正弦波和余弦波", x=0.5, y=1.05)
# ax = plt.subplot(1, 2, 1)
# ax.plot(x1, y1)
# ax.set_title("sin")
# ax = plt.subplot(1, 2, 2)
# ax.plot(x1, y2)
# ax.set_title("cos")
# rcParams["font.sans-serif"] = "KaiTi"
# rcParams["axes.unicode_minus"] = False  # 让负数 - 这个符号可以正常显示
#                                   y是文本距离图表的高度
# plt.suptitle("正弦波和余弦波", x=0.5, y=1.05)

# 注释    annotate()
# x,y参数设置箭头指示的位置,xytext参数设置注释文字的位置
# arrowprops参数以字典的形式设置箭头的样式
# 下面属性是自定义箭头:
# width参数设置箭头长方形部分的宽度,headlength参数设置箭头尖端的长度
# headwidth参数设置箭头尖端底部的宽度
# facecolor设置箭头颜色
# shrink参数设置箭头顶点、尾部与指示点、注释文字的距离(比例值)
# x2 = np.arange(15)
# y3 = np.random.randint(10, 15, size=15)
# y3[6] = 30
# plt.plot(x2, y3)
# plt.axis([-2, 15, 9, 35])
# plt.annotate(s="this point \n very good", xy=(6, 30), xytext=(8, 32), arrowprops={"arrowstyle": "->"})
# plt.annotate(s="this point \n very good", xy=(6, 30), xytext=(8, 32), arrowprops={"arrowstyle": "fancy"})
# plt.annotate(s="this point \n very good", xy=(6, 30), xytext=(8, 32),
#              arrowprops={"width": 10, "headlength": 15, "headwidth": 15, "shrink": 0.15})

# 3D 图形
from mpl_toolkits.mplot3d.axes3d import Axes3D

X = np.linspace(-2, 2, 100)
Y = np.linspace(-2, 2, 100)
Z = (X ** 2 + Y ** 2) ** 0.5
fig = plt.figure(figsize=(12, 9))
axes3D = Axes3D(fig)
# axes3D.plot(X, Y, Z)

XX, YY = np.meshgrid(X, Y)  # 把数据使用网格交叉之后返回两个数据 是交叉后的X,Y
ZZ = (XX ** 2 + YY ** 2) ** 0.5
axes3D.plot_surface(XX, YY, ZZ)
plt.show()

start = time.perf_counter()
print()
end = time.perf_counter()
print(f"程序运行耗时为{(start - end).__round__(20)}")

第四部分

# pd.pivot_table(df,index=[字段1],values=[字段2],aggfunc=[函数],fill_value=0)
# df.groupby([字段1])[字段2].agg(函数).fillna(0)
# 两个行数完全等价,pivot_table仿佛是加入了columns与margin功能的groupby函数,比groupby更加灵活。
# table.query('对手 == ["灰熊"]')

发布了30 篇原创文章 · 获赞 5 · 访问量 3319

猜你喜欢

转载自blog.csdn.net/Python_DJ/article/details/104362557
今日推荐