pandas绘图显示值标签_使用Pandas向绘图添加轴标签

news/2024/7/10 3:06:52 标签: python, 数据分析, java, vue, 机器学习

pandas绘图显示值标签

Pandas plotting methods provide an easy way to plot pandas objects. Often though, you’d like to add axis labels, which involves understanding the intricacies of Matplotlib syntax. Thankfully, there’s a way to do this entirely using pandas.

熊猫绘制方法提供了一种绘制熊猫对象的简便方法。 但是,通常您想添加轴标签,这涉及了解Matplotlib语法的复杂性。 幸运的是,有一种方法可以完全使用熊猫来做到这一点。

Let’s start by importing the required libraries:

首先导入所需的库:

python">import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

python">import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Next, we’ll create a test dataframe, with a column of names and corresponding test scores:

接下来,我们将创建一个测试数据框,其中包含一列名称和相应的测试分数:

name 名称 score 得分
0 0 Joe42 42
1 1个 Sally 莎莉 37 37
2 2 Ananya 阿南亚 98 98

Plotting this dataframe is simple with the pandas methods. We can just use the DataFrame.plot.bar() method to produce a bar plot in two lines.

使用pandas方法绘制此数据框很简单。 我们可以仅使用DataFrame.plot.bar()方法在两行中生成条形图。

python">df.plot.bar()
plt.show()

python">df.plot.bar()
plt.show()

using_set_index_to_provide_axis_labels_5_0

This gives us instant results, but it’s not easy to interpret this plot, because we can’t see which scores belong to which name. If you look closely, you might notice the currently x-axis labels are 0, 1, and 2. These actually correspond with the dataframe index.

这给了我们即时的结果,但是要解释这个图并不容易,因为我们看不到哪个分数属于哪个名字。 如果你仔细观察,你可能会注意到当前x轴标签是01 ,和2 。 这些实际上与数据帧索引相对应。

By setting the index of the dataframe to our names using the set_index() method, we can easily produce axis labels and improve our plot. We’ll use drop=True which will remove the column, and inplace=True instead of having to assign the variable back to itself or to a new variable name.

通过使用set_index()方法将数据set_index()的索引设置为我们的名称,我们可以轻松生成轴标签并改善图。 我们将使用drop=True删除列,并使用inplace=True而不是将变量分配回自身或新的变量名称。

score 得分
name 名称
Joe33 33
Sally 莎莉 95 95
Ananya 阿南亚 36 36

Now, let’s plot again and see our results:

现在,让我们再次绘图并查看结果:

python">df.plot.bar()
plt.show()

python">df.plot.bar()
plt.show()

using_set_index_to_provide_axis_labels_9_0

Instantly, our plot is better looking, and clearly communicates the data!

立刻,我们的绘图看起来更好,并且清楚地传达了数据!

翻译自: https://www.pybloggers.com/2017/12/adding-axis-labels-to-plots-with-pandas/

pandas绘图显示值标签


http://www.niftyadmin.cn/n/984759.html

相关文章

写一个决策树预测代码

决策树是一种常用的分类和回归算法。它能够帮助我们根据数据特征进行分类和预测。在 Python 中,可以使用 scikit-learn 库来训练和使用决策树模型。 以下是一个简单的决策树预测代码示例,假设你已经准备好了训练数据和测试数据。 from sklearn.tree impo…

Unity3d之Socket UDP协议

原文地址:http://blog.csdn.net/dingkun520wy/article/details/49201245 (一)Socket(套接字)UDP协议的特点 1、是基于无连接的协议,没有生成连接的延迟所以速度比TCP快; 2、支持一对一和一对多连接,可以使用…

【转】SQLServer内部原理

原文地址:http://twb.iteye.com/blog/182083 在讲SQLSERVER内部原理的之前,我觉得非常有必要向大家介绍一下SQLSERVER的历史。 让我们站在1999年,看看计算机数据库业界到底处于什么状态。 1999年,Oracle已经于1998年9月发布了Orac…

从讨论研究问题,思想碰撞到,算法实现和实车验证这一完整的开发过程,我学到了什么...

在完整的开发过程中,我们学到了很多东西。 首先,在讨论研究问题的过程中,我们学会了如何从多个角度来思考问题,如何去发现问题的核心,以及如何去解决问题。这是一个非常重要的步骤,因为它可以让我们在进行下…

目前最流行的开发模式DevOps究竟是什么鬼?

https://blog.csdn.net/bntX2jSQfEHy7/article/details/79168865

学习曲线 机器学习_机器学习的学习曲线

学习曲线 机器学习Diagnose Bias and Variance to Reduce Error 诊断偏差和方差以减少误差 When building machine learning models, we want to keep error as low as possible. Two major sources of error are bias and variance. If we managed to reduce these two, then…

四张图带你了解Tomcat系统架构

俗话说,站在巨人的肩膀上看世界,一般学习的时候也是先总览一下整体,然后逐个部分个个击破,最后形成思路,了解具体细节,Tomcat的结构很复杂,但是 Tomcat 非常的模块化,找到了 Tomcat最…

easyui动力头 amp;amp; 动态加入tabs

今天,在实现了业务时的,我们需要根据后台操作,以产生多个数据tab页,而且每一个tab页表格根据需要动态生成的标题数据。返回后台数据格例如,下面的公式: 实现方法例如以下: //$("#compareRe…