首页  

matplot 基本用法     所属分类 python 浏览量 36
折线图 散点图 柱状图 饼图 直方图

折线图
x:X轴的数据
y:Y轴的数据
label:线条的标签
color:线条的颜色
linestyle:线条的样式
marker:标记点的样式

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 17, 8]
# linestyle=''  不显示线条  变散点图
plt.plot(x, y, label='Data', color='blue', linestyle='-', marker='o')
plt.title('Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()

plt.plot(x, y, '-ok');


plt.plot(x, y, '-p', color='gray',
         markersize=15, linewidth=4,
         markerfacecolor='white',
         markeredgecolor='gray',
         markeredgewidth=2)


x = np.linspace(0, 10, 1000)
plt.plot(x, np.sin(x - 0), color='blue')        # 通过颜色名称指定
plt.plot(x, np.sin(x - 1), color='g')           # 通过颜色简写名称指定(rgbcmyk)
plt.plot(x, np.sin(x - 2), color='0.75')        # 介于0-1之间的灰阶值
plt.plot(x, np.sin(x - 3), color='#FFDD44')     # 16进制的RRGGBB值
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB元组的颜色值,每个值介于0-1

plt.plot(x, x + 4, linestyle='-')  # 实线
plt.plot(x, x + 5, linestyle='--') # 虚线
plt.plot(x, x + 6, linestyle='-.') # 长短点虚线
plt.plot(x, x + 7, linestyle=':');  # 点线


plt.plot(x, x + 0, '-g')  # 绿色实线
plt.plot(x, x + 1, '--c') # 天青色虚线
plt.plot(x, x + 2, '-.k') # 黑色长短点虚线
plt.plot(x, x + 3, ':r');  # 红色点线

散点图 s:点的大小 c:点的颜色 marker:标记点的样式 plt.scatter(x, y, label='Data', s=100, c='red', marker='o') plt.scatter(x, y, label='Data', s=1000, c='red', marker='o')
柱状图 x:X轴的数据 height:柱子的高度 width:柱子的宽度 align:柱子的对齐方式 color:柱子的颜色 import matplotlib.pyplot as plt categories = ['Category A', 'Category B', 'Category C', 'Category D'] values = [25, 32, 12, 19] plt.bar(categories, values, width=0.5, align='center', color='green') plt.title('Bar Plot') plt.xlabel('Categories') plt.ylabel('Values') plt.show() plt.barh(categories, values, height=0.5, align='center', color='green') barh 水平条 , 配合 height
饼图 labels:扇形块的标签 sizes:各扇形块的大小 colors:扇形块的颜色 explode:突出显示某些扇形块 autopct:扇形块上的百分比标签 labels = ['Category A', 'Category B', 'Category C', 'Category D'] sizes = [25, 32, 12, 19] colors = ['red', 'blue', 'green', 'yellow'] explode = (0.1, 0, 0, 0) # 突出显示第一个扇形块 plt.pie(sizes, labels=labels, colors=colors, explode=explode, autopct='%1.1f%%', shadow=True) plt.title('Pie Chart') plt.show()
直方图 import matplotlib.pyplot as plt import numpy as np # 创建一个正态分布的随机数据集 data = np.random.randn(1000) # 使用 matplotlib 的 hist 函数绘制直方图 # bins 参数指定了直方图的区间数量 plt.hist(data, bins=30, edgecolor='black') # 添加标题和坐标轴标签 plt.title('Histogram of Random Data') plt.xlabel('Value') plt.ylabel('Frequency') # 显示图表 plt.show()

上一篇     下一篇
Python统计学极简入门笔记 03 数据分布

jupyter 使用 技巧

matplotlib 技巧

pandas matplot 读取日线数据,滚动计算bias250 添加阈值线

pandas matplot 画图 超出阈值 变颜色

沪深300指数 bias250 z20 z250 分析