简体   繁体   English

绘制简单图形的最简单的python代码是什么(比matlab简单)

[英]What is the simplest python code to plot a simple graph (simpler than matlab)

If I want to draw ay=x^2 graph from 0 to 9 in matlab, I can do如果我想在 matlab 中从 0 到 9 绘制 ay=x^2 图,我可以这样做

a = [0:1:10]
b = a.^2
plot(a,b)

Using python, I can do the same like below使用python,我可以像下面一样做

import matplotlib.pyplot as plt
import numpy as np

a=[x for x in xrange(10)]
b=np.square(a)
plt.plot(a,b)
plt.show()

But to the contrary to my belief that python code is simpler, this takes more lines that matlab.但与我认为 python 代码更简单的看法相反,这需要更多的行数。 (I guess python tries to make things light weight, so we need to import things when we actually need something, hence more lines..) Can I make above python code simpler(I mean shorter)? (我猜python试图让事情变得轻量级,所以我们需要在我们真正需要的时候导入东西,因此更多的行..)我可以让上面的python代码更简单(我的意思是更短)?

EDIT : I know it doesn't matter and meaningless when it comes to processing time but I was just curious how short the code can become.编辑:我知道在处理时间方面这无关紧要且毫无意义,但我只是好奇代码可以变得多短。

This is a bit simpler这个简单一点

import matplotlib.pyplot as plt

X = range(10)
plt.plot(X, [x*x for x in X])
plt.show()

but remember that Python is a general purpose language, so it's not surprising it requires a bit more than a specific charting/math tool.但请记住,Python 是一种通用语言,因此它需要的不仅仅是特定的图表/数学工具,这并不奇怪。

The main clutter is importing the libraries that help about chart plotting and numeric computation, as this is implied for matlab (a tool designed around that).主要的混乱是导入有助于图表绘制和数值计算的库,因为这对 matlab(一个围绕它设计的工具)来说是隐含的。

These lines are however needed only once: they're not a factor, but just an additive constant, going to be negligible even in just slightly more serious examples.然而,这些线只需要一次:它们不是一个因素,而只是一个附加常数,即使在稍微更严重的例子中也可以忽略不计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM