日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python编程口诀_科学网—Python编程技巧汇总 - 高关胤的博文

發布時間:2023/12/2 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python编程口诀_科学网—Python编程技巧汇总 - 高关胤的博文 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

正在學習python編程,把一些小技巧記錄下來備查

======================計算技巧==========================

正常的條件語句如下if a>b:c=aelse:c=b

可以寫為以下簡潔的語句a=100b=200c=a if a>b else bprint(c)

======================作圖==========================

python中可以使用numpy的array來高效處理數組

下面主要列舉matplotlib的基本使用方法,以供查找

first plot

#first plot with matplotlib

import matplotlib.pyplot as plt

plt.plot([1,3,2,4])

plt.show()

in order to avoid pollution of global namespace, it is strongly recommended to never import like:

from import *

simple plot

import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(0.0,6.0,0.1)

plt.plot(x, [xi**2 for xi in x],label = 'First',linewidth = 4,color = 'black')

plt.plot(x, [xi**2+2 for xi in x],label = 'second',color = 'red')

plt.plot(x, [xi**2+5 for xi in x],label = 'third')

plt.axis([0,7,-1,50])

plt.xlabel(r"$\alpha$",fontsize=20)

plt.ylabel(r'y')

plt.title('simple plot')

plt.legend(loc = 'upper left')

plt.grid(True)

plt.savefig('simple plot.pdf',dpi = 200)

print mpl.rcParams['figure.figsize'] #return 8.0,6.0

print mpl.rcParams['savefig.dpi'] #default to 100 the size of the pic will be 800*600

#print mpl.rcParams['interactive']

plt.show()

Python-3

Decorate plot with styles and types

import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(0.0,6.0,0.1)

plt.plot(x, [xi**2 for xi in x],label = 'First',linewidth = 4,color = 'black') #using color string to specify color

plt.plot(x, [xi**2+2 for xi in x],'r',label = 'second') #using abbreviation to specify color

plt.plot(x, [xi**2+5 for xi in x],color = (1,0,1,1),label = 'Third') #using color tuple to specify color

plt.plot(x, [xi**2+9 for xi in x],color = '#BCD2EE',label = 'Fourth') #using hex string to specify color

plt.xticks(np.arange(0.0,6.0,2.5))

plt.xlabel(r"$\alpha$",fontsize=20)

plt.ylabel(r'y')

plt.title('simple plot')

plt.legend(loc = 'upper left')

plt.grid(True)

plt.savefig('simple plot.pdf',dpi = 200)

print mpl.rcParams['figure.figsize'] #return 8.0,6.0

print mpl.rcParams['savefig.dpi'] #default to 100 the size of the pic will be 800*600

#print mpl.rcParams['interactive']

plt.show()

image

types of graph

image

Bars

import matplotlib.pyplot as plt

import numpy as np

dict = {'A': 40, 'B': 70, 'C': 30, 'D': 85}

for i, key in enumerate(dict): plt.bar(i, dict[key]);

plt.xticks(np.arange(len(dict))+0.4, dict.keys());

plt.yticks(dict.values());

plt.grid(True)

plt.show()

image_1

Pies

import matplotlib.pyplot as plt

plt.figure(figsize=(10,10));

x = [4, 9, 21, 55, 30, 18]

labels = ['Swiss', 'Austria', 'Spain', 'Italy', 'France',

'Benelux']

explode = [0.2, 0.1, 0, 0, 0.1, 0]

plt.pie(x, labels=labels, explode=explode, autopct='%1.1f%%');

plt.show()

image_2

Scatter

import matplotlib.pyplot as plt

import numpy as np

x = np.random.randn(12,20)

y = np.random.randn(12,20)

mark = ['s','o','^','v','>','<','d','p','h','8','+','*']

for i in range(0,12):

plt.scatter(x[i],y[i],marker = mark[i],color =(np.random.rand(1,3)),s=50,label = str(i+1))

plt.legend()

plt.show()

轉載本文請聯系原作者獲取授權,同時請注明本文來自高關胤科學網博客。

鏈接地址:http://blog.sciencenet.cn/blog-64087-968950.html

上一篇:[轉載]“玩伴女郎”誤入學術圈

下一篇:APS模板包RevTex的安裝方法

總結

以上是生活随笔為你收集整理的python编程口诀_科学网—Python编程技巧汇总 - 高关胤的博文的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。