I tried to summarize all the Python plots used in the research by active science graduate students [Basic]

For advanced version, go to here https://qiita.com/coffiego/items/dfabde5f8588723b32d6

Introduction

I am currently a graduate student with a second year master's degree. I am studying the atmosphere of Mars using numerical simulations. I used to analyze the results obtained by numerical simulation mainly using Python, so I tried to summarize all the functions used in the process. Please note that it is not exhaustive. .. .. I've just looked at the documentation and struggled to use it, so I'm hoping that if anyone is going to use Python's visualization tools, it'll help!

Target

Since it's a basic edition, this article deals only with basic functions. This article explains all the elements that draw the graph below. The final sample code is at the bottom of the article. (If you want to remember the plot method, this sample is enough) Screen Shot 2020-03-18 at 17.55.44.png

Library to use

The libraries used in this article are as follows. There is no problem if you copy and paste the following code on the code for the time being.

import matplotlib.pyplot as plt #Actually plot
import numpy as np #Data organization

Actually plot

Because it is a basic edition

1. Plot data 1 [x1, x2, x3 ,,,] and data 2 [y1, y2, y3 ,,,] on the horizontal axis and vertical axis.
2. Plot y = f (x)
3. Explanation of plot decoration options (color, line type, etc.)

1. Plot [x ,,,] and [y ,,,]

I think this is a lot in other articles, but I will leave it as a basis for the time being. This is the most basic. Plot data x on the horizontal axis and data y on the vertical axis.

import matplotlib.pyplot as plt #Actually plot
import numpy as np #Data organization

X = np.linspace(1,10,num=10) #1 to 10 num=Make a row divided into 10 equal parts[1,2,3,,,,10]
#X = [1,2,3,4,5,6,7,8,9,10]Same as
Y = np.random.rand(10) #random number(0~1)Generates a column of size 10 consisting of

plt.plot(X,Y)#plot

The basis is plt.plot (data 1 column, data 2 column). It's dirty because I just plot the random numbers, but I can make a graph like this. Screen Shot 2020-03-18 at 14.41.25.png

2. Plot y = f (x)

Below we plot y = x ^ 2

def fun(x): 
    y = x**2 #Write your function here
    #ex)
    #y = np.sin(x)
    #y = np.log(x)
    return y

X = np.linspace(1,10,num=10) #1 to 10 num=Make a row divided into 10 equal parts[1,2,3,,,,10]
Y = fun(X) #Create column of data y
plt.plot(X, Y)#plot
#plt.plot(column x,y column,options)Can be plotted with

I feel like this. Screen Shot 2020-03-18 at 14.27.36.png

3. Various plot options! (This is the main lol)

--Color --Legend (position adjustment, size) --Axis adjustment (range, font size, log scale, scale) --Line type --Put a formula on the label --Put characters directly in the graph

・ Line color

To change the line color, add the following options.

plt.plot(X,Y,color='k') #k is black

Just change the part of color ='k'. Only the basic colors are listed below. Screen Shot 2020-03-18 at 15.03.05.png Please see this official document for a list of many colors.

・ Legend (label)

You can do it with:

plt.legend(loc='lower right',prop={'size':15})
import matplotlib.pyplot as plt #Actually plot
import numpy as np #Data organization
def fun(x): 
    y = 2*x #Write your function here
    return y
X = np.linspace(1,10,num=100) #1 to 10 num=Make a row divided into 100 equal parts
Y1 = fun(X)
Y2 = 20*np.sin(X) #y=20sin(x)
plt.plot(X,Y1,color='b',label='your original')#Plot Y1
plt.plot(X,Y2,color='r', label='sin')#Plot Y2
plt.legend(loc='lower right',prop={'size':15})#This is the legend

The size can be changed with prop = {'size': 15}. The position of the legend can be changed with loc ='lower right'. It can be placed in the approximate position as shown below. Screen Shot 2020-03-18 at 15.33.57.png Click here for details

The sample looks like this. You can add a legend with plt.legend (). Screen Shot 2020-03-18 at 15.23.07.png

-Add axis label & adjust axis (range, character size, log scale, scale)

Axis label added

plt.xlabel('xaxis',size='10') #Add label to x-axis, size='To the size you like'
plt.ylable('yaxis',size='10') #Label added to y-axis

Axis adjustment (range, scale, scale size)

#range
plt.xlim((0,10)) #Range of x: 0-10
plt.ylim((0,20)) #Range of y:0-20
#log scale
plt.xscale('log')#x to logscale
plt.yscale('log')#logscale y
#Scale size adjustment
plt.tick_params(labelsize=12) #labelsize=To the size you like

This is a sample code that I added in various ways.

import matplotlib.pyplot as plt #Actually plot
import numpy as np #Data organization
def fun(x): 
    y = 2**x #Write your function here
    return y

X = np.linspace(1,10,num=100) #1 to 10 num=Make a row divided into 100 equal parts
Y1 = fun(X)
Y2 = np.exp(X)
plt.plot(X,Y1,color='b',label=r'$y=2^x$')#plot
plt.plot(X,Y2,color='r', label=r'$y=e^x$')
plt.legend(loc='lower right',prop={'size':18}) #Add legend
plt.xlabel('xaxis',size='20') #Add label to x-axis, size='To the size you like'
plt.ylabel('yaxis',size='20') #Label added to y-axis
plt.xlim((0,10)) #Range of x: 0-10
plt.ylim((0,100)) #Range of y:0-20
plt.tick_params(labelsize=15) #labelsize=To the size you like

It should look like the graph below. Screen Shot 2020-03-18 at 17.05.02.png

・ Put a formula on the label

Actually, I added a formula to the character label in the sample code above.

plt.plot(X,Y1,color='b',label=r'$y=2^x$')#plot

To put a formula in the letters on the label,

label=r'$Formula$'

You can insert a formula by writing like this. This formula is in Latex format.

・ Line type

It is an option to make it a dashed line or display data points instead of just a line.

Is used. Here is an example.

#linestyle
X = np.linspace(1,10,num=100)
Y1=X
Y2=2*X
Y3=3*X
Y4=4*X
Y5=5*X
plt.plot(X,Y1,linestyle=':',label=':')#plot
plt.plot(X,Y2,linestyle='-.',label='-.')
plt.plot(X,Y3,linestyle='--',label='--')
plt.plot(X,Y4,linestyle='-',label='-')
plt.legend(prop={'size':18})
Screen Shot 2020-03-18 at 17.20.56.png
#Add marker
X = np.linspace(1,10,num=10)
Y1=X
Y2=2*X
Y3=3*X
Y4=4*X
Y5=5*X
plt.plot(X,Y1,marker='.',markersize='10',label='.')#adding marker
plt.plot(X,Y2,marker='v',markersize='12',label='v')#Change the size with markersize
plt.plot(X,Y3,marker='1',markersize='14',label='1')
plt.plot(X,Y4,marker='*',markersize='16',label='*')
plt.legend(prop={'size':18})
Screen Shot 2020-03-18 at 17.31.21.png

There are many marker types listed in the Official Documentation.

・ Enter characters directly in the graph

Sometimes you want to write letters directly on the graph instead of the legend, right? It is useful in such a case

#Coordinate(x,y)To'letter'To'Favorite size'Insert in the size of
plt.text(x,y,'letter',size='Favorite size') #x:x coordinate y:y coordinate

is. This is a sample code.

X = np.linspace(1,10,num=100)
Y=X
plt.plot(X,Y)
plt.text(2,2,'(2,2)',size='10')
plt.text(4,8,'(4,8)',size='15')
plt.text(8,3,'(8,3)',size='20')
plt.xlabel('xaxis') #Add label to x-axis, size='To the size you like'
plt.ylabel('yaxis') #Label added to y-axis
Screen Shot 2020-03-18 at 17.43.35.png It's easy! # Final sample code ```python #Final sample import matplotlib.pyplot as plt #Actually plot import numpy as np #Data organization

def fun(x): y = 2**x #Write your function here return y

