记录下最近学习的matplotlib。

import matplotlib.pyplot as plt
import numpy as np 
plt.rcParams['font.sans-serif']=['SimHei']##中文黑体
def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)
t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,4.0,0.2)
fig=plt.figure()
ax1=fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
ax4=fig.add_subplot(2,2,4)###一个画布画4个图片
x=[1,2,8]
ax1.pie(x)
ax2.plot(t1,f(t1),label='one')
x=['1','x','4','t']
y=[11,12,111,23]
ax3.bar(x,y,color='r')
ax4.plot(t,f(t),'r')
ax4.set_xticks([0,2,4,6,8,10])
ax4.set_yticks([0.1,0.2,0.4,0.6,0.8,1.0])
ax4.set_title('折线图')
plt.show()

效果如下所示:
matplotlib学习

def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)
t=np.arange(0.0,4.0,0.2)
fig=plt.figure()
ax=fig.add_subplot(1,1,1)

ax.plot(2*t,f(t),'b',label='one')
ax.plot(t,f(t),'r',label='two')
ax.set_xticks([0,2,4,6,8,10])
ax.set_yticks([0.1,0.2,0.4,0.6,0.8,1.0])#给x y轴指定刻度
ax.set_title('线图')
ax.legend(loc='best')#####添加图例,loc=‘best’找寻最佳位置
ax.text(1,f(1)+0.1,'hello')##标注图上的点
ax.annotate('测试点',xy=(2,f(2)),xytext=(3,f(2)),arrowprops=dict(facecolor='red'))#指定箭头,添加文字
ax.grid()##网格线

matplotlib学习
上午爬取的一些数据,用来学习画图。

matplotlib学习

fig1=plt.figure( figsize=(12,10))
bx=fig1.add_subplot(2,1,1)
count={}
for info in f.iloc[:,-2]:
    s=info.split(',')
    for addr in s:
        if addr not in count:
            count[addr]=0
        count[addr]+=1   ####统计各个地区有多少电影
y=[count[i] for i in count]
x=[i for i in count]
bx.set_title('地区电影统计',fontsize=20)
bx.set_xlabel('地区',fontsize=20)
bx.set_ylabel('数量',fontsize=20)###表的标题,xy轴名称及大小,color参数可以设置颜色。
bx.tick_params(color='r')##刻度线的颜色
plt.xticks(rotation=90)#x轴的名称旋转90for a,b in zip(x,y):
    plt.text(a,b+0.1,b)  ###在柱状图的顶部显示数量      
bx.bar(x,y,color='g')###画图

matplotlib学习

####大饼图
plt.rcParams['font.sans-serif']=['SimHei']##中文黑体
plt.figure( figsize=(12,10))
bx=fig1.add_subplot(1,1,1)
count={}
for info in f.iloc[:,-3]:
    s=info.split(',')
    for addr in s:
        if addr not in count:
            count[addr]=0
        count[addr]+=1####计算电影各种类型的数量
x=[i for i in count]
y=[count[i] for i in count]

paches,out_text,inner=plt.pie(y,labels=x,autopct='%.1f %%',startangle=90)
‘’‘接收三个参数,out_text是外部文字,inner是内部文字’‘’
for text in inner:
    text.set_size(15)###设置饼图外部文字的大小
    text.set_color('w')
plt.legend(loc='best')
plt.show()

matplotlib学习
感觉差不多了,画图这方面应该算是入门了。

相关文章:

  • 2021-09-07
  • 2021-04-23
  • 2021-12-12
  • 2021-10-25
  • 2021-07-11
  • 2021-06-16
猜你喜欢
  • 2021-08-26
  • 2021-06-23
  • 2022-01-01
  • 2021-12-27
  • 2022-01-03
相关资源
相似解决方案