- If the
RN column is numeric, the plot API will sort it numerically.
- This can be done if you set the
RN column type to str.
- This works best if the values in
RN are unique. If they are not unique, all the freq values for a non-unique RN will be plotted together.
- If
RN is not unique, there's no way for the plot API to differential one value from another.
Non-Unique 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)
- Use
pandas.DataFrame.groupby to group non-unique RNs together
# 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)
