简体   繁体   中英

matplotlib violin plot weird input requirement

Why is the violin plot in matplotlib demanding non-standard inputs?

minimum non-working example

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(3, 1)
ax[0].plot([3, 4, 5])
ax[1].boxplot([3, 4, 5])
ax[2].violin([3, 4, 5])

produces the first two plots but gives error for the third:

TypeError: 'int' object is not subscriptable

The following commands all produce errors

ax[2].violin([[3, 4, 5]])
ax[2].violin([[3], [4], [5]])
ax[2].violin(np.array([[3, 4, 5]]))
ax[2].violin(np.array([3, 4, 5]))
ax[2].violin([np.array([3, 4, 5])])
ax[2].violin([np.array([[3, 4, 5]])])
ax[2].violin([np.array([[3], [4], [5]])])

The doc simply states:

dataset : Array or a sequence of vectors.
    The input data.

What format must I input for this function and why not accepting standard data vectors?

ax.violin is different from plt.violinplot . From the (actual) doc of ax.violin :

ax.violin(vpstats, positions=None, vert=True, widths=0.5, showmeans=False, showextrema=True, showmedians=False)

(...)

vpstats : list of dicts
  A list of dictionaries containing stats for each violin plot.

See https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violin.html

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