神码ai火车头标题伪原创【php源码】

大家好,小编来为大家解答以下问题,基于python的图表生成系统,python表格生成图表,今天让我们一起来看看吧!

火车头采集ai伪原创插件截图:

本文参考文章:https://www.cnblogs.com/magle/p/7389240.html

Python之Matplotlib

写在前面:因为本人是属于纯小白,并且是想从Matplotlib开始入门,所以文中不止单单的涉及到Matplotlib的相关知识,只要有不会的我都会去查一下然后补充进去,所以内容稍显凌乱,但是方便新手理解各个代码段。

另外,来回切换网页干扰太多,所以本文汉字较少,解释说明大部分直接写在了代码块中,方便复制代码运行的同时可以看解释说明,避免来回切换屏幕。

导入相关模块

import matplotlib.pyplot as plt#Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。

import pandas as pd# pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

import numpy as np#numpy是Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

基本图表

散点图:scatter

N = 1000

x = np.random.randn(N)#randn函数返回一个或一组样本,具有标准正态分布。

y = np.random.randn(N)

plt.scatter(x,y)

plt.show()#如果不使用plo.show()图表是显示不出来的,因为可能你要对图表进行多种的描述,所以通过显式的调用show()可以避免不必要的错误。

#scatter的函数签名如下:

#scatter(x,y,s)=None,c=None,marker=None,cmap=None,norm=None,vmin=None,vmax=None,alpha=None,linewidths=None,verts=None,edgecolors=None,hold=None,data=None,**kwargs

#x,y:形如shape (n,)的数组 #表示的是大小为(n,)的数组,也就是我们即将绘制散点图的数据点

#s:点的大小,默认20,参数可选

#c:点的取色序列,默认蓝色‘b’,但是c不可以是一个单独的RGB数字,也不可以是一个RGBA的序列。可以是他们的2维数组(只有一行)

#marker:点的形状,默认是圆

#cmap:Colormap实体或者是一个colormap的名字

#norm:Normalize实体来将数据亮度转化到0-1之间,如果没有申明,就是默认为colors.Normalize。

#vmin,vmax:实数,当norm存在的时候忽略。用来进行亮度数据的归一化。

#alpha:点的透明度, 实数,0-1之间

#linewidths:也就是标记点的长度。

#edgecolors:边缘颜色

运行结果:

c347b2ef3005f76a986b4e6b77494fd4.png

plt.scatter(x,y,c='rykgm',s=100,marker='>')#这里运行报错,应该是c的RGB或者RGBA问题,后续查资料修正

plt.show()

运行结果:(出错,后续查询改正)

plt.scatter(x,y,alpha=0.5)

plt.show()

运行结果:

d0f2caad3449292e2cb5358129b75bfe.png

plt.scatter(x,y,edgecolors='r')

plt.show()

701d4960bd4e10855247e3b126eb4a04.png

柱状图:bar

data = [5, 20, 15, 25, 10]

plt.bar(range(len(data)),data)#len()返回字符串、列表、字典、元组等长度,range()函数它能返回一系列连续增加的整数

plt.show()

#bar的函数签名:

#bar(left, height, width=0.8, bottom=None, **kwargs)

#left,height,width,bottom这四个参数确定了柱体的位置和大小,具体定位:

#(left-width/2,bottom)为左下角位置

#(left+width/2,bottom+height)为右上角位置

运行结果:

75b3fe66251c2eaa68b91ff005aa33b7.png

plt.bar([0.3,1.7,4,6,7], data, width=0.6, bottom=[10,0,5,0,5])

plt.show()

运行结果:

9538fd82989d8c8ba9edd26ff746678f.png

#对于柱状图,还可以设置以下参数:

#颜色: color

#描边: 边缘颜色:edgecolor(ec);边缘样式:linestyle(ls);边缘粗细:linewidth(lw)

#填充: hatch: 可取值为: /,\,|,-,+,×,o,O,.,*

#位置标志: tick_label

