Python如果实现图形绘制

发布时间:2021-12-22 15:06:03 作者:小新
来源:亿速云 阅读:151

这篇文章主要介绍Python如果实现图形绘制,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1、画第一个图形

第一个图形从简单的开始。

1.1 代码

# importing the required module

import matplotlib.pyplot as plt



# x axis values

x = [1,2,3]

# corresponding y axis values

y = [2,4,1]



# plotting the points

plt.plot(x, y)



# naming the x axis

plt.xlabel('x - axis')

# naming the y axis

plt.ylabel('y - axis')



# giving a title to my graph

plt.title('My first graph!')



# function to show the plot

plt.show()

1.2 输出

Python如果实现图形绘制

1.3 代码的部分解释

2、在同一图上绘制两条或多条线

如果想在同一张图上再绘制多条线,可反复使用.plot()函数。

2.1 代码

import matplotlib.pyplot as plt



# line 1 points

x1 = [1,2,3]

y1 = [2,4,1]

# plotting the line 1 points

plt.plot(x1, y1, label = "line 1")



# line 2 points

x2 = [1,2,3]

y2 = [4,1,3]

# plotting the line 2 points

plt.plot(x2, y2, label = "line 2")



# naming the x axis

plt.xlabel('x - axis')

# naming the y axis

plt.ylabel('y - axis')

# giving a title to my graph

plt.title('Two lines on same graph!')



# show a legend on the plot

plt.legend()



# function to show the plot

plt.show()

2.2 输出

Python如果实现图形绘制

2.3 代码的部分解释

3、自定义绘图

下面将讨论适用于几乎所有场景的一些基本自定义。

3.1 代码

import matplotlib.pyplot as plt



# x axis values

x = [1,2,3,4,5,6]

# corresponding y axis values

y = [2,4,1,5,2,6]



# plotting the points

plt.plot(x, y, color='green', linestyle='dashed', linewidth = 3,marker='o', markerfacecolor='blue', markersize=12)



# setting x and y axis range

plt.ylim(1,8)

plt.xlim(1,8)



# naming the x axis

plt.xlabel('x - axis')

# naming the y axis

plt.ylabel('y - axis')



# giving a title to my graph

plt.title('Some cool customizations!')



# function to show the plot

plt.show()

3.2 输出

Python如果实现图形绘制

3.3 代码的部分解释

如上面代码所示,我们进行了一些自定义的改变:

以上是“Python如果实现图形绘制”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Qt如何实现基础图形绘制
  2. python图形绘制奥运五环实例讲解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:java享元模式有哪些类

下一篇:mysql中出现1053错误怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》