dave
dave

Reputation: 63

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 of 10 and the y-axis to have values from 0 to 1 in steps of 0.1. How do I plot this graph?

Upvotes: 6

Views: 7967

Answers (3)

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()

Upvotes: 0

Peter Mortensen
Peter Mortensen

Reputation: 31599

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

Plot created by Matplotlib

Upvotes: 2

Papiro
Papiro

Reputation: 159

There is a very good book:

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

Upvotes: 2

Related Questions