I'm trying to make some plots with plotnine.
I have clustered points into several clusters labeled 0 through 6. (but this range may change depending on the number of clusters in the future).
My goal is to plot each cluster with its own point on the legend, using plotnine's default color palette (I actually like it better than other palettes).
If I plot the data as-is, the clusters are integers and viewed as continuous, so the legend is plotted on a continuous scale.
I fixed this by adding
guides(fill=guide_legend(override_aes={"size": 6}))
but then it doesn't show all of the clusters in the legend.
Example:
frame_172_region_plot = (ggplot(data,
aes(x='x',y='y',fill='cluster'))
geom_point(size=3,alpha=1,stroke=0)
theme_bw()
theme(
legend_position=(.5, 0.05), legend_direction='horizontal'
, figure_size=(18.60, 13.96)
, dpi= 300
, axis_title=element_text(size=16)
)
guides(fill=guide_legend(override_aes={"size": 6}))
scale_x_continuous(limits = (0,1860), expand = (0, 0), name='')
scale_y_continuous(limits = (0,1396), expand = (0, 0), name='')
labs(title='All Variables: Unlabeled Clusters. (Frame: 172, Sample: COL3)')
)
Then I tried making the variable a categorical variable using pd.Categorical():
data = data.assign(cluster = pd.Categorical(data['cluster']))
This seemed to work, but now the color palette is different and I have no idea how to change it back to the Plotnine default color palette. Can anyone help me reassign the color palette back to the plotnine default? Thanks!
CodePudding user response:
Use an ordered categorical.
pd.Categorical(data['cluster'], categories=[1, 2, 3, ...], ordered=True)
CodePudding user response:
For some reason my colors are changing when I use categorical, but I found a solution.
I found some useful code (here) and adapted it slightly:
def discrete_cmap_hex(N, base_cmap=None):
"""Create an N-bin discrete colormap from the specified input map, and convert to hex"""
base = plt.cm.get_cmap(base_cmap)
color_list = list(base(np.linspace(0, 1, N)))
color_list_list = [list(x) for x in color_list]
#print(color_list_list)
hex_list = [mpl.colors.to_hex(x, keep_alpha=True) for x in color_list_list]
#print(hex_list)
return hex_list
Then, I used the hex_list in scale_fill_manual:
scale_fill_manual(discrete_cmap_hex(8,'viridis'))
This successfully used the viridis color scheme, discretized it and applied it to my plot.