[英]How to sort scatter plot by decreasing values of y axis using pyplot?
我有一个 dataframe df1 包含两列 freq 和 RN,数据按频率升序排序。
In [2]: df1.head()
Out[2]:
freq RN
147 1 181
56 1 848
149 1 814
25 1 829
我想 plot 分散 plot 与 X 轴作为 RN 和 y 轴作为频率,其中 X 值按 y 值的升序排列,即。 我希望 x 轴排列为 841,848,835,... 如 df1 中给出的,它已根据频率值的升序排序。
现在,如果我写plt.scatter('RN', 'freq',data=df1)
我得到的 output x 轴不是按频率的升序排序的。 它按照自己的自然升序排列,例如 800,801,...,860。
注意: plt.bar('RN', 'freq',data=df1)
以我想要的正确方式工作。
我该如何改变它?
RN
列是数字,则 plot API 将按数字对其进行排序。RN
列类型设置为str
,则可以做到这一点。
RN
中的值是唯一的,则此方法效果最佳。 如果它们不是唯一的,则非唯一RN
的所有freq
值将被绘制在一起。RN
不是唯一的,则 plot API 无法区分一个值与另一个值。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)
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)
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.