For advanced version, go to here https://qiita.com/coffiego/items/dfabde5f8588723b32d6
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!
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)
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
Because it is a basic edition
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.
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.
--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
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. Please see this official document for a list of many colors.
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. Click here for details
The sample looks like this. You can add a legend with plt.legend ().
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.
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.
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})
#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})
There are many marker types listed in the Official Documentation.
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
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'
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