#时间序列可视化
#离散数据的时间序列可视化
import numpy as np
import pandas as pdts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()
#%%
#连续数据的时间序列可视化
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
plt.figure()
df.plot()
plt.legend(loc='best')
#%%
#连续型图表
#阶梯图
import numpy as np
import matplotlib.pyplot as pltx = np.arange(14)
y = np.sin(x / 2)plt.figure(figsize=(12,5))
plt.subplot(121)
plt.step(x, y + 2, label='pre (default)')
plt.plot(x, y + 2, 'o--', color='grey', alpha=0.3)plt.step(x, y + 1, where='mid', label='mid')
plt.plot(x, y + 1, 'o--', color='grey', alpha=0.3)plt.step(x, y, where='post', label='post')
plt.plot(x, y, 'o--', color='grey', alpha=0.3)plt.grid(axis='x', color='0.95')
plt.legend(title='Parameter where:')
plt.title('plt.step(where=...)')plt.subplot(122)
plt.plot(x, y + 2, drawstyle='steps', label='steps (=steps-pre)')
plt.plot(x, y + 2, 'o--', color='grey', alpha=0.3)plt.plot(x, y + 1, drawstyle='steps-mid', label='steps-mid')
plt.plot(x, y + 1, 'o--', color='grey', alpha=0.3)plt.plot(x, y, drawstyle='steps-post', label='steps-post')
plt.plot(x, y, 'o--', color='grey', alpha=0.3)plt.grid(axis='x', color='0.95')
plt.legend(title='Parameter drawstyle:')
plt.title('plt.plot(drawstyle=...)')
plt.show()
#%%
#折线图
df2.plot(kind='line')
#%%
#拟合曲线
'''
Author: CloudSir
Date: 2021-08-03 15:01:17
LastEditTime: 2021-08-03 15:26:05
LastEditors: CloudSir
Description: Python拟合任意函数
https://github.com/cloudsir
'''
# 引用库函数import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize as opplt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号# 需要拟合的函数
def f_1(x, A, B, C):return A * x**2 + B * x + C# 需要拟合的数据组
x_group = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_group = [2.83, 9.53, 14.52, 21.57, 38.26, 53.92, 73.15, 101.56, 129.54, 169.75, 207.59]# 得到返回的A,B值
A, B, C = op.curve_fit(f_1, x_group, y_group)[0]# 数据点与原先的进行画图比较
plt.scatter(x_group, y_group, marker='o',label='真实值')
x = np.arange(0, 15, 0.01)
y = A * x**2 + B *x + C
plt.plot(x, y,color='red',label='拟合曲线')
plt.legend() # 显示labelplt.show()#%%
#螺旋图
import turtle
n = 500
# turtle.left(60)s
turtle.penup()
turtle.goto(-450,150)
turtle.pendown()
turtle.pencolor("blue")
for i in range(1,1000,1):if i < 500:n = n - 1turtle.speed(100)turtle.fd(n)turtle.right(140)else:n+=1turtle.speed(100)turtle.pencolor('red')turtle.fd(n)turtle.right(114)
turtle.done()
#%%
from turtle import *speed(0) # 最快的画笔速度# 画圆脸
setup(600, 600, 0, 0)
penup()
fd(-200)
pendown()
color('yellow', 'yellow')
begin_fill()
seth(-90)
circle(200)
end_fill()# 画嘴巴
penup()
seth(0)
fd(10)
pendown()
pensize(3) # 调整画笔大小
color('red')
seth(-90)
circle(190, 180)# 画眼睛
penup()
fd(100)
seth(180)
fd(573)
for i in range(2): # 给画两只眼睛制造相同代码,才可以使用for循环,绘制两只眼睛penup()seth(0)fd(200)pendown()pensize(2)seth(20)color('black', 'white')begin_fill()circle(-230, 40)circle(-10, 180)circle(210, 40)circle(-10, 180)end_fill()color('black', 'black')begin_fill()circle(-10)end_fill()#%%
#热图
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pddf = pd.DataFrame(np.random.randn(10, 5))plt.figure(figsize=(10, 8))# 设置字体
sns.set(font_scale=1.2)
plt.rc('font',family=['Times New Roman', 'SimSun'], size=12)
plt.subplots_adjust()
ax = sns.heatmap(df.corr(), annot=True, fmt=".2f")
ax.set_title('相关性热力图') # 图标题
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
figure = ax.get_figure()
# figure.savefig('sns_heatmap.jpg', bbox_inches='tight')
#%%
#离散型
#柱形图
df3.plot(kind='bar')
#%%
#散点图
df3.plot(kind='scatter', x='a', y='b')
#%%
#堆叠柱形图
df3.plot(kind='bar', stacked=True)
#%%
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D# 生成数据
theta = np.linspace(0, 2 * np.pi, 100)
z = np.linspace(-1, 1, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)# 创建图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')# 绘制花瓶图
ax.plot(x, y, z, label='花瓶图')
ax.legend()plt.show()