1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python画图 做表

python画图 做表

时间:2021-10-01 22:20:18

相关推荐

python画图 做表

生成随机数:

np.random.seed(n) 控制两次随机生成的数相不相同

np.random.seed(n)函数用于生成指定随机数。

把seed()中的参数比喻成“堆”;eg. seed(5):表示第5堆种子。

只调用一次seed(),两次的产生随机数不同

import numpy as npnp.random.seed(1)L1 = np.random.randn(3, 3)L2 = np.random.randn(3, 3)print(L1)print(L2)

np.random.seed(1)L1 = np.random.randn(3, 3)np.random.seed(1)L2 = np.random.randn(3, 3)print(L1)print(L2)

numpy.random.randn() 控制生成几维 什么分布的数据

/u012149181/article/details/78913167

x_ticks = np.linspace(-5, 4, 10) # 产生区间在-5至4间的10个均匀数值x=np.random.randint(0,100,100)#生成【0-100】之间的100个数据,即 数据集##### 画图--曲线图(x,y)plt.plot(x,y)plt.plot(x,y)plt.show()/p/258106097##### 画图--直方图(把数据按照所属的范围归类 画直方图)plt.hist(a,bins= 20)设置区间

#一个参数 默认起点0,步长为1 输出:[0 1 2]

a = np.arange(3)

#两个参数 默认步长为1 输出[3 4 5 6 7 8]

a = np.arange(3,9)

#三个参数 起点为0,终点为3,步长为0.1 输出[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9]

a = np.arange(0, 3, 0.1)

把数据按区间分类

plt.hist(x,bins,color=‘fuchsia’,alpha=0.5)#alpha设置透明度,0为完全透明

a = np.random.rand(100)

plt.hist(a,bins= 20) #把数值进行20等分

#在构建直方图之前,我们需要先定义好bin(把值分成多少等份),也就是说我们需要先把连续值划分成不同等份,然后计算每一份里面数据的数量

fig, axes = plt.subplots(2, 2) # 设置生成图片的大小 此处是一个2*2的图

plt.ylim(0,15) #限制y轴高度:0→15

plt.legend() #使图片的信息设置生效

plt.xlabel(“x-axis”) #设置x轴的标签

plt.ylabel(“y-axis”) #设置y轴的标签

plt.xlim(0,100)#设置x轴分布范围

#运行

#当前shell

export MPLBACKEND=Agg

#只为当前脚本test.py

MPLBACKEND=Agg python test.py

import matplotlib

matplotlib.use(‘Agg’)

/p/111108841若要生成html格式文本

def map_histogram(data):plt.title("Distribution of running results")plt.xlabel("run results")plt.ylabel("frequency")plt.hist(data, bins=100, normed=True)figfile = BytesIO()plt.savefig(figfile, format='png')figfile.seek(0)figdata_png = base64.b64encode(figfile.getvalue()) # to base64figdata_str = str(figdata_png)fig = '<img src=\"data:image/png;base64,{}\"/>'.format(figdata_str)return fig

##### 统计表格数据属性构建DataFrame--作用有两个:①构建成dataframe之后得到表格格式 就像excel一样 可以对数据进行处理 得到各种属性 ②对得到的数据构建成表格 方便进行展示

import pandas as pd

df = pd.DataFrame([‘value1’:[1, 2, 3], ‘value2’:[4, 5, np.nan], ‘value3’:[7, 8, 9]], index=[‘A’, ‘B’, ‘C’], columns=[‘c1’, ‘c2’, ‘c3’])

data0=[1,2,3]

df = pd.DataFrame(data0) #此时的构建只有一列 而且没有行列属性 index columns

把数据按行相加和按列相加

print(df)

‘’’

c1 c2 c3

A 1 2 3.0

B 4 5 NaN

C 7 8 9.0

‘’’

#按照每一列相加,返回或axis=0

print(df.sum())

‘’’

c1 12.0

c2 15.0

c3 12.0

dtype: float64

‘’’

#指定axis, 按照每一行相加

print(df.sum(axis=1))

‘’’

A 6.0

B 9.0

C 24.0

dtype: float64

/wenqiangit/p/11252784.html取第二行的数据

df.ix[1] #取第2行数据

/tanlangqie/article/details/78656588?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&utm_relevant_index=2取其discribeprint(df.describe())DataFrame中常见的其他方法:得到的是一个前面有索引的series结构数据 需要ix[1]得到具体数

df.count() #非空元素计算

df.min() #最小值

df.max() #最大值

df.idxmin() #最小值的位置,类似于R中的which.min函数

df.idxmax() #最大值的位置,类似于R中的which.max函数

df.quantile(0.1) #10%分位数

df.sum() #求和

df.mean() #均值

df.median() #中位数

df.mode() #众数

df.var() #方差

df.std() #标准差

df.mad() #平均绝对偏差

df.skew() #偏度

df.kurt() #峰度

df.describe() #一次性输出多个描述性统计指标

(df == 0).astype(int).sum().ix[0]判断df中每一个数值 为0则为true 通过.astype(int) 把其转为1 通过sun求所有1的和 通过ix取到series中的值生成html文本

def html(table, fig, list):html_string = '''<html><head><link rel="stylesheet" href="/bootstrap/3.3.1/css/bootstrap.min.css"><style>body{ margin:0 100; background:whitesmoke; }</style></head><body><h1>Tensor Analysis</h1><h2>Section 1: Results of various indicators</h2>''' + table + '''<h2>Section 2: Distribution of running results</h2><iframe width="1000" height="550" frameborder="0" seamless="seamless" scrolling="no" \''' + fig + ''' .embed?width=800&height=550"></iframe><h2>Section 3: Value</h2>''' + list + '''</body></html>'''return html_stringdata_html = html(data_dis, data_fig, data_list)f = open('{}/report.html'.format(args.output), 'w')f.write(data_html)f.close()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。