[英]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?我该如何改变它?
RN
column is numeric, the plot API will sort it numerically.RN
列是数字,则 plot API 将按数字对其进行排序。RN
column type to str
.RN
列类型设置为str
,则可以做到这一点。
RN
are unique.RN
中的值是唯一的,则此方法效果最佳。 If they are not unique, all the freq
values for a non-unique RN
will be plotted together.RN
的所有freq
值将被绘制在一起。RN
is not unique, there's no way for the plot API to differential one value from another.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
to group non-unique RNs togetherpandas.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.