绘制各种图表

# matplotlib中文网 https://www.matplotlib.org

%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt

# 0.设置中文
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False

# 1.准备大数据专业三位同学各科目成绩
dim_num = 6
radians = np.linspace(0, 2 * np.pi, dim_num, endpoint=False)
radians = np.concatenate((radians, [radians[0]]))  

# A同学: '数学','英语','线代','爬虫','数据可视化','吃喝玩乐'  --->   41,38,36,62,68,100
score_a = np.array([41,38,36,62,68,100])
score_a = np.concatenate((score_a, [score_a[0]]))  
# B同学:'数学','英语','线代','爬虫','数据可视化','吃喝玩乐'  --->   91,88,83,72,78,26
score_b = np.array([91,88,83,72,78,26])
score_b = np.concatenate((score_b, [score_a[0]]))
# C同学:'数学','英语','线代','爬虫','数据可视化','吃喝玩乐'  --->   81,78,85,76,74,36
score_c = np.array([81,78,85,76,74,36])
score_c = np.concatenate((score_c, [score_c[0]]))
# 2.绘制多边形(雷达图) (只允许调用一次plt.polar)
plt.polar(radians,score_c)
# 3.设置维度标签
radar_labels = ['数学','英语','线代','爬虫','数据可视化','吃喝玩乐']
radar_labels = np.concatenate((radar_labels, [radar_labels[0]])) # 拼接一下,构成闭环 (此处可以不拼接,为了统一而已)

# 4.设置极坐标的标签
angles = radians * 180/np.pi  # 弧度转角度
plt.thetagrids(angles, labels=radar_labels) # 设置新的刻度标签

# 5.填充多边形(只允许调用一次plt.fill)
plt.fill(radians,score_a,'purple',radians,score_b,'g',radians,score_c,'blue',alpha=0.25)
plt.show()

3705534ca476440aa827bc266db317db.jpeg

# matplotlib中文网 https://www.matplotlib.org


# 需求一: 利用subplots()创建2个子图,即1行2列,在极坐标系下绘制花瓣图
# (提示:查看subplots的官网API文档,利用subplot_kw参数控制坐标系类型,即投影方式)

# 需求二: 画布总标题为“花瓣图”

%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ["sans-serif"]

#1. 准备数据
theta = np.linspace(0, 2*np.pi, 400)
r = np.sin(theta**2)


#2. 绘图(极坐标系下绘制曲线和散点)
#ax=plt.polar(theta,r)
#ax1 = plt.polar(theta,r,'.')
fig,(ax1,ax2) = plt.subplots(1,2,subplot_kw=dict(projection='polar'))
ax1.plot(theta,r,color='g')
ax2.scatter(theta,r,s=1.2,color='r')
# 必须利用subplots(),而不能利用subplot()
# 提示:查看subplots的官网API文档,利用subplot_kw参数控制坐标系类型,即投影方式

#ax_arr = plt.subplots(1,2)
#ax_arr[0] = plt.polar(theta,r,'--')
#ax_arr[1] = plt.plot(theta,r,'red')


#3. 设置画布中文标题“花瓣图”(字号40,颜色为红色)

#ax.set_suptitle('花瓣图')
fig.suptitle('花瓣图',color='r')


#4. 展示图表
plt.tight_layout()
plt.show()

 8b8f7214cf504c408a07d6291a65bcc5.jpeg

 

# 需求一: 利用subplot2grid()修改下列代码,使其运行结果如图所示(2行4列)


%matplotlib auto
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ["SimHei"]

x = [x for x in range(1, 13)]
y1 = [20, 28, 23, 16, 29, 36, 39, 33, 31, 19, 21, 25]
y2 = [17, 22, 39, 26, 35, 23, 25, 27, 29, 38, 28, 20]
labels = ['1 月', '2 月', '3 月', '4 月', '5 月', '6 月', '7月', '8 月', '9 月', '10 月', '11 月', '12 月']

# 绘制第一个子图
#ax1 = plt.subplot(211)
ax1 = plt.subplot2grid((2,4),(0,1),colspan=2)
ax1.plot(x, y1, 'm--o', lw=2, ms=5, label='产品A')
ax1.plot(x, y2, 'g--o', lw=2, ms=5, label='产品B')
ax1.set_title("产品A 与产品B的销售额", fontsize=11)
ax1.set_ylim(10, 45)
ax1.set_ylabel('销售额(亿元)')
ax1.set_xlabel('月份')
#
for xy1 in zip(x, y1):
    ax1.annotate("%s" % xy1[1], xy=xy1, xytext=(-5, 5), textcoords='offset points')
for xy2 in zip(x, y2):
    ax1.annotate("%s" % xy2[1], xy=xy2, xytext=(-5, 5), textcoords='offset points')
ax1.legend()

# 绘制第二个子图
ax2 = plt.subplot(223)
ax2.pie(y1, radius=1, wedgeprops={'width':0.5}, labels=labels, autopct='%3.1f%%', pctdistance=0.75)
ax2.set_title('产品A的销售额 ')
#ax1 = plt.subplot2grid(rowspan=0,colspan=1)
# 绘制第三个子图
ax3 = plt.subplot(224)
ax3.pie(y2, radius=1, wedgeprops={'width':0.5}, labels=labels,autopct='%3.1f%%', pctdistance=0.75)
ax3.set_title('产品B的销售额 ')

# 调整子图之间的距离
plt.tight_layout()
plt.show()



7f551a5958c0413e9cce5670904807aa.jpeg

# 需求一:绘制动画,让星星一会儿出现,一会儿消失,间隔时间为1秒(见运行结果展示图)
# 提示:利用scatter返回的示例,调用PatchCollection类中的set()方法,可以隐藏散点
#       当然,也可以采用你自己的方法!只要实现相同效果即可。



# matplotlib中文网 https://www.matplotlib.org

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

# 1.生成测试数据
xx = np.array([13, 5, 25, 13, 9, 19, 3, 39, 13, 27])
yy = np.array([4, 38, 16, 26, 7, 19, 28, 10, 17, 18])
zz = np.array([7, 19, 6, 12, 25, 19, 23, 25, 10, 15])

# 2.创建画布和3d坐标系实例
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 3.绘制初始的3D散点图
star = ax.scatter(xx, yy, zz, c='orange', marker='*', s=160, lw=1, edgecolor='k')

# 4.更新画面函数(回调函数)
def update(i):
    if i % 2:
        #color = 'black'
        ax.set(visible=False)  #完全等价于:star.set_color('white')
    else:
        color = 'orange'
        ax.set(visible=True)
    
    star = ax.scatter(xx, yy, zz, c=color, marker='*', s = 160, lw=1, edgecolor='k')  
    return star
	
def init_draw():
    star = ax.scatter(xx, yy, zz, c='orange', marker='*', s = 160, lw=1, edgecolor='k')  
    return star

# 5.创建动画实例
ani = FuncAnimation(fig=fig, func=update, frames=None, interval=20, blit=True)

# 6.展示图表
plt.show()

9edafec19ae845fba51ff6ec9f89eeb8.gif 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值