In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
x = np.arange(2, 20)
y = 2 * x + np.random.randint(5, 20, 18)
In [3]:
plt.plot(x, y, '*-', color='r')
plt.show()
In [4]:
x = np.linspace(0, 1, 100)
y1 = np.power(x, 0.5)
y2 = x
y3 = np.power(x, 1.5)
plt.plot(x, y1, label='0.5')
plt.plot(x, y2, label='1')
plt.plot(x, y3, label='1.5')
plt.legend() # 显示图例
plt.xlabel('r') # x坐标轴名称
plt.ylabel('s') # y坐标轴名称
plt.grid() # 网格
plt.xlim([0, 1]) # 坐标轴范围
plt.ylim([0, 1])
plt.show()
In [5]:
a = np.random.randint(0, 101, 1000)
In [6]:
# 直方图统计
# rwidth 显示宽度
plt.hist(a, rwidth=0.9, color='g')
plt.show()
In [7]:
bins = np.arange(-0.5, 101, 1)
bins
Out[7]:
array([ -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5, 30.5, 31.5, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5, 50.5, 51.5, 52.5, 53.5, 54.5, 55.5, 56.5, 57.5, 58.5, 59.5, 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5, 79.5, 80.5, 81.5, 82.5, 83.5, 84.5, 85.5, 86.5, 87.5, 88.5, 89.5, 90.5, 91.5, 92.5, 93.5, 94.5, 95.5, 96.5, 97.5, 98.5, 99.5, 100.5])
x:表示要绘制直方图的数据,可以是一个一维数组或列表。
bins:可选参数,表示直方图的箱数。默认为10。
range:可选参数,表示直方图的值域范围,可以是一个二元组或列表。默认为None,即使用数据中的最小值和最大值。
density:可选参数,表示是否将直方图归一化。默认为False,即直方图的高度为每个箱子内的样本数,而不是频率或概率密度。
weights:可选参数,表示每个数据点的权重。默认为None。
cumulative:可选参数,表示是否绘制累积分布图。默认为False。
bottom:可选参数,表示直方图的起始高度。默认为None。
histtype:可选参数,表示直方图的类型,可以是'bar'、'barstacked'、'step'、'stepfilled'等。默认为'bar'。
align:可选参数,表示直方图箱子的对齐方式,可以是'left'、'mid'、'right'。默认为'mid'。
orientation:可选参数,表示直方图的方向,可以是'vertical'、'horizontal'。默认为'vertical'。
rwidth:可选参数,表示每个箱子的宽度。默认为None。
log:可选参数,表示是否在y轴上使用对数刻度。默认为False。
color:可选参数,表示直方图的颜色。
label:可选参数,表示直方图的标签。
stacked:可选参数,表示是否堆叠不同的直方图。默认为False。
In [8]:
plt.hist(a, bins=bins ,rwidth=0.9, color='g')
plt.show()
In [ ]: