matplotlib笔记1

##matplotlib笔记1

import numpy as np
from matplotlib import pyplot as plt

#numpy绘图

pyplot()
#绘制 2D 数据

#绘制方程y = 2x + 5
x = np.arange(1,11)
y = 2 * x + 5
plt.title(“Matplotlib demo”)
plt.xlabel(“x axis caption”)
plt.ylabel(“y axis caption”)
plt.plot(x,y)
plt.show()
#若x,y不成线性关系,则为折线图

#要显示圆点来代表点,而不是上面示例中的线,请使用ob作为plot()函数中的格式字符串

x = np.arange(1,11)
y = 2 * x + 5
plt.title(“Matplotlib demo”)
plt.xlabel(“x axis caption”)
plt.ylabel(“y axis caption”)
plt.plot(x,y,“ob”)
#可选其他样式,‘o’圆标记,’.'点标记
plt.show()

subplot()
#在同一图中绘制不同的东西。 在下面的脚本中,绘制正弦和余弦值
#计算正弦和余弦曲线上的点的 x 和 y 坐标
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
#建立 subplot 网格,高为 2,宽为 1
#激活第一个 subplot
plt.subplot(2, 1, 1)
#绘制第一个图像
plt.plot(x, y_sin)
plt.title(‘Sine’)
#将第二个 subplot 激活,并绘制第二个图像
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title(‘Cosine’)
#展示图像
plt.show()

bar()
#条形图
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.bar(x, y, align = ‘center’)
plt.bar(x2, y2, color = ‘g’, align = ‘center’)
plt.title(‘Bar graph’)
plt.ylabel(‘Y axis’)
plt.xlabel(‘X axis’)
plt.show()

#直方图

#numpy.histogram()
#函数将输入数组和bin作为两个参数。 bin数组中的连续元素用作每个bin的边界

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
np.histogram(a,bins = [0,20,40,60,80,100])
hist,bins = np.histogram(a,bins = [0,20,40,60,80,100])
#频数
print(hist)
#间距
print(bins)

#输出
[3 4 5 2 1]
[0 20 40 60 80 100]

plt()

#函数将数据和bin数组作为参数,并转换为直方图

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.hist(a, bins = [0,20,40,60,80,100])
plt.title(“histogram”)
plt.show()

#pandas绘图

#折线图
df = pd.DataFrame(np.random.randn(10,4),index=pd.date_range(‘2018/12/18’,
periods=10), columns=list(‘ABCD’))

df.plot()

#条形图
df = pd.DataFrame(np.random.rand(10,4),columns=[‘a’,‘b’,‘c’,‘d’])
df.plot.bar()

#堆积条形图
df = pd.DataFrame(np.random.rand(10,4),columns=[‘a’,‘b’,‘c’,‘d’])
df.plot.bar(stacked=True)

#水平条形图
df = pd.DataFrame(np.random.rand(10,4),columns=[‘a’,‘b’,‘c’,‘d’])
df.plot.barh(stacked=True)

#直方图
df = pd.DataFrame({‘a’:np.random.randn(1000)+1,‘b’:np.random.randn(1000),‘c’:
np.random.randn(1000) - 1}, columns=[‘a’, ‘b’, ‘c’])
df.plot.hist(bins=20)

#为每列绘制不同的直方图
df=pd.DataFrame({‘a’:np.random.randn(1000)+1,‘b’:np.random.randn(1000),‘c’:
np.random.randn(1000) - 1}, columns=[‘a’, ‘b’, ‘c’])
df.hist(bins=20)

#箱型图
df = pd.DataFrame(np.random.rand(10, 5), columns=[‘A’, ‘B’, ‘C’, ‘D’, ‘E’])
df.plot.box()

#散点图
df = pd.DataFrame(np.random.rand(50, 4), columns=[‘a’, ‘b’, ‘c’, ‘d’])
df.plot.scatter(x=‘a’, y=‘b’)

#饼状图
df = pd.DataFrame(3 * np.random.rand(4), index=[‘a’, ‘b’, ‘c’, ‘d’], columns=[‘x’])
df.plot.pie(subplots=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值