[PYTHON] Pharmaceutical company researchers summarized Matplotlib

Introduction

Here, we will explain about Matplotlib for Python beginners. It is supposed to use Python3 series.

Loading the library

Like any other library, load it with ʻimport. % matplotlib inlline` is a description for drawing a graph on a notebook in Jupyter Notebook.

matplotlib_1.py


%matplotlib inline
import matplotlib.pyplot as plt

Line graph

A line graph can basically be drawn as follows.

matplotlib_2.py


%matplotlib inline
import matplotlib.pyplot as plt


x = [1, 2, 3]
y = [3, 1, 2]
plt.title('Line-chart') #Graph title
plt.xlabel('X-axis') #x-axis label
plt.ylabel('Y-axis') #y-axis label

plt.plot(x, y) #Create a graph
plt.savefig('matplotlib_2.png') #Save the graph as an image file

matplotlib_2.png

To draw multiple line graphs, write as follows.

matplotlib_3.py


%matplotlib inline
import matplotlib.pyplot as plt


plt.plot([1, 2, 3])
plt.plot([3, 1, 2])
plt.title('Line-chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend(['Line-1', 'Line-2']) #Usage Guide

plt.show() #Show graph
plt.savefig('matplotlib_3.png')

matplotlib_3.png

You can also rewrite the above code as follows:

matplotlib_4.py


%matplotlib inline
import matplotlib.pyplot as plt


fig, ax = plt.subplots()

ax.plot([1, 2, 3])
ax.plot([3, 1, 2])
ax.set_title('Line-chart')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.legend(['Line-1', 'Line-2'])

plt.show()
plt.savefig('matplotlib_4.png')

matplotlib_4.png

You can also arrange multiple graphs vertically.

matplotlib_5.py


%matplotlib inline
import matplotlib.pyplot as plt


fig, ax = plt.subplots(2) #Line up in two lines
plt.subplots_adjust(wspace=1, hspace=1) #Space between graphs

ax[0].plot([1, 2, 3])
ax[0].set_title('Line-chart-1')
ax[0].set_xlabel('X-axis')
ax[0].set_ylabel('Y-axis')
ax[1].plot([3, 1, 2])
ax[1].set_title('Line-chart-2')
ax[1].set_xlabel('X-axis')
ax[1].set_ylabel('Y-axis')

plt.show()
plt.savefig('matplotlib_5.png')

matplotlib_5.png

Similarly, you can arrange them side by side.

matplotlib_6.py


%matplotlib inline
import matplotlib.pyplot as plt


fig, ax = plt.subplots(1, 2) #Arrange in 1 row and 2 columns
plt.subplots_adjust(wspace=1, hspace=1)

ax[0].plot([1, 2, 3])
ax[0].set_title('Line-chart-1')
ax[0].set_xlabel('X-axis')
ax[0].set_ylabel('Y-axis')
ax[1].plot([3, 1, 2])
ax[0].set_title('Line-chart-1')
ax[1].set_xlabel('X-axis')
ax[1].set_ylabel('Y-axis')

plt.show()
plt.savefig('matplotlib_6.png')

matplotlib_6.png

bar graph

The bar graph can be drawn as follows.

matplotlib_7.py


%matplotlib inline
import matplotlib.pyplot as plt


x = [1, 2, 3]
y = [3, 1, 2]

plt.bar(x, y, tick_label=['Bar-1', 'Bar-2', 'Bar-3']) #Create a bar graph by specifying data and label name

plt.show()
plt.savefig('matplotlib_7.png')

matplotlib_7.png

When arranging multiple bar graphs side by side, write as follows.

matplotlib_8.py


%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np


y1 = [3, 1, 2]
y2 = [2, 3, 1]
x = np.arange(len(y1))

bar_width = 0.3 #Specify the width of the bar graph

plt.bar(x, y1, width=bar_width, align='center')
plt.bar(x+bar_width, y2, width=bar_width, align='center')
plt.xticks(x+bar_width/2, ['Bar-1', 'Bar-2', 'Bar-3'])

plt.show()
plt.savefig('matplotlib_8.png')

matplotlib_8.png

When stacking vertically, write as follows.

matplotlib_9.py


%matplotlib inline
import matplotlib.pyplot as plt


x = [1, 2, 3]
y1 = [3, 1, 2]
y2 = [2, 3, 1]

plt.bar(x, y1, tick_label=['Bar-1', 'Bar-2', 'Bar-3'])
plt.bar(x, y2, bottom=y1) #Put y2 on top of y1

plt.show()
plt.savefig('matplotlib_9.png')

matplotlib_9.png

histogram

When drawing the histogram, write as follows.

matplotlib_10.py


%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np


num_random = np.random.randn(100)
plt.hist(num_random, bins=10) #Create a histogram

plt.show()
plt.savefig('matplotlib_10.png')

matplotlib_10.png

Scatter plot

The drawing of the scatter plot is as follows.

matplotlib_11.py


%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np


x = np.random.choice(np.arange(100), 100)
y = np.random.choice(np.arange(100), 100)

plt.scatter(x, y) #Create a scatter plot

plt.show()
plt.savefig('matplotlib_11.png')

matplotlib_11.png

pie chart

A pie chart can be drawn as follows.

matplotlib_12.py


%matplotlib inline
import matplotlib.pyplot as plt


percent_data = [45, 25, 15, 10, 5]

plt.pie(percent_data, labels=['data-1', 'data-2', 'data-3', 'data-4', 'data-5']) #Create a pie chart (ellipse)
plt.axis('equal') #Make it circular

plt.show()
plt.savefig('matplotlib_12.png')

matplotlib_12.png

Summary

Here, I explained how to draw line graphs, bar graphs, histograms, scatter plots, and pie charts using Matplotlib. I want to be able to select the appropriate data visualization method according to the purpose.

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python's data structures
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers have summarized the operators used in Python
How to install Python for pharmaceutical company researchers
A pharmaceutical company researcher summarized the basic description rules of Python