Python练习题(三)

直方图

  1. 题目:

    输出:

    代码:
		import numpy as np
		import matplotlib.pyplot as plt

		print(plt.style.available)  # 打印图像风格
		plt.style.use('seaborn-talk')  # 设置图像风格
		fig, ax = plt.subplots()
		ax.set_title('cube numbers')
		x = np.array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
		y = x*x*x
		plt.bar(x, y, color='y')
		for a, b in zip(x, y):
		    plt.text(a, b/2, '%d'%b, ha='center', va='bottom', fontsize=10)
		plt.show()
  1. 题目:

    输出:

    代码:
		import matplotlib.pyplot as plt
		import random

		plt.style.use('ggplot')
		fig, ax = plt.subplots(ncols=1, nrows=2)
		ax1, ax2 = ax.ravel()

		# 创建20001个范围为0-1000的随机数
		L = []
		for i in range(20001):
		    L.append(random.randint(0, 1000))

		# 用字典D1统计每个数值的个数,将每5作为一个数段,用字典D2统计每个数段的个数
		D1, D2 = {
   }, {
   }
		for i in L:
		    D1[i] = D1.get(i, 0) + 1  # 累加每个值的个数
		    D2[i/5*5] = D2.get(i/5*5, 0) + 1

		# 利用直方图展示数据分布情况
		ax1.axis([0, 1000, 0, 40])  # 设置x轴和y轴的最小和最大值
		ax1.bar(D1.keys(), D1.values(), 1, alpha=0.5, color='b')  # 第三个参数是宽度,第四个是透明度
		ax2.axis([0, 1000, 0, 40])
		ax2.bar(D2.keys(), D2.values(), 5, alpha=0.5, color='y')

		plt.show()
  1. 题目:

    输出:

    代码:
		import numpy as np
		import matplotlib.pyplot as plt

		fig, ax = plt.subplots()
		plt.rcParams['font.sans-serif'] = ['SimHei']  # 添加对中文字体的支持

		# 创建每个学生三个学期的排名
		S1 = [4, 6, 8, 10, 11, 14, 46, 48, 60, 71]
		S2 = [10, 4, 1, 8, 23, 34, 71, 43, 123, 90]
		S3 = [6, 5, 7, 10, 25, 20, 50, 33, 78, 64]

		x = np.arange(1, 11)  # 生成横轴数据
		plt.bar(x, S1, 0.25, alpha=0.5, color='b')  # 生成本学期排名直方图,宽度为0.25
		plt.bar(x+0.25, S2, 0.25, alpha=0.5, color='r')
		plt.bar(x+0.5, S3, 0.25, alpha=0.5, color='y')

		for a, b in zip(x, S1):
		    plt.text(a, b+0.2, '%d'%b, ha='center', va='bottom', fontsize=9)
		for a, b in zip(x, S2):
		    plt.text(a+0.25, b+0.2, '%d'%b, ha='center', va='bottom', fontsize=9)
		for a, b in zip(x, S3):
		    plt.text(a+0.5, b+0.2, '%d'%b, ha='center', va='bottom', fontsize=9)

		plt.legend(['本学期', '上学期', '上上学期'], loc='upper left')
		plt.grid(True, linestyle='--', alpha=0.8)  # 设置网格线

		plt.show()

线形图

  1. 题目:

    输出:

    代码:
		import numpy as np
		import matplotlib.pyplot as plt

		fig, ax = plt.subplots()
		x = np.linspace(-7, 11, 16)  # 在-7到11的区间内产生16个数据
		y1 = -80*x + 800
		y2 = (1/3)**x
		plt.plot(x, y1, 'r+', color='red', linewidth=1.0, linestyle='-', label='line1')
		plt.plot(x, y2, 'bo', color='blue', linewidth=2.0, linestyle='--', label='line2')
		plt.xlim(-8, 12)
		ax.legend(['y=-80x+800', 'y=(1/3)^x'], loc='upper right')
		plt.show()
  1. 题目:

    输出:

    代码:
		import numpy as np
		import matplotlib.pyplot as plt

		fig, ax = plt.subplots()
		x = np.linspace(-5*np.pi, 5*np.pi, 512)  # 生成从-5π到5π的512个数据
		cos, sin = 2*np.cos(x), np.sin(3*x)
		ax.set_xticks([-5*np.pi, -4*np.pi, -3*np.pi, -2*np.pi, -1*np.pi, 0, np.pi, 2*np.pi, 3*np.pi, 4*np.pi, 5*np.pi])  # 设置x轴刻度
		plt.plot(x, cos, color='blue', linewidth=2.0, linestyle='-', label='cos')
		plt.plot(x, sin, color='red', linewidth=2.0, linestyle='--', label='sin')

		ax.spines['right'].set_visible(False)  # 隐藏右边框
		ax.spines['top'].set_visible(False)
		ax.spines['left'].set_position(('data', 0))  # 设置左边框到x轴0的位置
		ax.yaxis.set_ticks_position('left')  # 刻度值设置在左侧
		ax.spines['bottom'].set_position(('data', 0))
		ax
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值