开发者

How do I plot a graph in Python?

I have installed Matplotlib, and I have created two lists, x and y.

I want the x-axis to have values from 0 to 100 in steps o开发者_如何学JAVAf 10 and the y-axis to have values from 0 to 1 in steps of 0.1. How do I plot this graph?


Have a look through the Matplotlib gallery. All the graphs there have their source code available. Find one you like, cut & paste, and dissect!


There is a very good book:

Sandro Tosi, Matplotlib for Python Developers, Packt Pub., 2009.


Use xlim and ylim to set the range to be displayed, [0; 100] and [0; 1] in this case. Use xticks and yticks to control the spacing of the ticks, 10 and 0.01 in this case (11 steps for both).

Complete example

import pylab as pl
import numpy as np

# Sample data
X = np.linspace(-5, 105, 2000, endpoint = True)
Cosine, Sine = 0.45 * np.cos(0.2*X) + 0.5, 0.45 * np.sin(0.2*X) + 0.5


# Plot
pl.plot(X, Cosine)
pl.plot(X, Sine)


# Set x and y limits
pl.xlim(0.0, 100.0)
pl.ylim(0.0,   1.0)


# Set ticks for x and y axis
pl.xticks(np.linspace(0.0, 100.0, 11, endpoint = True))
pl.yticks(np.linspace(0.0,   1.0, 11, endpoint = True))


pl.show()

Result

How do I plot a graph in Python?


use the arange function to stepwise your intervals for X and Y

X=np.arange(0,110,10)
Y=np.arange(0,1.1,.1)
print(Y)
plt.scatter(X,Y)

Cosine = np.cos(X) + 0.5
plt.plot(X, Cosine)
plt.show()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