python绘制折线图、直方图

  • 安装 matplotlib
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

折线图

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['SimHei']    # 处理中文无法正常显示的问题
plt.rcParams['axes.unicode_minus'] = False    #负号显示

# 折线图
# 去掉上方和右方的边框
# fig, ax = plt.subplots()
# ax.spines['top'].set_visible(False)
# ax.spines['right'].set_visible(False)

# 设置图例中的字体和大小
font = {'family': 'serif',
        'serif': 'Times New Roman',
        'weight': 'normal',
        'size': 15}
plt.rc('font', **font)

# 数据
x = [i * 0.1 for i in range(1, 10)]  # 点的横坐标x
y1 = [1234, 2345, 2345, 4656, 4366, 5356, 5367, 6356, 6654] # 线1的纵坐标
y2 = [234, 345, 456, 678, 789, 912, 945, 1111, 1234]  # 线2的纵坐标
y3 = [150, 224, 356, 451, 558, 490, 589, 723, 915]  # 线3的纵坐标
y4 = [45, 19, 25, 36, 83, 78, 34, 16, 24]  # 线4的纵坐标


# 点的形状	. 点	, 像素点	o 圆点	v 下三角点	^ 上三角点	< 左三角点
#			> 右三角点	1 下三叉点	 2 上三叉点	 3 左三叉点	 4 右三叉点
#			s 正方点	p 五角点	* 星形点	h 六边形点1	H 六边形点2
#			+ 加号点	x 乘号点	D 实心菱形点	d 瘦菱形点	_ 横线点
# 线条类型	- 实线	-- 虚线	-. 虚点线	: 点线
#绘画出四条线,x 横坐标,y1 纵坐标,'s-' 点的形状,color 线条颜色,label 线条标签
plt.plot(x, y1, 's-', color='black', label='y1')
plt.plot(x, y2, '^-', color='black', label="y2")
plt.plot(x, y3, 'v-', color='black', label="y3")
plt.plot(x, y4, 'o-', color='black', label="y4")

# 设置横坐标
plt.xticks([x * 0.1 for x in range(1, 10)], fontsize=12)
plt.xlabel("X", fontsize=15)  # 横坐标名字

# 设置纵坐标
plt.yscale('log')  # 设置纵坐标的缩放,写成m³格式
plt.yticks([1,10, 100, 1000, 10000],fontsize=12)
plt.ylabel("Y", fontsize=15)  # 纵坐标名字


# loc 位置	best 0	upper right 1	upper left 2	lower left 3
# 			lower right 4	right 5	center left 6	center right 6
#			lower center 8	upper center 9	center 10
# 显示每条线的标签,loc 标签位置
plt.legend(loc="lower right")
plt.title("lineChart")   # 图标题
# plt.savefig("demo.pdf", dpi=300)   #保存图片
plt.show()    # 显示图像

折线图效果

直方图

import matplotlib.pyplot as plt
import numpy as np

# 折线图
# 去掉上方和右方的边框
# fig, ax = plt.subplots()
# ax.spines['top'].set_visible(False)
# ax.spines['right'].set_visible(False)

# 设置图例中的字体和大小
font = {'family': 'serif',
        'serif': 'Times New Roman',
        'weight': 'normal',
        'size': 15}
plt.rc('font', **font)

# 横坐标标签
labels=  ['0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9']
# 数据
y1 = [1234, 2345, 2345, 4656, 4366, 5356, 5367, 6356, 6654] # 柱体1的纵坐标
y2 = [234, 345, 456, 678, 789, 912, 945, 1111, 1234]  # 柱体2的纵坐标
y3 = [150, 224, 356, 451, 558, 490, 589, 723, 915]  # 柱体3的纵坐标
y4 = [45, 19, 25, 36, 83, 78, 34, 16, 24]  # 柱体4的纵坐标


# 横坐标位置(相对位置)
x = np.arange(len(labels))
# 柱体宽度
width = 0.2


#绘制柱体 x-width*1.5 柱体相对中央的偏移 y1 纵坐标 width 柱体宽度 color 柱体颜色 edgecolor 柱体边缘颜色 hatch 填充线条类型 label 柱体标签
# hatch 填充线条类型: {'/', '', '|', '-', '+', 'x', 'o', 'O', '.', '*'} 连续多个表示密度
plt.bar(x - width * 1.5, y1, width, color='white', edgecolor='black', hatch='|||', label='feat')
plt.bar(x - width * 0.5, y2, width, color='white', edgecolor='black', hatch='\\\\\\\\\\', label='key')
plt.bar(x + width * 0.5, y3, width, color='white', edgecolor='black', hatch='/', label='soc')
plt.bar(x + width * 1.5, y4, width, color='white', edgecolor='black', hatch='---', label='ksg')

# 显示x坐标轴的标签,即tick_label,调整位置,使其落在两个直方图中间位置
plt.xticks(x, labels)
plt.xlabel("X", fontsize=15)  # 横坐标名字

plt.yscale('log')  # 设置纵坐标的缩放,写成m³格式
plt.yticks([1,10, 100, 1000, 10000], fontsize=12)
plt.ylabel('Y')

# loc 位置	best 0	upper right 1	upper left 2	lower left 3
# 			lower right 4	right 5	center left 6	center right 6
#			lower center 8	upper center 9	center 10
# 显示每条线的标签,loc 标签位置
plt.legend(loc="upper right")
plt.title("Histogram")   # 图标题
# plt.savefig("demo.pdf", dpi=300)  # 保存图片
plt.show()   # 显示图像

直方图效果

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值