python 绘制简单的折现图

1,先要安装 matplotlib
2,生成第一个图
这里我们创建一个列表,在其中存入前述平方数,再将这个列表传递给函数plot(),这个函数尝试根据这些数字绘制出一些有意义的图形。plt.show()打开 matplotlib 查看器,并显示绘制的图形。

import matplotlib.pyplot as plt
squars = [1, 4, 9, 16, 25]
plt.plot(squars)
plt.show()

运行结果
在这里插入图片描述
修改标签文字和线条粗细
参数linewidth()决定plot()绘制的线条的粗细。函数title()给图标指定标题。多次出现fontsize,这个是指定文字大小的。函数xlabel()和ylabel() 是设置横坐标和纵坐标的标题,而函数tick_parems()设置刻度的样式,其中指定的参数将影响x轴和y轴上的刻度( axis=‘both’),并将刻度标记的字号设置为14 (labelsize=14)

import matplotlib.pyplot as plt
squars = [1, 4, 9, 16, 25]
plt.plot(squars, linewidth=5)

设置图标题,并给坐标轴加上标签

plt.title(“Sqyuare Numbers”, fontsize=24)
plt.xlabel(“Value”, fontsize=14)
plt.ylabel(“Square of Value”, fontsize=14)

设置刻度标记大小

plt.tick_params(axis=‘both’, labelsize=14)
plt.show()

运行结果
在这里插入图片描述
校正图形
原本x轴值为0 修改成1

import matplotlib.pyplot as plt
input_values = [1, 2, 3, 4, 5]
squars = [5, 4, 9, 16, 25]
plt.plot(input_values, squars, linewidth=5)

设置图标题,并给坐标轴加上标签

plt.title(“Sqyuare Numbers”, fontsize=24)
plt.xlabel(“Value”, fontsize=14)
plt.ylabel(“Square of Value”, fontsize=14)

设置刻度标记大小

plt.tick_params(axis=‘both’, labelsize=14)
plt.show()

运行结果
在这里插入图片描述
使用scatter()绘制散点图并设置其样式
使用 scatter()传递x轴和y轴

import matplotlib.pyplot as plt
plt.scatter(2, 4)
plt.show()

运行结果

在这里插入图片描述
设置输出样式,并添加标题
调用scatter(),使用实参s 设置了绘制图形使用的点的尺寸

import matplotlib.pyplot as plt
plt.scatter(2, 4, s=200)
#设置图标题,并给坐标轴加上标签
plt.title(“Sqyuare Numbers”, fontsize=24)
plt.xlabel(“Value”, fontsize=14)
plt.ylabel(“Square of Value”, fontsize=14)
#设置刻度标记大小
plt.tick_params(axis=‘both’, which=‘major’, labelsize=14)
plt.show()

运行结果

在这里插入图片描述
绘制一系列点
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.scatter(x_values, y_values, s=200)
#设置图标题,并给坐标轴加上标签
plt.title(“Sqyuare Numbers”, fontsize=24)
plt.xlabel(“Value”, fontsize=14)
plt.ylabel(“Square of Value”, fontsize=14)
#设置刻度标记大小
plt.tick_params(axis=‘both’, which=‘major’, labelsize=14)
plt.show()

运行结果
在这里插入图片描述
自动计算数据
创建一个包含x值的列表,在利用该列表运算y 列表
axis()函数 他是限制x轴和y轴的 ,所以要求提供4个值(最大与最小)

import matplotlib.pyplot as plt
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, s=40)
#设置图标题,并给坐标轴加上标签
plt.title(“Sqyuare Numbers”, fontsize=24)
plt.xlabel(“Value”, fontsize=14)
plt.ylabel(“Square of Value”, fontsize=14)
#设置刻度标记大小
plt.tick_params(axis=‘both’, which=‘major’, labelsize=14)
plt.axis([0, 1100, 0, 1100000])
plt.show()

运行结果
在这里插入图片描述
删除数据点的轮廓
plt.scatter(x_values, y_values, edgecolor=‘none’, s=40)
自定义颜色
plt.scatter(x_values, y_values, c=‘red’, edgecolor=‘none’, s=40)
使用颜色映射
plt.scatter(x_values, y_values, c=‘red’, cmap=plt.cm.Blues, edgecolor=‘none’, s=40)
自动保存图片
plt.savefig(‘squares_plot’, bbox_inches=‘tight’)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值