简体   繁体   English

Matplotlib 散点图,每个点都有不同的文本

[英]Matplotlib scatter plot with different text at each point

Let's Say I have 3 series假设我有 3 个系列

>>> df[df['Type']=="Machine Learning"]['Cost']
0     2300.00
1     3200.00
4     1350.00
7     1352.00
8     4056.00
9       79.00
10    1595.00
Name: Cost, dtype: float64
>>>df[df['Type']=="Machine Learning"]['Rank']
0      1
1      1
4      1
7      2
8      2
9      2
10     2
Name: Rank, dtype: int64
>>>df[df['Type']=="Machine Learning"]['Univ/Org']
0     Massachusetts Institute of Technology 
1     Massachusetts Institute of Technology 
4                                    EDX/MIT
7                        Stanford University
8                        Stanford University
9               Coursera/Stanford University
10                       Stanford University
Name: Univ/Org, dtype: object

Now I want to draw a scatter plot with Cost at the y-axis, Rank at the X-axis, and Name of Univ/Org at each data point.现在我想绘制一个散点图,y 轴为成本,X 轴为排名,每个数据点的大学/组织名称。

Now What I am able to do yet after referring to this question is现在我在提到这个问题后还能做的是

plt.scatter(df[df['Type']=="Machine Learning"]['Rank'], df[df['Type']=="Machine Learning"]['Cost'],marker='2', edgecolors='black')
for i, txt in enumerate(df[df['Type']=="Machine Learning"]['Univ/Org']):
    plt.annotate(txt, (df[df['Type']=="Machine Learning"]['Rank'][i], df[df['Type']=="Machine Learning"]['Cost'][i]))

It is naming 2 data points and then giving an error.它正在命名 2 个数据点,然后给出错误。

Plot is :情节是: 在此处输入图片说明

And Error is:错误是:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-111-0d31107a166a> in <module>
      1 plt.scatter(df[df['Type']=="Machine Learning"]['Rank'], df[df['Type']=="Machine Learning"]['Cost'],marker='2', edgecolors='black')
      2 for i, txt in enumerate(df[df['Type']=="Machine Learning"]['Univ/Org']):
----> 3     plt.annotate(txt, (df[df['Type']=="Machine Learning"]['Rank'][i], df[df['Type']=="Machine Learning"]['Cost'][i]))

~/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 2

Couple of things.几件事。

First I recommend you select you ML data in to a new dataframe.首先,我建议您将 ML 数据选择到一个新的数据框中。 You should also use the .loc and .at accessors to be a little more precise.您还应该使用.loc.at访问器来更精确一些。 So like this:所以像这样:

mldf = df.loc[df['Type'] == "Machine Learning", :]

fig, ax = plt.sunplots()
ax.scatter('Rank', 'Cost', data=mldf, marker='2', edgecolors='black')
for i in mldf.index:
    ax.annotate(mldf.at[i, 'Univ/Org'], (mldf.at[i, 'Rank'], mldf.at[i, 'Cost'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在每个数据点使用不同的文本散布 plot - Scatter plot with different text at each data point 如何在matplotlib中为散点图中的每个点绘制不同深浅的颜色? - How to plot different shades of a color for each point in a scatter plot in matplotlib? Python matplotlib:如何为散点图中的每个点分配不同的不透明度? - Python matplotlib: How to assign a different opacity to each point in a scatter plot? 指定散点图中每个点的颜色(matplotlib) - Specify color of each point in scatter plot (matplotlib) 在与标记的大小和颜色相匹配的每个数据点处使用不同文本的散点图 - Scatter plot with different text at each data point that matches the size and colour of the marker matplotlib绘制多个散点图,每个散点图由不同的三次变量着色 - matplotlib Plot multiple scatter plots, each colored by different thrid variable 在matplotlib上的散点图中为每个系列设置不同的颜色 - Setting different color for each series in scatter plot on matplotlib pandas - 散布 plot,每个点都有不同的颜色图例 - pandas - scatter plot with different color legend for each point Matplotlib:具有多个具有不同数据长度和文本的轴的散点图 - Matplotlib: scatter plot with multiple axes with different data lengths and text 删除散点图中的绘制点 - matplotlib - Removing a plotted point in scatter plot - matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM