Python从门到精通(九):matploblib图形库

Matplotlib 是一个数字绘图库,可以绘制多种图形
在这里插入图片描述

绘制曲线图

import matplotlib.pyplot as plt

class PltDemo:

    def __init__(self) -> None:
        # 数据
        self.squares = [1, 4, 9, 16, 25]

    def init_ax(self):
        # flg:由生成的一系列绘图构建成的整体个图形
        # ax:表示图形中的绘图,使用这个变量来定义和绘制图形
        fig, ax = plt.subplots()
        return ax
    
    def ax_setting(self, ax):
         # 绘图
        ax.plot(self.squares)
        
    def showGraphy(self):
        plt.show()
    

if __name__ == "__main__":
    pltDemo = PltDemo()
    ax = pltDemo.init_ax()
    pltDemo.ax_setting(ax)
    pltDemo.showGraphy()

在这里插入图片描述

设置样式

个性化样式

 def ax_setting(self, ax):
         # 绘图
        ax.set_title("title", fontsize=24)
        ax.set_xlabel("xlabe", fontsize=14)
        ax.set_ylabel("ylabel", fontsize=14)
        ax.tick_params(labelsize=14) # 刻度样式
        ax.ticklabel_format(style='plain')

        # 设置初始值
        ax.plot(self.input_value, self.squares, linewidth=3) #图形的线条粗细

内置样式

    def init_ax(self):
        print(plt.style.available)
        plt.style.use('seaborn')
        fig, ax = plt.subplots()
        return ax

设置散列点

        ax.scatter(2, 4, s=200) #设置某个点的大小
        ax.scatter(self.input_value, self.squares, s=100)
        ax.scatter(self.input_value, self.squares,color='red', s=100)

设置坐标范围

# 设置每个坐标轴的取值范围,x=0~100, y=0~1
ax.axis([0, 50, 0, 50])

使用plotly.express在浏览器中显示图形

Plotly Express是Matplotlib的一个子集

import plotly.express as px

poss_results = range(1, 7)
frequencies = [185, 164, 171, 168, 145, 167]
title = "Results of Rolling One D6 1,000 Times"
labels = {'x': 'Result', 'y': 'Frequency of Result'}
fig = px.bar(x=poss_results, y=frequencies, title=title, labels=labels)
fig.show()

在这里插入图片描述

相关推荐

  1. Python精通():numpy科学计算

    2023-12-11 17:26:02       60 阅读
  2. Python入门精通秘籍

    2023-12-11 17:26:02       43 阅读
  3. Python入门精通秘籍十

    2023-12-11 17:26:02       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 17:26:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 17:26:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 17:26:02       87 阅读
  4. Python语言-面向对象

    2023-12-11 17:26:02       96 阅读

热门阅读

  1. 鸿蒙(HarmonyOS)应用开发——保存应用数据

    2023-12-11 17:26:02       68 阅读
  2. vue简单的图片预览

    2023-12-11 17:26:02       58 阅读
  3. Selenium Web网页自动化测试

    2023-12-11 17:26:02       63 阅读
  4. [数据结构]TreeSet的条件筛选

    2023-12-11 17:26:02       57 阅读
  5. 算法基础十一

    2023-12-11 17:26:02       43 阅读
  6. 十二.镜头知识之镜头分辨率(解析力)

    2023-12-11 17:26:02       53 阅读
  7. ElasticSearch

    2023-12-11 17:26:02       59 阅读