简体   繁体   中英

How to change Label format in Violin plot of Matplotlib?

I am plotting some data in the violin plot I want to keep the label format the same for all plots. This below is the first plot:

在此处输入图像描述

I want the following given plot with the same formatted y-axis. (I want to add a scientific notation to the figure given below.) I want the exponential part common. I searched a lot but could not get the solution. Please Help.

在此处输入图像描述

If you want to add the scientific notation, you can use plt.ticklabel_format()

Consider the following sample data and initial violin plot.

x = sorted([1,2,3,4,5] * 200)
y = (list(np.linspace(0.3 * 1e-3, 3 * 1e-4, 70)) + [0] * 130) * 2 + \
    list(np.linspace(0 * 1e-3, 1 * 1e-4, 200)) + \
    list(np.linspace(0 * 1e-3, 1.3 * 1e-4, 200)) * 2

df = pd.DataFrame({'x':x, 'y':y})

sns.violinplot(x='x',y='y',data=df)

在此处输入图像描述

By using plt.ticklabel_format() the code and result will look like this:

sns.violinplot(x='x',y='y',data=df)

plt.ticklabel_format(style='sci', axis='y', scilimits=(3,10))

在此处输入图像描述

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