라벨이 Python인 게시물 표시

Python, Contourf, Contour and Filled contour with lines and labels

Below is a simple example. v = numpy.linspace(0.0, 1.0, 100, endpoint=True) contourf(xx,yy,zz,v) colorbar() cs = contour(xx,yy,zz,v, colors='white') clabel(cs, inline=1, fontsize=10) show()

[Python] matplotlib pyplot color map and list of name

이미지
[ref. https://matplotlib.org/3.1.0/gallery/color/named_colors.html ]

How to control decimal point of float format in Python (파이썬 소수점 표시하는 방법)

Summary of decimal point control of float format in Python  >>> 125650429603636838/(2**53)   13.949999999999999   >>> 234042163/(2**24)   13.949999988079071   >>> a=13.946   >>> print(a)   13.946   >>> print("%.2f" % a)   13.95   >>> round(a,2)   13.949999999999999   >>> print("%.2f" % round(a,2))   13.95   >>> print("{0:.2f}".format(a))   13.95   >>> print("{0:.2f}".format(round(a,2)))   13.95   >>> print("{0:.15f}".format(round(a,2)))   13.949999999999999 Thanks, JF ref.  https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points .

Python Release Memory from Dataframe, Array, etc.

Use del as below. df = pandas.read_csv['filename.txt'] del df Thanks, JF

Previous Command in Python IDLE Editor

Up arrow key does not work in Python IDLE Editor to pull out the previous command. Alt + p : Previous commend Alt + n : Next commend Let's try these! Thanks, JF

Read text file and plot by using Pandas and matplotlib.pyplot in Python

Here is a sample code. import pandas as pd import matplotlib.pyplot as plt filename = 'a.txt' data = pd.read_csv(filename, sep='\t', header=None) plt.plot(data[0],data[1]) plt.xlabel('Raman shift (cm-1)', size=14) plt.ylabel('Intensity', size=14) plt.xlim(1000,3000) plt.savefig('Raman.png', transparent=True) plt.show() This is the link on details of read_csv of pandas. ( https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html ) This is the link on details of matplotlib.pyplot. ( https://matplotlib.org/api/_as_gen/matplotlib.pyplot.html ) Thanks, JF

Parallel execution of python code using mpirun

If you give a command as below: mpirun -np 12 python pythoncode.py , same 12 jobs (python pythoncode.py) will be executed. Thanks, JF

Random number, array, vector, matrix, tensor generations using python, numpy, and random

I am going to use only one function for all of these. I will generate random numbers between 0 and 1. import numpy as np number = numpy.random.uniform(0, 1) array = numpy.random.uniform(0,1,n) # n is array size matrix = numpy.random.uniform(0,1,(n,m)) # n x m matrix Thanks, JF

Install Python to User Account in Linux

1. Download PythonO.O.tgz 2. Unzip >> tar -xvzf PythonO.O.tgz 3. cd PythonO.O 4. ./confiure --prefix=$HOME/python27/           (example) 5. make 6. make install 7. Setup environment variables Thanks, JF

os.system and exitcode in Python

If you want to execute Linux command in Python code, use os.system. For example, import os os.system('ls') Above commands will show you a file list in your directory. When it comes to exitcode, os.system('command') will give (return) you an exit value, and value 0 means success. Thanks, JF

Finding minimum value and its index in list of Python

minimum value -> min(list) index of minimum value in list -> list.index(min(list)) Thanks, JF

Using argument in Python

If you want to use argument in Python, do this! sys.argv[1] : first argument sys.argv[2] : second argument sys.argv[n] : nth argument For example, num = int(sys.argv[1]) : store first argument as an integer type Thanks, JF

Installing python modules using pip in Windows

Today I am going to post about installing python modules such as setuptools, pytz, and six using pip ! If the environment variable of 'C:\python27' are not set, go to the folder of C:\python27. If you are using powershell, it is like below PS C:\Python27> For example, install six module PS C:\Python27> .\python.exe -m pip install six If you want to upgrade this module PS C:\Python27> .\python.exe -m pip install --upgrade six Thanks !

Line break and line continuation in Python

Back slash (\) could be used for line break and line continuation in Python! a = 12 + 13 + 14 + 15 is same as a = 12 + 13 \       + 14 + 15 Thanks, Have fun!

How to use log function in Python ?

이미지
Using math module, like this! ------------------------------ import math print math.log(arg) ------------------------------ Above command will return the natural logarithm of x ( to base e ). Here is an example with some application to the other important cases!

How do I generate random numbers in Python !

이미지
Using random module, you can generate random number from 0 to 1 in float format. import random rannum = random.random() Here is an example!

How to make empty matrix with zeros without numpy in Python

이미지
We can generate empty matrix without numpy module in Python! Just do this! mat = [[0 for i in range(5)] for i in range(5)] Here is an example!

How to make a empty matrix with zeros in Python using Numpy

이미지
It is quite simple! ------------------------------- import numpy numpy.zeros([rows,cols]) ------------------------------- Here is an example!

How to install 'Scipy' using Anaconda of Python in Windows environment

이미지
First, visit the website of 'scipy.org' And then go to Install! (Left-Top direction) In this post, we will use the Anaconda server! click Anaconda link in the middle of page! From this page, you can download the Scipy package by clicking the button Finally, just double click the .exe file and follow the instructions after downloading the installer! Thanks! Have Fun!

String X or Y ticks label in plotting graph of Python using matplotlib

이미지
In usual case, we are plotting graphs about integer or float x or y values, but sometimes you need to plot some graphs using string x or y values such as dates, categories. In this case, you can easily draw plot with string values by using xticks or yticks module. In this post, I will show you an example using x string values. Just for integer plot ----------------------------------------------------------------------------- from pylab import * x = [ 1, 2, 3 ] y = [ 2, 5, 10 ] plot(x, y) show() ----------------------------------------------------------------------------- The results is like below Now, for string x values --------------------------------------------------------------------- from pylab import * x = [ 'Mon' , 'Tue' , 'Wed' ] y = [ 2, 5, 10 ] plot( range ( len (y)), y) xticks( range ( len (y)), x, size= 'small' ) show() --------------------------------------------------------------------