plt.figure(dpi=100) #resolution(dpi)change X = np.linspace(1,10,num=50) #1 to 10 num=Make a row divided into 100 equal parts Y1 = fun(X)#Create data Y Y2 = np.exp(X) plt.plot(X,Y1,color='g',label=r'y=2^x', linestyle='-.')#plot plt.plot(X,Y2,color='r', label=r'y=e^x', marker='*',markersize='16')#plot plt.legend(loc='lower right',prop={'size':18}) plt.xlabel('xaxis',size='20') #Add label to x-axis, size='To the size you like' plt.ylabel('yaxis',size='20') #Label added to y-axis plt.xlim((0,10)) #Range of x: 0-10 plt.ylim((0,100)) #Range of y:0-20 plt.tick_params(labelsize=15) #labelsize=To the size you like plt.text(6,50,r'y=2^x',size='15',color='g') #add text plt.text(1,20,r'y=e^x',size='15',color='r') #add text plt.text(1,80,'Good luck!',size='15',color='k')

 It will be as follows!
 <img width="581" alt="Screen Shot 2020-03-18 at 17.55.44.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/199901/473ac50b-6cd4-467c-5071-0bce96de0224.png ">

# Finally
 This article didn't cover niche things like animations and two-axis versions because it's a basic edition. In the next application

 --Axis
 --Animation (gif file creation)
 --contour graph
 --Reading and writing csv files
 --Reading and exporting HDF5 files

 I would like to summarize such things!
 Then!

 [Addition]
 Application 1: https://qiita.com/coffiego/items/dfabde5f8588723b32d6
 Is done!


Recommended Posts

I tried to summarize all the Python plots used in the research by active science graduate students [Basic]
I tried to summarize all the Python visualization tools used in research by active science graduate students [Application]
I tried to summarize the code often used in Pandas
I tried to summarize the commands often used in business
I tried to summarize the commands used by beginner engineers today
I tried to summarize the contents of each package saved by Python pip in one line
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried to summarize the Linux commands used by beginner engineers today-Part 1-
I tried to summarize the methods that are often used when implementing basic algo in Quantx Factory
I tried to graph the packages installed in Python
I tried to summarize the basic form of GPLVM
I tried to summarize how to use pandas in python
I tried to summarize the string operations of Python
I tried to implement the mail sending function in Python
I tried to summarize what python strong people are doing in the competition professional neighborhood
I tried to summarize the frequently used implementation method of pytest-mock
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to summarize the umask command
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to summarize the graphical modeling.
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to touch Python (basic syntax)
I tried to summarize the general flow up to service creation by self-education.
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to verify and analyze the acceleration of Python by Cython
I tried to analyze the New Year's card by myself using python
I tried to open the latest data of the Excel file managed by date in the folder with Python
I tried to organize the evaluation indexes used in machine learning (regression model)
How to read all the classes contained in * .py in the directory specified by Python
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I tried to summarize the operations that are likely to be used with numpy-stl
I tried to implement TOPIC MODEL in Python
I tried using the Datetime module by Python
I tried to predict the change in snowfall for 2 years by machine learning
I tried to implement selection sort in python
LeetCode I tried to summarize the simple ones
I want to display the progress in Python!
[Introduction to Docker] I tried to summarize various Docker knowledge obtained by studying (Windows / Python)
[First data science ⑤] I tried to help my friend find the first property by data analysis.
I tried to summarize the new coronavirus infected people in Ichikawa City, Chiba Prefecture
[First data science ⑥] I tried to visualize the market price of restaurants in Tokyo
I used Python to find out about the role choices of the 51 "Yachts" in the world.
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
I tried to summarize how to use matplotlib of python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to implement a pseudo pachislot in Python
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
[Python] I tried to graph the top 10 eyeshadow rankings
I want to write in Python! (3) Utilize the mock
I tried to solve the problem with Python Vol.1
I want to use the R dataset in python
Python OpenCV tried to display the image in text.
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
I made a class to get the analysis result by MeCab in ndarray with python