简单matplotlib

版权声明:来一来,看一看,有钱的捧个人场,没钱的你不得捧个人场 https://blog.csdn.net/wait_for_eva/article/details/80386881

子图

位置

data = pd.read_csv('data.csv')
# @ax   :画布
# @2    :子图行数
# @3    :子图列数
# @4    :子图序号,按照从左到右,从上到下顺序编号
ax = plt.subplot(2, 3, 4)
plt.show()

设置

data = pd.read_csv('data.csv')
# figure设置画图宽高
# @6    :宽度
# @4    :高度
fig = plt.figure(figsize=(6, 4))
plt.plot()
plt.show()

折线图

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

# 生成X
x = np.arange(1, 9, 2)
# 生成Y
y = np.arange(2, 10, 2)
# 画图
# @x        :指定横坐标
# @y        :指定纵坐标
# @c        :指定颜色
# @label    :指定图例,需要设置显示
# @linewidth:指定线宽
plt.plot(x, y, c='red', label='label', linewidth=10)
# @loc      :指定图例位置
# 左上       : upper left
# 右上       : upper left
# 自动       : best
# 更多       :help(plt.legend)
plt.legend(loc='best')
# @(2,8)    :限制x范围
plt.xlim((2, 8))
# @(3,7)    :限制y范围
plt.ylim((3, 7))
# @x-label  :x数据标签
plt.xlabel('x-label')
# @y-label  :y数据标签
plt.ylabel('y-label')
# @title    :图像名称
plt.title('title')
# @*        :去除指示线段
plt.tick_params(bottom='off', top='off',left='off', right='off')
# 绘制
plt.show()


散点图

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

# 生成X
x = np.linspace(-np.pi, np.pi, 100)
# 生成Y
y = np.sin(x)
# 画图
# @x        :指定横坐标
# @y        :指定纵坐标
# @c        :指定颜色
# @label    :指定图例,需要设置显示
# @linewidth:指定线宽
plt.scatter(x, y, c='red', label='label', linewidth=1)
# @loc      :指定图例位置
# 左上       : upper left
# 右上       : upper left
# 自动       : best
# 更多       :help(plt.legend)
plt.legend(loc='best')
# @x-label  :x数据标签
plt.xlabel('x-label')
# @y-label  :y数据标签
plt.ylabel('y-label')
# @title    :图像名称
plt.title('title')
# @*        :去除指示线段
plt.tick_params(bottom='off', top='off', left='off', right='off')
# 绘制
plt.show(

柱形图

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

# 生成X
x = ['godme', 'foreva', 'judas']
# 生成Y
y = [100, 56, 78]
# 指定x位置
bar_position = np.arange(len(x)) + 0.75
# 指定高度
bar_high = y
# 指定柱子宽度
bar_width = 0.3
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# 画图
# @bar_position :指定横坐标
# @bar_high     :指定纵坐标
# @bar_width    :指定宽度
# @label        :指定图例,需要设置显示
ax.bar(bar_position, bar_high, bar_width, label='label')
# 设置标签位置
ax.set_xticks(bar_position)
# 为柱子指定标签
ax.set_xticklabels(x)
# @loc      :指定图例位置
# 左上       : upper left
# 右上       : upper left
# 自动       : best
# 更多       :help(plt.legend)
ax.legend(loc='best')
# @x-label  :x数据标签
plt.xlabel('x-label')
# @y-label  :y数据标签
plt.ylabel('y-label')
# @title    :图像名称
plt.title('title')
# @*        :去除指示线段
plt.tick_params(bottom='off', top='off', left='off', right='off')
# 绘制
plt.show()

横向

ax.barh(bar_position, bar_high, bar_width, label='label')

频率图

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

# 生成X
# @1    :最小1
# @100  :最大100
# @100  :100个
x = np.random.randint(1, 100, 100)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# 画图
# @x        :数据
# @bins     :统计个数
# @range    :指定区间
ax.hist(x, bins=20, range=(25, 65))
# @loc      :指定图例位置
# 左上       : upper left
# 右上       : upper left
# 自动       : best
# 更多       :help(plt.legend)
ax.legend(loc='best')
# @x-label  :x数据标签
plt.xlabel('x-label')
# @y-label  :y数据标签
plt.ylabel('y-label')
# @title    :图像名称
plt.title('title')
# @*        :去除指示线段
plt.tick_params(bottom='off', top='off', left='off', right='off')
# 绘制
plt.show()

盒图

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

# 生成X
x = ['godme', 'foreva', 'judas']
# 生成Y
y = np.random.rand(50, 3)
print(y.shape)
# 指定x位置
label_position = np.arange(len(x)) + 1
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# 画图
# @y     : 数据
# 盒图四要素
# 峰值     :最顶上横杠
# 四分之一  :盒子最下
# 二分之一  :中间横线
# 四分之三  :盒子上顶
ax.boxplot(y)
# 设置标签位置
ax.set_xticks(label_position)
# 为柱子指定标签
ax.set_xticklabels(x)
# @x-label  :x数据标签
plt.xlabel('x-label')
# @y-label  :y数据标签
plt.ylabel('y-label')
# @title    :图像名称
plt.title('title')
# @*        :去除指示线段
plt.tick_params(bottom='off', top='off', left='off', right='off')
# 绘制
plt.show()

常用

数据生成

import numpy as np

# 一维数据
# @1    :from,起始值
# @2    :to,最大值
# @3    :num,值个数,自动平均取值
X = np.linspace(-np.pi, np.pi, 100)

# 随机矩阵
# @8    :矩阵行数
# @9    :矩阵列数
Y = np.random.rand((8, 9))
图像叠加
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 图像叠加重复绘制即可
# 注意通过颜色区分,还有使用图例
fig = plt.figure()
fig.set_figwidth(50)
fig.set_figheight(50)
X = np.linspace(-np.pi, np.pi, 100)
Y1 = np.sin(X)
Y2 = np.cos(X)
plt.plot(X, Y1, 'r-*', label='sin')
plt.plot(X, Y2, 'g-o', label='cos')
plt.xlabel('x')
plt.ylabel('y')
plt.title('sin(x) and cos(x)')
# 多图例最好带参数名指定图例位置
plt.legend(loc='best')
plt.show()





猜你喜欢

转载自blog.csdn.net/wait_for_eva/article/details/80386881