简体   繁体   中英

Pandas Dataframe Boxplot Y axis not correct scale

I am trying to use the boxplot feature of Pandas Dataframe but some of my features have different scale than others, which makes the charts look bad, it is impossible to read the Y values of the smallest ones.

Here is my code:

df.boxplot(by='label', figsize=(30,30))

And I get a grid of 4 by 4 charts, with 4 labels for each which is correct.

However, the scale is adjusted for the largest values not individually per charts.

Is there a way to solve this ? Thanks !

Update:

Here is something I tried:

charts = df.boxplot(by='label', figsize=(30,30), return_type='axes')

for ax in charts:
   y_axe_name = ax.get_title()
   min_value = all_data[y_axe_name].min()
   max_value = all_data[y_axe_name].max()
   ax.set_ylim(0, max_value + (20 * max_value / 100))

plt.show()

I was inspired by Set y-axis scale for pandas Dataframe Boxplot(), 3 Deviations? but it still not working. The charts are still sharing the same y axes.

The best walkaround I found is to normalize the dataset:

def normalized(df, column_name):
  df[column_name].fillna(value=0, inplace=True)
  df[column_name] = df[column_name] / df[column_name].max()
  return df

That way the y-scale is between 0 and 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM