| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
xabenohjoxo998
7年前发布

基于Python的数据可视化 matplotlib seaborn pandas

   <p>原文采用了kaggle上iris花的数据,数据来源从上面的网址上找噢</p>    <p>如果没有seaborn库 安装方法如下</p>    <p>http://www.ithao123.cn/content-10393533.html</p>    <p>正式开始了~~~</p>    <pre>  <code class="language-python"># 首先载入pandas  importpandasas pd     # 我们将载入seaborn,但是因为载入时会有警告出现,因此先载入warnings,忽略警告  importwarnings  warnings.filterwarnings("ignore")  importseabornas sns  importmatplotlib.pyplotas plt  sns.set(style="white", color_codes=True)     # 载入数据  iris = pd.read_csv("../input/Iris.csv") # 数据现在为 DataFrame格式     # 用head函数看一下数据结构啥样  iris.head()  </code></pre>    <p>数据结构就这样:</p>    <table>     <thead>      <tr>       <th> </th>       <th>Id</th>       <th>SepalLengthCm</th>       <th>SepalWidthCm</th>       <th>PetalLengthCm</th>       <th>PetalWidthCm</th>       <th>Species</th>      </tr>     </thead>     <tbody>      <tr>       <th>0</th>       <td>1</td>       <td>5.1</td>       <td>3.5</td>       <td>1.4</td>       <td>0.2</td>       <td>Iris-setosa</td>      </tr>      <tr>       <th>1</th>       <td>2</td>       <td>4.9</td>       <td>3.0</td>       <td>1.4</td>       <td>0.2</td>       <td>Iris-setosa</td>      </tr>      <tr>       <th>2</th>       <td>3</td>       <td>4.7</td>       <td>3.2</td>       <td>1.3</td>       <td>0.2</td>       <td>Iris-setosa</td>      </tr>      <tr>       <th>3</th>       <td>4</td>       <td>4.6</td>       <td>3.1</td>       <td>1.5</td>       <td>0.2</td>       <td>Iris-setosa</td>      </tr>      <tr>       <th>4</th>       <td>5</td>       <td>5.0</td>       <td>3.6</td>       <td>1.4</td>       <td>0.2</td>       <td>Iris-setosa</td>      </tr>     </tbody>    </table>    <pre>  <code class="language-python"># 让我们用counts功能看下一共有多少种花  iris["Species"].value_counts()  </code></pre>    <p>结果是:</p>    <pre>  <code class="language-python">Iris-setosa        50  Iris-virginica    50  Iris-versicolor    50  Name: Species, dtype: int64  </code></pre>    <p>1.</p>    <pre>  <code class="language-python"># 使用 .plot 做散点图  iris.plot(kind="scatter", x="SepalLengthCm", y="SepalWidthCm")#数据为萼片的长和宽 结果如下  </code></pre>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/83729513586cf96390fd03d23e1a24e8.png"></p>    <p>2.</p>    <pre>  <code class="language-python"># 开始使用seaborn了它能同时显示直方图噢  sns.jointplot(x="SepalLengthCm", y="SepalWidthCm", data=iris, size=5)  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/707b6ed306a185fea00c399815de7e35.png"></p>    <p>3神奇的还在下面:</p>    <pre>  <code class="language-python"># 我们还可以用seaborn's FacetGrid 标记不同的种类噢  sns.FacetGrid(iris, hue="Species", size=5)  #hue英文是色彩的意思    .map(plt.scatter, "SepalLengthCm", "SepalWidthCm") #注意这里的plt哦    .add_legend()  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/5e82bf6abf75715c428fb40282d616c5.png"></p>    <p>4箱线图!</p>    <pre>  <code class="language-python">#  Seaborn中的boxplot,可以画箱线图,可以看出不同种类的分布情况  sns.boxplot(x="Species", y="PetalLengthCm", data=iris)  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/cb28760b14893d1a759b3e6a0ebc827c.png"></p>    <p>5、</p>    <pre>  <code class="language-python"># 利用striplot可以锦上添花,加上散点图  #  # 使振动值jitter=True 使各个散点分开,要不然会是一条直线  #  # 注意这里将坐标图用ax来保存了哦,这样第二次才会在原来的基础上加点  ax = sns.boxplot(x="Species", y="PetalLengthCm", data=iris)  ax = sns.stripplot(x="Species", y="PetalLengthCm", data=iris, jitter=True, edgecolor="gray")  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/33e162920d7d4136f50f1a28c353ebdb.png"></p>    <p>6、小提琴图</p>    <pre>  <code class="language-python"># 这图可以变现出密度的分布  sns.violinplot(x="Species", y="PetalLengthCm", data=iris, size=6)  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d3328181b94b63c30b62030963d15fe5.png"></p>    <p>7、kdeplot</p>    <pre>  <code class="language-python"># 通过这个曲线图可以看出不同特征值时的分布密度  sns.FacetGrid(iris, hue="Species", size=6)     .map(sns.kdeplot, "PetalLengthCm")     .add_legend()  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/b4ad6a869768c7bd5096a86166a728ae.png"></p>    <p>8.大招来了</p>    <pre>  <code class="language-python">#  pairplot显示不同特征之间的关系  sns.pairplot(iris.drop("Id", axis=1), hue="Species", size=3)  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/0b1a0b93b2a321eab5e189d4721d58d2.png"></p>    <p>9、中间对角线的图形也可以用kde显示哦</p>    <pre>  <code class="language-python"># 修改参数dige_kind  sns.pairplot(iris.drop("Id", axis=1), hue="Species", size=3, diag_kind="kde")  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/14f2eeeb280b69479fb20b9e65aebb9d.png"></p>    <p>10.现在是pandas表现的时间了</p>    <pre>  <code class="language-python"># 用Pandas 快速做出每个特征在不同种类下的箱线图  iris.drop("Id", axis=1).boxplot(by="Species", figsize=(12, 6))  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/192496e7def44a95e6fc47224d658d5e.png"></p>    <p>11.调和曲线图 Andrew Curves</p>    <p>首先啥是Andrew curves呢 看维基百科</p>    <p>https://en.wikipedia.org/wiki/Andrews_plot</p>    <p>他是将高维的点 化为二维的曲线,曲线是一条傅里叶函数的样子,参数项为不同的特征值,臆想出来了自变量t,这样每个点都是一条曲线</p>    <pre>  <code class="language-python"># 画图的函数在下面,我们会发现相同种类的线总是缠绵在一起,可以和聚类混在一起噢,事实上他们与欧氏距离是有关系的  frompandas.tools.plottingimportandrews_curves  andrews_curves(iris.drop("Id", axis=1), "Species")  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/81f33711ce40c5f1b801bda7f6d5814c.png"></p>    <p>12轮廓图</p>    <p>https://en.wikipedia.org/wiki/Parallel_coordinates</p>    <pre>  <code class="language-python"># 轮廓图也是看高维数据的一种方法,将不同的特征放在横坐标,然后将各点的特征值放在纵坐标就可以了  frompandas.tools.plottingimportparallel_coordinates  parallel_coordinates(iris.drop("Id", axis=1), "Species")  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/210925c81f14b151d923e44d002a284f.png"></p>    <p>13 radviz</p>    <p>http://www.doc88.com/p-912968623585.html</p>    <pre>  <code class="language-python"># 这也是一种将高维点表现在二维平面的方法,具体作图方法应该在上面的网址上应该有  frompandas.tools.plottingimportradviz  radviz(iris.drop("Id", axis=1), "Species")  </code></pre>    <p style="text-align:center"><img src="https://simg.open-open.com/show/2bf41f0029601868d58eff314c26a67d.png"></p>    <p>暂时就是这些,希望会对大家有帮助</p>    <p> </p>    <p> </p>    <p> </p>    
 本文由用户 xabenohjoxo998 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1482998303479.html
数据可视化 Python Python开发 pandas