绘制简单的折线图-matplotlib

本文介绍如何使用Python的matplotlib库绘制平方数的折线图和散点图,包括自定义颜色和保存图表的方法。

1.平方数折线图

import matplotlib.pyplot as plt

input_values=[1,2,3,4,5]
squares=[1,4,9,16,25]
plt.plot(input_values,squares,linewidth=5)

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

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

 

2.平方数自动生成散点图

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,c=(0.7,0,0),edgecolor='none',s=40)

plt.title("Square 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()

散点默认为蓝色点和黑色轮廓,可通过参数c指定自定义颜色,并将其设置一个元组,其中包含三个0~1之间的小数值,它们分别表示红色、绿色和蓝色分量。

 

使用颜色映射:

plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolor='none',s=40)

 

自动保存图表可将对plt.show()的调用替换为对plt.savefig()的调用:

plt.savefig('squares_plot.png',bbox_inchs='tight')

其中第二个参数指定将图表多余的空白区域裁剪掉。

 

转载于:https://www.cnblogs.com/exciting/p/9005399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值