labels = ['Tom', 'Dick', 'Harry', 'Slim', 'Jim']

plt.bar(range(len(data)), data, color='rgb', ec='r', ls='--', lw=2, hatch='o', tick_label=labels)#rgb:颜色 比如white:整数值(255,255,255),小数值(1,1,1)

plt.show()

运行结果:

59cf8878661f5c8278914c5ab9bff20e.png

#堆叠柱状图

#通过bottom参数,可以轻松实现堆叠柱状图

size = 5

x = np.arange(size)

a = np.random.random(size)#random() 方法返回随机生成的一个实数,它在[0,1)范围内。

b = np.random.random(size)

plt.bar(x, a, label='a')

plt.bar(x, b, bottom=a, label='b')

plt.legend()#plt.legend()函数主要的作用就是给图加上图例,plt.legend([x,y,z])里面的参数使用的是list的的形式将图表的的名称喂给这和函数。

plt.show()

运行结果:

cd8f0ee231372eab05507931fa2fc395.png

补充上面代码出现的legend()函数:

文章参考链接:https://www.cnblogs.com/czz0508/p/10451944.html

#补充:legend()函数,函数功能:表示不同图形的文本标签图案。调用签名:plt.legend(loc="lower left")。loc:图例在图中的地理位置。

#legend()函数代码实现:(左下角,plot figure)文章参考链接:https://www.cnblogs.com/czz0508/p/10451944.html

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0.05, 10, 1000)

y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")

plt.legend(loc="lower left")

plt.show()

运行结果:

95f8ab7e8dc263ce29ce04800a96dc74.png

#并列柱状图

#并列柱状图需要通过left属性添加偏移量来实现

size = 5

x = np.arange(size)

a = np.random.random(size)#random()random模块用于生成随机数,里面有许多函数用法https://blog.csdn.net/kancy110/article/details/69665164/

b = np.random.random(size)

total_width, n = 0.8, 2

width = total_width / n #宽=总长度/2

x = x - (total_width - width) / 2

plt.bar(x, a, width=width, label = 'a')

plt.bar(x + width, b, width=width, label = 'b')

plt.legend()

plt.show()

运行结果:

356519b516c24f2fd9b6eb67b6485ff1.png

补充:Python 中的range,以及numpy包中的arange函数

链接:https://blog.csdn.net/qianwenhong/article/details/41414809

#补充:Python 中的range,以及numpy包中的arange函数

#链接:https://blog.csdn.net/qianwenhong/article/details/41414809

#range()函数:

#函数说明: range(start, stop[, step]) -> range object,根据start与stop指定的范围以及step设定的步长,生成一个序列。

#参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);

# end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

# scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

# 函数返回的是一个range object

#代码实例:

#>>> range(0,5) #生成一个range object,而不是[0,1,2,3,4]

#range(0, 5)

#>>> c = [i for i in range(0,5)] #从0 开始到4,不包括5,默认的间隔为1

#>>> c

#[0, 1, 2, 3, 4]

#>>> c = [i for i in range(0,5,2)] #间隔设为2

#>>> c

#[0, 2, 4]

#若需要生成[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]

#>>> range(0,1,0.1) #range中的setp 不能使float

#Traceback (most recent call last):

# File "", line 1, in

#range(0,1,0.1)

#TypeError: 'float' object cannot be interpreted as an integer

#arrange()函数:

#函数说明:arange([start,] stop[, step,], dtype=None)根据start与stop指定的范围以及step设定的步长,生成一个 ndarray。 dtype : dtype

# The type of the output array. If `dtype` is not given, infer the data

# type from the other input arguments.

#代码实例:

# >>> np.arange(3)

# array([0, 1, 2])

# >>> np.arange(3.0)

# array([ 0., 1., 2.])

# >>> np.arange(3,7)

# array([3, 4, 5, 6])

# >>> np.arange(3,7,2)

# array([3, 5])

#>>> arange(0,1,0.1)

#array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值