Plot Graph to Show Ideas - Installation and Scatter Plot

When we do experiments to observe or demonstrate some ideas, one graph can produce a correct intuition if you use a proper graph. For example, if you want to observe the distribution of data, the bar or line graphs are not sufficient to show this information. Because they can only show the average and variance of data. If the data is plotted by the violin or CDF (cumulative distribution function), they will show the distribution of data in a better way.

Of course, how to visualize data is not easy work and I’m not a profession here. I just want to introduce 1) how to plot some general graphs by Python3 (Matplotlib package); 2) these graphs are suited to which situations.

If you know better references, please feel free to send them to me by Email. (zurielibra@gmail.com)

More examples of data visualization can be found on the website of Matplotlib.

Install Matplotlib

The installation instruction can be found on the website of Matplotlib. If you have pip, you can use the following command to install directly.

1
$ pip install matplotlib

Scatter Plot (2D)

A comprehensive description of the scatter plot can be found on Wikipedia.

TL;DR: the scatter plot is usually used to find the distribution of data with two dimensions.

To draw the scatter plot, we can use the code shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from matplotlib import pyplot as plt
# fake data
x = [1,2,3,4]
y = [1,4,9,16]

# function that you use to plot
plt.scatter(x, y, marker='o', color="g", label="green")
x = [1,2,3,4]
y = [2,5,10,17]
plt.scatter(x, y, marker='x', color="r", label="read")
x = [1,2,3,4]
y = [0.5,3,8,15]
plt.scatter(x, y, marker='*', color="b", label="blue")

# create legend
plt.legend()

# set label of x and y axies
plt.xlabel("x")
plt.ylabel("y")

# set title
plt.title("scatter plot")

# present graph
plt.show()

scatter

You can just copy line 7 to plot data.

In the function plt.scatter(), only the first two variables are necessary. If you want to beauty your graph, you can play with marker,color, etc. If you plot several datasets on one graph, you may want to use label and plt.legend() to distinguish these datasets.

To make your graph readable, lables for x-axis and y-axis are necessary. You can set them by plt.xlabel() and plt.ylabel() easily. The title of graph can also help reader to understand graph, so you can set it by plt.title(). However, title should not be plot if this graph is used in academic publications.

To make your graph readable, labels for the x-axis and y-axis are necessary. You can set them by plt.xlabel() and plt.ylabel() easily. The title of the graph can also help the reader to understand the graph, so you can set it by plt.title(). However, the title should not be plot if this graph is used in academic publications.

After all of the code for configuration, you only need plt.show() to present the final graph. You can also use plt.savefig() to save the graph into any format (pdf, png, jpg, etc.).

Recommended Posts