简体   繁体   English

如何通过使用pyplot降低y轴的值来对散点图plot进行排序?

[英]How to sort scatter plot by decreasing values of y axis using pyplot?

I have a dataframe df1 containing two columns freq and RN with the data sorted according to ascending order by freq.我有一个 dataframe df1 包含两列 freq 和 RN,数据按频率升序排序。

    In [2]: df1.head()
    Out[2]: 
       freq  RN
    147 1   181
    56  1   848
    149 1   814
    25  1   829

I want to plot a scatter plot with X axis as RN and y axis as freq where the X values are arranged in ascending order of the y values ie.我想 plot 分散 plot 与 X 轴作为 RN 和 y 轴作为频率,其中 X 值按 y 值的升序排列,即。 I want the x axis to be arranged as 841,848,835,... as given in df1 which has been sorted according to ascending order of freq values.我希望 x 轴排列为 841,848,835,... 如 df1 中给出的,它已根据频率值的升序排序。

Now if I write plt.scatter('RN', 'freq',data=df1) the output x axis I get is not sorted by the ascending order of freq.现在,如果我写plt.scatter('RN', 'freq',data=df1)我得到的 output x 轴不是按频率的升序排序的。 It is arranged in its own natural ascending order like 800,801,...,860.它按照自己的自然升序排列,例如 800,801,...,860。

Note: plt.bar('RN', 'freq',data=df1) works in the correct way as I want.注意: plt.bar('RN', 'freq',data=df1)以我想要的正确方式工作。

在此处输入图像描述

How Do I change it?我该如何改变它?

  • If the RN column is numeric, the plot API will sort it numerically.如果RN列是数字,则 plot API 将按数字对其进行排序。
  • This can be done if you set the RN column type to str .如果您将RN列类型设置为str ,则可以做到这一点。
    • This works best if the values in RN are unique.如果RN中的值是唯一的,则此方法效果最佳。 If they are not unique, all the freq values for a non-unique RN will be plotted together.如果它们不是唯一的,则非唯一RN的所有freq值将被绘制在一起。
    • If RN is not unique, there's no way for the plot API to differential one value from another.如果RN不是唯一的,则 plot API 无法区分一个值与另一个值。

Non-Unique RN (a)非唯一 RN (a)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# create test data
np.random.seed(365)
data = {'freq': np.random.randint(20, size=(20,)), 'RN': np.random.randint(800, 900, size=(20,))}
df = pd.DataFrame(data)

# convert RN to a str type
df.RN = df.RN.astype(str)

# sort freq
df.sort_values('freq', ascending=True, inplace=True)

# plot
plt.scatter('RN', 'freq', data=df)

在此处输入图像描述

Non-Unique RN (b)非唯一 RN (b)

  • Use pandas.DataFrame.groupby to group non-unique RNs together使用pandas.DataFrame.groupby将非唯一的 RN 分组在一起
# create test data
# create test data
np.random.seed(365)
data = {'freq': np.random.randint(20, size=(20,)), 'RN': np.random.randint(800, 900, size=(20,))}
df = pd.DataFrame(data)

# convert RN to a str type
df.RN = df.RN.astype(str)

# combine non-unique RN with groupby and sort by freq
dfg = df.groupby('RN', as_index=False)['freq'].sum().sort_values('freq')

# plot
plt.scatter('RN', 'freq', data=dfg)

在此处输入图像描述

Unique RN独特的注册护士

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# create test data
np.random.seed(365)
data = {'freq': np.random.randint(20, size=(20,)), 'RN': np.arange(800, 820)}
df = pd.DataFrame(data)

# convert RN to a str type
df.RN = df.RN.astype(str)

# sort `freq`
df.sort_values('freq', ascending=True, inplace=True)

# plot
plt.scatter('RN', 'freq', data=df)

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM