Matplotlib basic commands

·

1 min read

First, we have to import matplotlib like below.

import matplotlib.pyplot as plt

Give an x-axis and y-axis array:

You have to make the length of the two array x and y the same.
x = [0,1,2,3,4]
y = [3,4,5,6,7]

To plot x and y data, you can use the following command.
plt.plot(x, y)

If you want to add a label and other features, you can do following things.

plt.plot(x, y, label="2x", marker=".",linestyle="--", markersize=12 ,color="#ababab", linewidth=2)

label is the label for this graph. 
marker defines how each dot is displayed. you can set it as "x" as well.
linestyle defines the linestyle.

Declaring each of them might be bothersome for you. In that case, you can use this.

plt.plot(x, y, 'rx--',label="2x") #shorthand for colour, marker, and linestyle

In this way, you can set the colour, marker, and line style. In the code example above, the line colour is red, the marker is "x", and the line is "--".

Also, you can set ticks, title, x and y labels.

x = [0,1,2,3,4]
y = [3,4,5,6,7]

plt.plot(x, y, 'rx--',label="2x") #shorthand for colour, marker, and linestyle
plt.title("Example Graph", fontdict={'fontsize': 13, 'fontname': 'Comic Sans MS'})
plt.xlabel("x axist")
plt.ylabel("y axist")

plt.xticks([0,2,4])
plt.yticks([3,5,7])
plt.legend() #This shows the label in the graph.

plt.show() #to delete this <matplotlib.lines.Line2D at