带圆圈大小的散点图_Python数据可视化,Matplotlib绘制“散点图”的两种方法!...
前言
散點圖是Matplotlib常用圖形之一,與線形圖類似。但是這種圖形不再由線段連接,而是由獨立的點、圓圈或其他形狀構成。那么怎么畫散點圖呢?Matplotlib給出了兩種不同的方法,去畫散點圖。如何在不同的情況下,合理的使用這兩種方法?
用plt.plot畫散點圖
"-webkit-tap-highlight-color:?transparent;?box-sizing:?border-box;?font-family:?Consolas,?Menlo,?Courier,?monospace;?font-size:?16px;?white-space:?pre-wrap;?position:?relative;?line-height:?1.5;?color:?rgb(153,?153,?153);?margin:?1em?0px;?padding:?12px?10px;?background:?rgb(244,?245,?246);?border:?1px?solid?rgb(232,?232,?232);?font-style:?normal;?font-variant-ligatures:?normal;?font-variant-caps:?normal;?font-weight:?normal;?letter-spacing:?normal;?orphans:?2;?text-align:?start;?text-indent:?0px;?text-transform:?none;?widows:?2;?word-spacing:?0px;?-webkit-text-stroke-width:?0px;?text-decoration-style:?initial;?text-decoration-color:?initial;">import?numpy?as?npimport?matplotlib.pyplot?as?pltx=?np.linspace(0,?10,?30)
y=?np.sin(x)
plt.plot(x,?y,?'o',?color='black');pre>
plt.plot()函數的第三個參數是一個字符,表示圖形符號的類型。與你之前用 '-' 和 '--' 設置線條屬性類似,對應的圖形標記也有縮寫形式。
1.部分圖形標記展示
"-webkit-tap-highlight-color:?transparent;?box-sizing:?border-box;?font-family:?Consolas,?Menlo,?Courier,?monospace;?font-size:?16px;?white-space:?pre-wrap;?position:?relative;?line-height:?1.5;?color:?rgb(153,?153,?153);?margin:?1em?0px;?padding:?12px?10px;?background:?rgb(244,?245,?246);?border:?1px?solid?rgb(232,?232,?232);?font-style:?normal;?font-variant-ligatures:?normal;?font-variant-caps:?normal;?font-weight:?normal;?letter-spacing:?normal;?orphans:?2;?text-align:?start;?text-indent:?0px;?text-transform:?none;?widows:?2;?word-spacing:?0px;?-webkit-text-stroke-width:?0px;?text-decoration-style:?initial;?text-decoration-color:?initial;">rng=?np.random.RandomState(0)formarkerin['o',?'.',?',',?'x',?'+',?'v',?'^',?',?'>',?'s',?'d']:
plt.plot(rng.rand(5),?rng.rand(5),?marker,
label="marker='{0}'".format(marker))
plt.legend(numpoints=1)
plt.xlim(0,?1.8);
2.連接每一個點
"-webkit-tap-highlight-color:?transparent;?box-sizing:?border-box;?font-family:?Consolas,?Menlo,?Courier,?monospace;?font-size:?16px;?white-space:?pre-wrap;?position:?relative;?line-height:?1.5;?color:?rgb(153,?153,?153);?margin:?1em?0px;?padding:?12px?10px;?background:?rgb(244,?245,?246);?border:?1px?solid?rgb(232,?232,?232);?font-style:?normal;?font-variant-ligatures:?normal;?font-variant-caps:?normal;?font-weight:?normal;?letter-spacing:?normal;?orphans:?2;?text-align:?start;?text-indent:?0px;?text-transform:?none;?widows:?2;?word-spacing:?0px;?-webkit-text-stroke-width:?0px;?text-decoration-style:?initial;?text-decoration-color:?initial;">plt.plot(x,?y,?'-ok')#?直線(-)、圓圈(o)、黑色(k)用圖形標記的縮寫形式,跟線段組合成一給新的字符,傳給plt.plot()函數
3.自定義線條和散點屬性
"-webkit-tap-highlight-color:?transparent;?box-sizing:?border-box;?font-family:?Consolas,?Menlo,?Courier,?monospace;?font-size:?16px;?white-space:?pre-wrap;?position:?relative;?line-height:?1.5;?color:?rgb(153,?153,?153);?margin:?1em?0px;?padding:?12px?10px;?background:?rgb(244,?245,?246);?border:?1px?solid?rgb(232,?232,?232);?font-style:?normal;?font-variant-ligatures:?normal;?font-variant-caps:?normal;?font-weight:?normal;?letter-spacing:?normal;?orphans:?2;?text-align:?start;?text-indent:?0px;?text-transform:?none;?widows:?2;?word-spacing:?0px;?-webkit-text-stroke-width:?0px;?text-decoration-style:?initial;?text-decoration-color:?initial;">plt.plot(x,?y,?'-p',?color='gray',markersize=15,?linewidth=4,
markerfacecolor='white',
markeredgecolor='gray',
markeredgewidth=2)
plt.ylim(-1.2,?1.2)
plt.plot函數非常靈活,可以滿足各種不同的可視化配置需求。
用plt.scatter畫散點圖
這是另一個創建散點圖的函數是plt.scatter。它的功能非常強大,其用法與plt.plot函數類似。
<pre?style="-webkit-tap-highlight-color:?transparent;?box-sizing:?border-box;?font-family:?Consolas,?Menlo,?Courier,?monospace;?font-size:?16px;?white-space:?pre-wrap;?position:?relative;?line-height:?1.5;?color:?rgb(153,?153,?153);?margin:?1em?0px;?padding:?12px?10px;?background:?rgb(244,?245,?246);?border:?1px?solid?rgb(232,?232,?232);?font-style:?normal;?font-variant-ligatures:?normal;?font-variant-caps:?normal;?font-weight:?normal;?letter-spacing:?normal;?orphans:?2;?text-align:?start;?text-indent:?0px;?text-transform:?none;?widows:?2;?word-spacing:?0px;?-webkit-text-stroke-width:?0px;?text-decoration-style:?initial;?text-decoration-color:?initial;">plt.scatter(x,?y,?marker='o')pre>
plot與scatter的區別
plt.scatter與plt.plot的主要差別在于,前者在創建散點圖時具有更高的靈活性,可以 單獨控制每個散點與數據匹配,也可以讓每個散點具有不同的屬性(大小、表面顏色、邊 框顏色等)。
1.隨機散點圖
創建一個隨機散點圖,里面有各種顏色和大小的散點。為了能更好地顯示重疊部分,用alpha參數來調整透明度。
"-webkit-tap-highlight-color:?transparent;?box-sizing:?border-box;?font-family:?Consolas,?Menlo,?Courier,?monospace;?font-size:?16px;?white-space:?pre-wrap;?position:?relative;?line-height:?1.5;?color:?rgb(153,?153,?153);?margin:?1em?0px;?padding:?12px?10px;?background:?rgb(244,?245,?246);?border:?1px?solid?rgb(232,?232,?232);?font-style:?normal;?font-variant-ligatures:?normal;?font-variant-caps:?normal;?font-weight:?normal;?letter-spacing:?normal;?orphans:?2;?text-align:?start;?text-indent:?0px;?text-transform:?none;?widows:?2;?word-spacing:?0px;?-webkit-text-stroke-width:?0px;?text-decoration-style:?initial;?text-decoration-color:?initial;">rng=?np.random.RandomState(0)x=?rng.randn(100)
y=?rng.randn(100)
colors=?rng.rand(100)
sizes=?1000*rng.rand(100)
plt.scatter(x,?y,?c=colors,?s=sizes,?alpha=0.3,
cmap='viridis')
plt.colorbar();?#?顯示顏色條
注意點,顏色自動映射成顏色條(color scale,通過colorbar()顯示),散點的大小以像素為單位。這樣,散點的顏色與大小就可以在可視化圖中顯示多維數據的信息了。
plot與scatter:效率對比
plt.plot 與 plt.scatter 除了特征上的差異之外,還有什么影響我們選擇的因素呢?在數 據量較小的時候,兩者在效率上的差異不大。但是當數據變大到幾千個散點時,plt.plot 的效率將大大高于 plt.scatter。
這是由于 plt.scatter 會對每個散點進行單獨的大小與顏 色的渲染,因此渲染器會消耗更多的資源。而在 plt.plot 中,散點基本都彼此復制,因此整個數據集中所有點的顏色、尺寸只需要配置一次。
由于這兩種方法在處理大型數據集時有很大的性能差異,因此面對大型數據集時,plt.plot 方法比 plt.scatter 方法好。
*聲明:本文于網絡整理,版權歸原作者所有,如來源信息有誤或侵犯權益,請聯系我們刪除或授權事宜。
覺得不錯,點個“在看”然后轉發出去
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的带圆圈大小的散点图_Python数据可视化,Matplotlib绘制“散点图”的两种方法!...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fastadmin 批量上传不成功_sh
- 下一篇: python条件语句函数_python