Table of Contents
1. Basic use
1.1. Line style & color
%%PRE_BLOCK_7%%
Running results
3.2、频率直方图
3.3、直方图
4、散点图
5、饼图
6、箱线图
7、雷达图
Home Backend Development Python Tutorial What are the steps and methods to draw charts using the Python Matplotlib library?

What are the steps and methods to draw charts using the Python Matplotlib library?

May 08, 2023 pm 05:16 PM
python matplotlib

    Chinese font settings:

    # 字体设置
    plt.rcParams['font.sans-serif'] = ["SimHei"]
    plt.rcParams["axes.unicode_minus"] = False

    1. Basic use

    Matplotlib: It is a Python 2D drawing Library, through Matplotlib, developers can generate line charts, histograms, bar charts, pie charts, scatter charts, etc. with just a few lines of code.
    plot is a drawing function, its parameters: plot([x],y,[fmt],data=None,**kwargs)

    1.1. Line style & color

    (1) Dotted line form

    What are the steps and methods to draw charts using the Python Matplotlib library?

    ##(2) Line color

    What are the steps and methods to draw charts using the Python Matplotlib library?

    import matplotlib.pyplot as plt
    import numpy as np
    # 原始线图
    plt.plot(range(10),[np.random.randint(0,10) for x in range(10)])
    # 点线图
    plt.plot(range(10),[np.random.randint(0,10) for x in range(10)],"*")
    # 线条颜色
    plt.plot([1,2,3,4,5],[1,2,3,4,5],'r') #将颜色线条设置成红色

    Run results:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    1.2, Axis & Title

    • 1, Settings Figure title: plt.title

    • 2. Set axis title: plt.xlabel & plt.ylabel - Title name

    • 3. Set axis Scale: plt.xticks & plt.yticks - Scale length, scale title

    Example:

    x = range(10)
    y = [np.random.randint(0,10) for x in range(10)]
    plt.plot(x,y,linewidth=10,color='red')
    # 设置图标题
    plt.title("sin函数")
    # 设置轴标题
    plt.xlabel("x轴")
    plt.ylabel("y轴")
    # 设置轴刻度
    plt.xticks(range(10),["第%d天"%x for x in range(1,10)])
    plt.yticks(range(10),["第%d天"%x for x in range(1,10)])
    # 加载字体
    plt.rcParams['font.sans-serif'] = ["SimHei"]
    plt.rcParams["axes.unicode_minus"] = False

    Run result:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    1.3. Marker settings

    marker:关键点重点标记

    Example:

    x = range(10)
    y = [np.random.randint(0,10) for x in range(10)]
    plt.plot(x,y,linewidth=10,color='red')
    # 重点标记
    plt.plot(x,y,marker="o",markerfacecolor='k',markersize=10)

    Run result:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    1.4. Comment text

    annotate:注释文本

    Example:

    x = range(10)
    y = [np.random.randint(0,10) for x in range(10)]
    plt.plot(x,y,linewidth=10,color='red')
    # 重点标记
    plt.plot(x,y,marker="o",markerfacecolor='k',markersize=10)
    
    # 注释文本设置
    plt.annotate('local max', xy=(5, 5), xytext=(10,15),
    arrowprops=dict(facecolor='black',shrink=0.05),
    )

    Run result:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    1.5. Set the graphic style

    plt.figure:调整图片的大小和像素
    	`num`:图的编号,
    	`figsize`:单位是英寸,
    	`dpi`:每英寸的像素点,
    	`facecolor`:图片背景颜色,
    	`edgecolor`:边框颜色,
    	`frameon`:是否绘制画板。

    Example:

    x = range(10)
    y = [np.random.randint(0,10) for x in range(10)]
    # 设置图形样式
    plt.figure(figsize=(20,10),dpi=80)
    plt.plot(x,y,linewidth=10,color='red')

    Running result:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    2. Bar chart

    Application scenario:

    • 1 . total.

    • 2. Frequency statistics.

    Related parameters:

    barh: Bar chart

    • 1. `x`: an array or list, representing the coordinate point of the x-axis of the bar chart to be drawn.

    • 2. `height`: an array or list, representing the coordinate point of the y-axis of the bar chart that needs to be drawn.

    • 3. `width`: The width of each bar chart, the default is 0.8 width.

    • 4. `bottom`: The baseline of the `y` axis, the default is 0, that is, the distance from the bottom is 0.

    • ##5. `align`: Alignment method, the default is `center`, which is aligned with the center of the specified `x` coordinate, and `edge`, which is aligned to the edge, whether to the right or left, depends on the positive or negative value of `width`.
    • 6. `color`: The color of the bar chart.
    • 2.1. Horizontal bar chart example
    movies = {
        "流浪地球":40.78,
        "飞驰人生":15.77,
        "疯狂的外星人":20.83,
        "新喜剧之王":6.10,
        "廉政风云":1.10,
        "神探蒲松龄":1.49,
        "小猪佩奇过大年":1.22,
        "熊出没·原始时代":6.71
    }
    plt.barh(np.arange(len(movies)),list(movies.values()))
    plt.yticks(np.arange(len(movies)),list(movies.keys()),fontproperties=font)
    plt.grid()

    Running results

    What are the steps and methods to draw charts using the Python Matplotlib library?2.2. Grouped bar chart

    Example:

    movies = {
        "流浪地球":[2.01,4.59,7.99,11.83,16],
        "飞驰人生":[3.19,5.08,6.73,8.10,9.35],
        "疯狂的外星人":[4.07,6.92,9.30,11.29,13.03],
        "新喜剧之王":[2.72,3.79,4.45,4.83,5.11],
        "廉政风云":[0.56,0.74,0.83,0.88,0.92],
        "神探蒲松龄":[0.66,0.95,1.10,1.17,1.23],
        "小猪佩奇过大年":[0.58,0.81,0.94,1.01,1.07],
        "熊出没·原始时代":[1.13,1.96,2.73,3.42,4.05]
    }
    plt.figure(figsize=(20,8))
    width = 0.75
    bin_width = width/5
    movie_pd = pd.DataFrame(movies)
    ind = np.arange(0,len(movies))
    
    # 第一种方案
    for index in movie_pd.index:
        day_tickets = movie_pd.iloc[index]
        xs = ind-(bin_width*(2-index))
        plt.bar(xs,day_tickets,width=bin_width,label="第%d天"%(index+1))
        for ticket,x in zip(day_tickets,xs):
            plt.annotate(ticket,xy=(x,ticket),xytext=(x-0.1,ticket+0.1))
    # 设置图例
    plt.ylabel("单位:亿")
    plt.title("春节前5天电影票房记录")
    # 设置x轴的坐标
    plt.xticks(ind,movie_pd.columns)
    plt.xlim
    plt.grid(True)
    plt.show()

    Run result:

    What are the steps and methods to draw charts using the Python Matplotlib library?2.3, Stacked Bar Chart

    Example:

    menMeans = (20, 35, 30, 35, 27)
    womenMeans = (25, 32, 34, 20, 25)
    groupNames = ('G1','G2','G3','G4','G5')
    xs = np.arange(len(menMeans))
    plt.bar(xs,menMeans)
    plt.bar(xs,womenMeans,bottom=menMeans)
    plt.xticks(xs,groupNames)
    plt.show()

    Run result:

    What are the steps and methods to draw charts using the Python Matplotlib library?3. Histogram

    plt.hist: Histogram

      1. x: Array or sequence that can be looped;
    • 2. bins: numbers or sequences (arrays/lists, etc.);
    • 3. range: tuple or None, if it is a tuple, specify `x` to divide the interval Maximum and minimum values;
    • 4. density: The default is `False`, if equal to `True`, then the frequency distribution histogram will be used;
    • 5. cumulative: If this and `density` are both equal to `True`, then the first parameter of the return value will continue to accumulate and eventually equal `1`.
    Application scenarios:

      1. Display the quantity distribution of each group of data.
    • 2. Used to observe abnormal or isolated data.
    • 3. If the number of samples drawn is too small, large errors will occur, the credibility will be low, and the statistical significance will be lost. Therefore, the number of samples should not be less than 50.
    • 3.1, Histogram

    Example:

    durations = [131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
    plt.figure(figsize=(15,5))
    nums,bins,patches = plt.hist(durations,bins=20,edgecolor='k')
    plt.xticks(bins,bins)
    for num,bin in zip(nums,bins):
        plt.annotate(num,xy=(bin,num),xytext=(bin+1.5,num+0.5))
    plt.show()

    Run result:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    3.2、频率直方图

    density:频率直方分布图

    范例:

    nums,bins,patches = plt.hist(durations,bins=20,edgecolor='k',density=True,cumulative=True)
    plt.xticks(bins,bins)
    for num,bin in zip(nums,bins):
        plt.annotate("%.4f"%num,xy=(bin,num),xytext=(bin+0.2,num+0.0005))

    运行结果:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    3.3、直方图

    cumulative参数:nums的总和为1

    范例:

    plt.figure(figsize=(15,5))
    nums,bins,patches = plt.hist(durations,bins=20,edgecolor='k',density=True,cumulative=True)
    plt.xticks(bins,bins)
    for num,bin in zip(nums,bins):
        plt.annotate("%.4f"%num,xy=(bin,num),xytext=(bin+0.2,num+0.0005))

    运行结果:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    4、散点图

    plt.scatter:散点图绘制:

    • 1. x,y:分别是x轴和y轴的数据集。两者的数据长度必须一致。

    • 2. s:点的尺寸。

    • 3. c:点的颜色。

    • 4. marker:标记点,默认是圆点,也可以换成其他的。

    范例:

    plt.scatter(x =data_month_sum["sumprice"]     #传入X变量数据
                ,y=data_month_sum["Quantity"]     #传入Y变量数据
                ,marker='*'     #点的形状
                ,s=10           #点的大小
                ,c='r'          #点的颜色
               )
    plt.show()

    运行结果:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    5、饼图

    饼图:一个划分为几个扇形的圆形统计图表,用于描述量、频率或百分比之间的相对关系的。

    matplotlib中,可以通过plt.pie来实现,其中的参数如下:

    x:饼图的比例序列。labels:饼图上每个分块的名称文字。explode:设置某几个分块是否要分离饼图。autopct:设置比例文字的展示方式。比如保留几个小数等。shadow:是否显示阴影。textprops:文本的属性(颜色,大小等)。 范例

    plt.figure(figsize=(8,8),dpi=100,facecolor='white')
    plt.pie(x = StockCode.values,                  #数据传入
            radius=1.5,                            #半径
            autopct='%.2f%%'                       #百分比显示
            ,pctdistance=0.6,                      #百分比距离圆心比例
            labels=StockCode.index,                #标签
            labeldistance=1.1,                     #标签距离圆心比例
            wedgeprops ={'linewidth':1.5,'edgecolor':'green'}, #边框的线宽和颜色
            textprops={'fontsize':10,'color':'blue'})  #文本字体大小和颜色
    plt.title('商品销量占比',pad=100)              #设置标题及距离坐标轴的位置
    plt.show()

    运行结果:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    6、箱线图

    箱图的绘制方法是:

    • :1、先找出一组数据的上限值、下限值、中位数(Q2)和下四分位数(Q1)以及上四分位数(Q3)

    • :2、然后连接两个四分位数画出箱子

    • :3、再将最大值和最小值与箱子相连接,中位数在箱子中间。

    中位数:把数据按照从小到大的顺序排序,然后最中间的那个值为中位数,如果数据的个数为偶数,那么就是最中间的两个数的平均数为中位数。
    上下四分位数:同样把数据排好序后,把数据等分为4份。出现在`25%`位置的叫做下四分位数,出现在`75%`位置上的数叫做上四分位数。但是四分位数位置的确定方法不是固定的,有几种算法,每种方法得到的结果会有一定差异,但差异不会很大。

    上下限的计算规则是:

    • IQR=Q3-Q1

    • 上限=Q3+1.5IQR

    • 下限=Q1-1.5IQR

    matplotlib中有plt.boxplot来绘制箱线图,这个方法的相关参数如下:

    x:需要绘制的箱线图的数据。notch:是否展示置信区间,默认是False。如果设置为True,那么就会在盒子上展示一个缺口。sym:代表异常点的符号表示,默认是小圆点。vert:是否是垂直的,默认是True,如果设置为False那么将水平方向展示。whis:上下限的系数,默认是1.5,也就是上限是Q3+1.5IQR,可以改成其他的。也可以为一个序列,如果是序列,那么序列中的两个值分别代表的就是下限和上限的值,而不是再需要通过IQR来计算。positions:设置每个盒子的位置。widths:设置每个盒子的宽度。labels:每个盒子的labelmeanlineshowmeans:如果这两个都为True,那么将会绘制平均值的的线条。

    范例:

    #箱线图 - 主要观察数据是否有异常(离群点)
    #箱须-75%和25%的分位数+/-1.5倍分位差
    plt.figure(figsize=(6.4,4.8),dpi=100)
    
    #是否填充箱体颜色,是否展示均值,是否展示异常值,箱体设置,异常值设置,均值设置,中位数设置
    plt.boxplot(x=UnitPrice                               #传入数据
                ,patch_artist=True                                #是否填充箱体颜色
                ,showmeans=True                                   #是否展示均值
                ,showfliers=True                                  #是否展示异常值
                ,boxprops={'color':'black','facecolor':'white'}    #箱体设置
                ,flierprops={'marker':'o','markersize':4,'markerfacecolor':'red'} #异常值设置
                ,meanprops={'marker':'o','markersize':6,'markerfacecolor':'indianred'} #均值设置
                ,medianprops={'linestyle':'--','color':'blue'}   #中位数设置
               )
    plt.show()

    运行结果:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    7、雷达图

    雷达图:又被叫做蜘蛛网图,适用于显示三个或更多的维度的变量的强弱情况

    plt.polar来绘制雷达图,x轴的坐标点应该为弧度(2*PI=360°)

    范例:

    import numpy as np
    properties = ['输出','KDA','发育','团战','生存']
    values = [40,91,44,90,95,40]
    theta = np.linspace(0,np.pi*2,6)
    plt.polar(theta,values)
    plt.xticks(theta,properties)
    plt.fill(theta,values)

    运行结果:

    What are the steps and methods to draw charts using the Python Matplotlib library?

    注意事项:

    • Because polar will not complete the closed drawing of the line, so we need to balance theta and values when drawing. Repeat and add the value of the 0th position at the end, and then it can be closed with the first point when drawing.

    • polar just draws lines, so if you want to fill it with color, you need to call the fill function to achieve it.

    • polarThe default coordinates of the circle are angles. If we want to change it to text display, we can set it through xticks.

    The above is the detailed content of What are the steps and methods to draw charts using the Python Matplotlib library?. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    PHP Tutorial
    1503
    276
    python connect to sql server pyodbc example python connect to sql server pyodbc example Jul 30, 2025 am 02:53 AM

    Install pyodbc: Use the pipinstallpyodbc command to install the library; 2. Connect SQLServer: Use the connection string containing DRIVER, SERVER, DATABASE, UID/PWD or Trusted_Connection through the pyodbc.connect() method, and support SQL authentication or Windows authentication respectively; 3. Check the installed driver: Run pyodbc.drivers() and filter the driver name containing 'SQLServer' to ensure that the correct driver name is used such as 'ODBCDriver17 for SQLServer'; 4. Key parameters of the connection string

    python pandas melt example python pandas melt example Jul 27, 2025 am 02:48 AM

    pandas.melt() is used to convert wide format data into long format. The answer is to define new column names by specifying id_vars retain the identification column, value_vars select the column to be melted, var_name and value_name, 1.id_vars='Name' means that the Name column remains unchanged, 2.value_vars=['Math','English','Science'] specifies the column to be melted, 3.var_name='Subject' sets the new column name of the original column name, 4.value_name='Score' sets the new column name of the original value, and finally generates three columns including Name, Subject and Score.

    python django forms example python django forms example Jul 27, 2025 am 02:50 AM

    First, define a ContactForm form containing name, mailbox and message fields; 2. In the view, the form submission is processed by judging the POST request, and after verification is passed, cleaned_data is obtained and the response is returned, otherwise the empty form will be rendered; 3. In the template, use {{form.as_p}} to render the field and add {%csrf_token%} to prevent CSRF attacks; 4. Configure URL routing to point /contact/ to the contact_view view; use ModelForm to directly associate the model to achieve data storage. DjangoForms implements integrated processing of data verification, HTML rendering and error prompts, which is suitable for rapid development of safe form functions.

    Optimizing Python for Memory-Bound Operations Optimizing Python for Memory-Bound Operations Jul 28, 2025 am 03:22 AM

    Pythoncanbeoptimizedformemory-boundoperationsbyreducingoverheadthroughgenerators,efficientdatastructures,andmanagingobjectlifetimes.First,usegeneratorsinsteadofliststoprocesslargedatasetsoneitematatime,avoidingloadingeverythingintomemory.Second,choos

    What is statistical arbitrage in cryptocurrencies? How does statistical arbitrage work? What is statistical arbitrage in cryptocurrencies? How does statistical arbitrage work? Jul 30, 2025 pm 09:12 PM

    Introduction to Statistical Arbitrage Statistical Arbitrage is a trading method that captures price mismatch in the financial market based on mathematical models. Its core philosophy stems from mean regression, that is, asset prices may deviate from long-term trends in the short term, but will eventually return to their historical average. Traders use statistical methods to analyze the correlation between assets and look for portfolios that usually change synchronously. When the price relationship of these assets is abnormally deviated, arbitrage opportunities arise. In the cryptocurrency market, statistical arbitrage is particularly prevalent, mainly due to the inefficiency and drastic fluctuations of the market itself. Unlike traditional financial markets, cryptocurrencies operate around the clock and their prices are highly susceptible to breaking news, social media sentiment and technology upgrades. This constant price fluctuation frequently creates pricing bias and provides arbitrageurs with

    python iter and next example python iter and next example Jul 29, 2025 am 02:20 AM

    iter() is used to obtain the iterator object, and next() is used to obtain the next element; 1. Use iterator() to convert iterable objects such as lists into iterators; 2. Call next() to obtain elements one by one, and trigger StopIteration exception when the elements are exhausted; 3. Use next(iterator, default) to avoid exceptions; 4. Custom iterators need to implement the __iter__() and __next__() methods to control iteration logic; using default values is a common way to safe traversal, and the entire mechanism is concise and practical.

    python psycopg2 connection pool example python psycopg2 connection pool example Jul 28, 2025 am 03:01 AM

    Use psycopg2.pool.SimpleConnectionPool to effectively manage database connections and avoid the performance overhead caused by frequent connection creation and destruction. 1. When creating a connection pool, specify the minimum and maximum number of connections and database connection parameters to ensure that the connection pool is initialized successfully; 2. Get the connection through getconn(), and use putconn() to return the connection to the pool after executing the database operation. Constantly call conn.close() is prohibited; 3. SimpleConnectionPool is thread-safe and is suitable for multi-threaded environments; 4. It is recommended to implement a context manager in combination with context manager to ensure that the connection can be returned correctly when exceptions are noted;

    python shutil rmtree example python shutil rmtree example Aug 01, 2025 am 05:47 AM

    shutil.rmtree() is a function in Python that recursively deletes the entire directory tree. It can delete specified folders and all contents. 1. Basic usage: Use shutil.rmtree(path) to delete the directory, and you need to handle FileNotFoundError, PermissionError and other exceptions. 2. Practical application: You can clear folders containing subdirectories and files in one click, such as temporary data or cached directories. 3. Notes: The deletion operation is not restored; FileNotFoundError is thrown when the path does not exist; it may fail due to permissions or file occupation. 4. Optional parameters: Errors can be ignored by ignore_errors=True

    See all articles