<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Matplotlib 教程 > 原文: [http://zetcode.com/python/matplotlib/](http://zetcode.com/python/matplotlib/) Matplotlib 教程展示了如何使用 Matplotlib 在 Python 中創建圖表。 我們創建散點圖,折線圖,條形圖和餅圖。 ## Matplotlib Matplotlib 是用于創建圖表的 Python 庫。 Matplotlib 可用于 Python 腳本,Python 和 IPython shell,jupyter 筆記本,Web 應用服務器以及四個圖形用戶界面工具包。 ## Matplotlib 安裝 Matplotlib 是需要安裝的外部 Python 庫。 ```py $ sudo pip install matplotlib ``` 我們可以使用`pip`工具安裝該庫。 ## Matplotlib 散點圖 散點圖是一種圖形或數學圖,使用笛卡爾坐標顯示一組數據的兩個變量的值。 `scatter.py` ```py #!/usr/bin/python3 import matplotlib.pyplot as plt x_axis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89] plt.title("Prices over 10 years") plt.scatter(x_axis, y_axis, color='darkblue', marker='x', label="item 1") plt.xlabel("Time (years)") plt.ylabel("Price (dollars)") plt.grid(True) plt.legend() plt.show() ``` 該示例繪制了一個散點圖。 該圖表顯示了十年內某些商品的價格。 ```py import matplotlib.pyplot as plt ``` 我們從`matplotlib`模塊導入`pyplot`。 它是創建圖表的命令樣式函數的集合。 它的操作與 MATLAB 類似。 ```py x_axis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89] ``` 我們有 x 和 y 軸的數據。 ```py plt.title("Prices over 10 years") ``` 通過`title()`函數,我們可以為圖表設置標題。 ```py plt.scatter(x_axis, y_axis, color='darkblue', marker='x', label="item 1") ``` `scatter()`函數繪制散點圖。 它接受 x 和 y 軸,標記的顏色,標記的形狀和標簽的數據。 ```py plt.xlabel("Time (years)") plt.ylabel("Price (dollars)") ``` 我們為軸設置標簽。 ```py plt.grid(True) ``` 我們用`grid()`函數顯示網格。 網格由許多垂直和水平線組成。 ```py plt.legend() ``` `legend()`函數在軸上放置圖例。 ```py plt.show() ``` `show()`函數顯示圖表。 ![Scatter chart](https://img.kancloud.cn/94/41/9441bbaf6ef186fdb0301227035e78d7_642x539.jpg) 圖:散點圖 ## 兩個數據集 在下一個示例中,我們將另一個數據集添加到圖表。 `scatter2.py` ```py #!/usr/bin/python3 import matplotlib.pyplot as plt x_axis1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis1 = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89] x_axis2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis2 = [53, 6, 46, 36, 15, 64, 73, 25, 82, 9] plt.title("Prices over 10 years") plt.scatter(x_axis1, y_axis1, color='darkblue', marker='x', label="item 1") plt.scatter(x_axis2, y_axis2, color='darkred', marker='x', label="item 2") plt.xlabel("Time (years)") plt.ylabel("Price (dollars)") plt.grid(True) plt.legend() plt.show() ``` 該圖表顯示兩個數據集。 我們通過標記的顏色來區分它們。 ```py x_axis1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis1 = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89] x_axis2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis2 = [53, 6, 46, 36, 15, 64, 73, 25, 82, 9] ``` 我們有兩個數據集。 ```py plt.scatter(x_axis1, y_axis1, color='darkblue', marker='x', label="item 1") plt.scatter(x_axis2, y_axis2, color='darkred', marker='x', label="item 2") ``` 我們為每個集合調用`scatter()`函數。 ## Matplotlib 折線圖 折線圖是一種顯示圖表的圖表,該信息顯示為一系列數據點,這些數據點通過直線段相連,稱為標記。 `linechart.py` ```py #!/usr/bin/python3 import numpy as np import matplotlib.pyplot as plt t = np.arange(0.0, 3.0, 0.01) s = np.sin(2.5 * np.pi * t) plt.plot(t, s) plt.xlabel('time (s)') plt.ylabel('voltage (mV)') plt.title('Sine Wave') plt.grid(True) plt.show() ``` 該示例顯示正弦波折線圖。 ```py import numpy as np ``` 在示例中,我們還需要`numpy`模塊。 ```py t = np.arange(0.0, 3.0, 0.01) ``` `arange()`函數返回給定間隔內的均勻間隔的值列表。 ```py s = np.sin(2.5 * np.pi * t) ``` 我們獲得數據的`sin()`值。 ```py plt.plot(t, s) ``` 我們使用`plot()`函數繪制折線圖。 ## Matplotlib 條形圖 條形圖顯示帶有矩形條的分組數據,其長度與它們代表的值成比例。 條形圖可以垂直或水平繪制。 `barchart.py` ```py #!/usr/bin/python3 from matplotlib import pyplot as plt from matplotlib import style style.use('ggplot') x = [0, 1, 2, 3, 4, 5] y = [46, 38, 29, 22, 13, 11] fig, ax = plt.subplots() ax.bar(x, y, align='center') ax.set_title('Olympic Gold medals in London') ax.set_ylabel('Gold medals') ax.set_xlabel('Countries') ax.set_xticks(x) ax.set_xticklabels(("USA", "China", "UK", "Russia", "South Korea", "Germany")) plt.show() ``` 該示例繪制了條形圖。 它顯示了 2012 年倫敦每個國家/地區的奧運金牌數量。 ```py style.use('ggplot') ``` 可以使用預定義的樣式。 ```py fig, ax = plt.subplots() ``` `subplots()`函數返回圖形和軸對象。 ```py ax.bar(x, y, align='center') ``` 使用`bar()`函數生成條形圖。 ```py ax.set_xticks(x) ax.set_xticklabels(("USA", "China", "UK", "Russia", "South Korea", "Germany")) ``` 我們為 x 軸設置國家/地區名稱。 ## Matplotlib 餅圖 餅圖是圓形圖,將其分成多個切片以說明數值比例。 `piechart.py` ```py #!/usr/bin/python3 import matplotlib.pyplot as plt labels = ['Oranges', 'Pears', 'Plums', 'Blueberries'] quantity = [38, 45, 24, 10] colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] plt.pie(quantity, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90) plt.axis('equal') plt.show() ``` 該示例創建一個餅圖。 ```py labels = ['Oranges', 'Pears', 'Plums', 'Blueberries'] quantity = [38, 45, 24, 10] ``` 我們有標簽和相應的數量。 ```py colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] ``` 我們為餅圖的切片定義顏色。 ```py plt.pie(quantity, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90) ``` 餅圖是通過`pie()`函數生成的。 `autopct`負責在圖表的楔形圖中顯示百分比。 ```py plt.axis('equal') ``` 我們設置了相等的長寬比,以便將餅圖繪制為圓形。 ![Pie chart](https://img.kancloud.cn/43/c5/43c5afc114e724f2494eca895207eedb_558x399.jpg) 圖:餅圖 在本教程中,我們使用 Matplotlib 庫創建了散點圖,折線圖,條形圖和餅圖。 您可能也對以下相關教程感興趣: [PrettyTable 教程](/python/prettytable/), [Tkinter 教程](/tkinter/), [SymPy 教程](/python/sympy/), [Python Pillow 教程](/python/pillow/), [PyQt5 教程](/gui/pyqt5/)和 [Python 教程](/lang/python/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看