最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python繪制散點(diǎn)圖的教程詳解

 更新時間:2022年03月16日 15:23:02   作者:Vertira  
散點(diǎn)圖是指在回歸分析中,數(shù)據(jù)點(diǎn)在直角坐標(biāo)系平面上的分布圖,散點(diǎn)圖表示因變量隨自變量而變化的大致趨勢,據(jù)此可以選擇合適的函數(shù)對數(shù)據(jù)點(diǎn)進(jìn)行擬合。本文將用Python繪制散點(diǎn)圖,需要的可以參考一下

少廢話,直接上代碼 

import matplotlib.pyplot as plt
import numpy as np
# 1. 首先是導(dǎo)入包,創(chuàng)建數(shù)據(jù)
n = 10
x = np.random.rand(n) * 2# 隨機(jī)產(chǎn)生10個0~2之間的x坐標(biāo)
y = np.random.rand(n) * 2# 隨機(jī)產(chǎn)生10個0~2之間的y坐標(biāo)
# 2.創(chuàng)建一張figure
fig = plt.figure(1)
# 3. 設(shè)置顏色 color 值【可選參數(shù),即可填可不填】,方式有幾種
# colors = np.random.rand(n) # 隨機(jī)產(chǎn)生10個0~1之間的顏色值,或者
colors = ['r', 'g', 'y', 'b', 'r', 'c', 'g', 'b', 'k', 'm']  # 可設(shè)置隨機(jī)數(shù)取
# 4. 設(shè)置點(diǎn)的面積大小 area 值 【可選參數(shù)】
area = 20*np.arange(1, n+1)
# 5. 設(shè)置點(diǎn)的邊界線寬度 【可選參數(shù)】
widths = np.arange(n)# 0-9的數(shù)字
# 6. 正式繪制散點(diǎn)圖:scatter
plt.scatter(x, y, s=area, c=colors, linewidths=widths, alpha=0.5, marker='o')
# 7. 設(shè)置軸標(biāo)簽:xlabel、ylabel
#設(shè)置X軸標(biāo)簽
plt.xlabel('X坐標(biāo)')
#設(shè)置Y軸標(biāo)簽
plt.ylabel('Y坐標(biāo)')
# 8. 設(shè)置圖標(biāo)題:title
plt.title('test繪圖函數(shù)')
# 9. 設(shè)置軸的上下限顯示值:xlim、ylim
# 設(shè)置橫軸的上下限值
plt.xlim(-0.5, 2.5)
# 設(shè)置縱軸的上下限值
plt.ylim(-0.5, 2.5)
# 10. 設(shè)置軸的刻度值:xticks、yticks
# 設(shè)置橫軸精準(zhǔn)刻度
plt.xticks(np.arange(np.min(x)-0.2, np.max(x)+0.2, step=0.3))
# 設(shè)置縱軸精準(zhǔn)刻度
plt.yticks(np.arange(np.min(y)-0.2, np.max(y)+0.2, step=0.3))
# 也可按照xlim和ylim來設(shè)置
# 設(shè)置橫軸精準(zhǔn)刻度
plt.xticks(np.arange(-0.5, 2.5, step=0.5))
# 設(shè)置縱軸精準(zhǔn)刻度
plt.yticks(np.arange(-0.5, 2.5, step=0.5))
 
# 11. 在圖中某些點(diǎn)上(位置)顯示標(biāo)簽:annotate
# plt.annotate("(" + str(round(x[2], 2)) + ", " + str(round(y[2], 2)) + ")", xy=(x[2], y[2]), fontsize=10, xycoords='data')# 或者
plt.annotate("({0},{1})".format(round(x[2],2), round(y[2],2)), xy=(x[2], y[2]), fontsize=10, xycoords='data')
# xycoords='data' 以data值為基準(zhǔn)
# 設(shè)置字體大小為 10
# 12. 在圖中某些位置顯示文本:text
plt.text(round(x[6],2), round(y[6],2), "good point", fontdict={'size': 10, 'color': 'red'})  # fontdict設(shè)置文本字體
# Add text to the axes.
# 13. 設(shè)置顯示中文
plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負(fù)號
# 14. 設(shè)置legend,【注意,'繪圖測試':一定要是可迭代格式,例如元組或者列表,要不然只會顯示第一個字符,也就是legend會顯示不全】
plt.legend(['繪圖測試'], loc=2, fontsize=10)
# plt.legend(['繪圖測試'], loc='upper left', markerscale = 0.5, fontsize = 10) #這個也可
# markerscale:The relative size of legend markers compared with the originally drawn ones.
# 15. 保存圖片 savefig
plt.savefig('test_xx.png', dpi=200, bbox_inches='tight', transparent=False)
# dpi: The resolution in dots per inch,設(shè)置分辨率,用于改變清晰度
# If *True*, the axes patches will all be transparent
# 16. 顯示圖片 show
plt.show()

scatter主要參數(shù):

def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
                vmin=None, vmax=None, alpha=None, linewidths=None,
                verts=None, edgecolors=None,
                **kwargs):
        """
        A scatter plot of *y* vs *x* with varying marker size and/or color.
        Parameters
        ----------
        x, y : array_like, shape (n, )
            The data positions.
        s : scalar or array_like, shape (n, ), optional
            The marker size in points**2.
            Default is ``rcParams['lines.markersize'] ** 2``.
        c : color, sequence, or sequence of color, optional, default: 'b'
            The marker color. Possible values:
            - A single color format string.
            - A sequence of color specifications of length n.
            - A sequence of n numbers to be mapped to colors using *cmap* and
              *norm*.
            - A 2-D array in which the rows are RGB or RGBA.
            Note that *c* should not be a single numeric RGB or RGBA sequence
            because that is indistinguishable from an array of values to be
            colormapped. If you want to specify the same RGB or RGBA value for
            all points, use a 2-D array with a single row.
        marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'
            The marker style. *marker* can be either an instance of the class
            or the text shorthand for a particular marker.
            See `~matplotlib.markers` for more information marker styles.
        cmap : `~matplotlib.colors.Colormap`, optional, default: None
            A `.Colormap` instance or registered colormap name. *cmap* is only
            used if *c* is an array of floats. If ``None``, defaults to rc
            ``image.cmap``.
        alpha : scalar, optional, default: None
            The alpha blending value, between 0 (transparent) and 1 (opaque).
        linewidths : scalar or array_like, optional, default: None
            The linewidth of the marker edges. Note: The default *edgecolors*
            is 'face'. You may want to change this as well.
            If *None*, defaults to rcParams ``lines.linewidth``.

設(shè)置legend,【注意,'繪圖測試’:一定要是可迭代格式,例如元組或者列表,要不然只會顯示第一個字符,也就是legend會顯示不全】

plt.legend(['繪圖測試'], loc=2, fontsize = 10)
# plt.legend(['繪圖測試'], loc='upper left', markerscale = 0.5, fontsize = 10) #這個也可
#  markerscale:The relative size of legend markers compared with the originally drawn ones.

其參數(shù)loc對應(yīng)為:

運(yùn)行結(jié)果:

補(bǔ)充

除了二維的散點(diǎn)圖,Python還能繪制三維的散點(diǎn)圖,下面的示例代碼

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
 
# 隨機(jī)種子
np.random.seed(1)
 
 
def randrange(n, vmin, vmax):
    '''
    使數(shù)據(jù)分布均勻(vmin, vmax).
    '''
    return (vmax - vmin)*np.random.rand(n) + vmin
 
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')  # 可進(jìn)行多圖繪制
 
n = 500
 
# 對于每一組樣式和范圍設(shè)置,在由x在[23,32]、y在[0,100]、
# z在[zlow,zhigh]中定義的框中繪制n個隨機(jī)點(diǎn)
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, marker=m)  # 繪圖
 
# X、Y、Z的標(biāo)簽
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
 
plt.show()

輸出結(jié)果:

到此這篇關(guān)于Python繪制散點(diǎn)圖的教程詳解的文章就介紹到這了,更多相關(guān)Python散點(diǎn)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python @property使用方法解析

    Python @property使用方法解析

    這篇文章主要介紹了Python @property使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • python基于plotly實(shí)現(xiàn)畫餅狀圖代碼實(shí)例

    python基于plotly實(shí)現(xiàn)畫餅狀圖代碼實(shí)例

    這篇文章主要介紹了python基于plotly實(shí)現(xiàn)畫餅狀圖代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • pandas 數(shù)據(jù)類型轉(zhuǎn)換的實(shí)現(xiàn)

    pandas 數(shù)據(jù)類型轉(zhuǎn)換的實(shí)現(xiàn)

    這篇文章主要介紹了pandas 數(shù)據(jù)類型轉(zhuǎn)換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Pytorch訓(xùn)練過程出現(xiàn)nan的解決方式

    Pytorch訓(xùn)練過程出現(xiàn)nan的解決方式

    今天小編就為大家分享一篇Pytorch訓(xùn)練過程出現(xiàn)nan的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python實(shí)現(xiàn)的一個自動售飲料程序代碼分享

    Python實(shí)現(xiàn)的一個自動售飲料程序代碼分享

    這篇文章主要介紹了Python實(shí)現(xiàn)的一個自動售飲料程序代碼分享,就是用python實(shí)現(xiàn)的生活中一種投幣式自動售飲料機(jī)的內(nèi)部程序判斷代碼,需要的朋友可以參考下
    2014-08-08
  • python實(shí)現(xiàn)sqlalchemy的使用概述

    python實(shí)現(xiàn)sqlalchemy的使用概述

    SQLAlchemy是Python中最有名的ORM工具,特點(diǎn)是操縱Python對象而不是SQL查詢,也就是在代碼層面考慮的是對象,而不是SQL,體現(xiàn)的是一種程序化思維,這樣使得Python程序更加簡潔易懂,具體內(nèi)容詳情跟隨小編一起看看吧
    2021-08-08
  • Python3中的map函數(shù)調(diào)用后內(nèi)存釋放問題

    Python3中的map函數(shù)調(diào)用后內(nèi)存釋放問題

    這篇文章主要介紹了Python3中的map函數(shù)調(diào)用后內(nèi)存釋放問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python自然語言處理停用詞過濾實(shí)例詳解

    Python自然語言處理停用詞過濾實(shí)例詳解

    這篇文章主要為大家介紹了Python自然語言處理停用詞過濾實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Pytorch中transforms.Resize()的簡單使用

    Pytorch中transforms.Resize()的簡單使用

    這篇文章主要介紹了Pytorch中transforms.Resize()的簡單使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python將多個excel文件合并為一個文件

    Python將多個excel文件合并為一個文件

    這篇文章主要為大家詳細(xì)介紹了Python將多個excel文件合并為一個文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論

敖汉旗| 西青区| 新宾| 特克斯县| 宁陕县| 肇源县| 太谷县| 大庆市| 鸡东县| 广宗县| 古交市| 鹤庆县| 龙门县| 加查县| 环江| 安图县| 尼勒克县| 静宁县| 边坝县| 洛阳市| 阳原县| 陵川县| 普格县| 沈阳市| 青河县| 永宁县| 洱源县| 罗源县| 邯郸县| 高雄县| 星子县| 清新县| 黔西县| 临猗县| 松滋市| 广德县| 永顺县| 宽甸| 昌图县| 安乡县| 伊春市|