Skylimit
Skylimit

Reputation: 43

Plotly parallel coordinate plots (adjusting colorbar and label fonts) inside a for loop

I am working on generating plotly parallel coordinate plots and I am having problems on how to make the colorbar discrete as well as increasing the font size of my labels. My code also does not seem to work to generate the slice images inside the for loop and it only generates the first slice which does not seem to be correct either, as it incorrectly generates the plot in the first slice for only the store_index 1! In summary, here are the issues: I'd greatly appreciate any feedback on this.

  1. Is there a way to convert the colorbar to 3 discrete colors (or even more colors in other data cases) each one unique for each store_index?

  2. Is there any way to increase the label font sizes (feb_sales, jan_sales, etc...)?

  3. In the plot, is there any way to eliminate the store_index and slice_number from the plots and only keep the colorbar?

  4. What am I doing wrong that the generated plot for each slice_number is either wrong or empty?

Here is the snapshots of my df, my code and attempt:

import pandas as pd
import numpy as np
import plotly.express as px
import os


path = r"C:\Users\test"
  

my_df = pd.DataFrame()
np.random.seed(0)
jan_sales = np.random.randint(low=0, high=100, size = 75)
feb_sales = np.random.randint(low=0, high=100, size =75)

store_index = np.arange(1, 4)
store_index = np.repeat(store_index, 25)

slice_number = np.arange(1, 6)
slice_number = np.tile(slice_number, 3)
slice_number = np.repeat(slice_number, 5)

my_df['jan_sales'] = jan_sales
my_df['feb_sales'] = feb_sales
my_df['store_index'] = store_index
my_df['slice_number'] = slice_number

#my_df


#n_colors = 3
#colors = px.colors.sample_colorscale("turbo", [n/(n_colors -1) for n in range(n_colors)])

for slice in np.arange(1, 6):
    my_df = my_df.loc[my_df['slice_number'] == slice]
    fig = px.parallel_coordinates(my_df, color="store_index", labels={"jan_sales": "feb_sales"},   #colors = colors
                                 color_continuous_scale=px.colors.diverging.Tealrose,  # color_discrete_scale=px.colors.diverging.Tealrose
                                 color_continuous_midpoint=2)
    fig.update_layout(title=dict(
        text='<b>Store Sales by Month and Slice: Slice: {}<b>'.format(slice),

        x=0.5,
        y=0.99,
        font=dict(
            family="Arial",
            size=12,
color='#000000'  )))
    fig.write_image(os.path.join(path,'Slice_{}.png'.format(slice)))
    fig.show()

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Upvotes: 3

Views: 844

Answers (0)

Related Questions