成功解决TypeError: distplot() got an unexpected keyword argument ‘y‘


怕黑小蘑菇
怕黑小蘑菇 2022-09-19 11:36:39 54847
分类专栏: 资讯

成功解决TypeError: distplot() got an unexpected keyword argument 'y'

目录

解决问题

解决思路

解决方法


解决问题

TypeError: distplot() got an unexpected keyword argument 'y'

解决思路

类型错误:distplot()得到了一个意外的关键字参数'y'

解决方法

  1. fg=sns.JointGrid(x=cols[0],y=cols[1],data=data_frame,)
  2. fg.plot_marginals(sns.distplot)

distplot()函数中,只接受一个输入数据,即有x,没有y

  1. def distplot Found at: seaborn.distributions
  2. def distplot(a=None, bins=None, hist=True, kde=True, rug=False, fit=None,
  3. hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None,
  4. color=None, vertical=False, norm_hist=False, axlabel=None,
  5. label=None, ax=None, x=None):
  6. """DEPRECATED: Flexibly plot a univariate distribution of observations.
  7. .. warning::
  8. This function is deprecated and will be removed in a future version.
  9. Please adapt your code to use one of two new functions:
  10. - :func:`displot`, a figure-level function with a similar flexibility
  11. over the kind of plot to draw
  12. - :func:`histplot`, an axes-level function for plotting histograms,
  13. including with kernel density smoothing
  14. This function combines the matplotlib ``hist`` function (with automatic
  15. calculation of a good default bin size) with the seaborn :func:`kdeplot`
  16. and :func:`rugplot` functions. It can also fit ``scipy.stats``
  17. distributions and plot the estimated PDF over the data.
  18. Parameters
  19. ----------
  20. a : Series, 1d-array, or list.
  21. Observed data. If this is a Series object with a ``name`` attribute,
  22. the name will be used to label the data axis.
  23. bins : argument for matplotlib hist(), or None, optional
  24. Specification of hist bins. If unspecified, as reference rule is used
  25. that tries to find a useful default.
  26. hist : bool, optional
  27. Whether to plot a (normed) histogram.
  28. kde : bool, optional
  29. Whether to plot a gaussian kernel density estimate.
  30. rug : bool, optional
  31. Whether to draw a rugplot on the support axis.
  32. fit : random variable object, optional
  33. An object with `fit` method, returning a tuple that can be passed to a
  34. `pdf` method a positional arguments following a grid of values to
  35. evaluate the pdf on.
  36. hist_kws : dict, optional
  37. Keyword arguments for :meth:`matplotlib.axes.Axes.hist`.
  38. kde_kws : dict, optional
  39. Keyword arguments for :func:`kdeplot`.
  40. rug_kws : dict, optional
  41. Keyword arguments for :func:`rugplot`.
  42. color : matplotlib color, optional
  43. Color to plot everything but the fitted curve in.
  44. vertical : bool, optional
  45. If True, observed values are on y-axis.
  46. norm_hist : bool, optional
  47. If True, the histogram height shows a density rather than a count.
  48. This is implied if a KDE or fitted density is plotted.
  49. axlabel : string, False, or None, optional
  50. Name for the support axis label. If None, will try to get it
  51. from a.name if False, do not set a label.
  52. label : string, optional
  53. Legend label for the relevant component of the plot.
  54. ax : matplotlib axis, optional
  55. If provided, plot on this axis.
  56. Returns
  57. -------
  58. ax : matplotlib Axes
  59. Returns the Axes object with the plot for further tweaking.
  60. See Also
  61. --------
  62. kdeplot : Show a univariate or bivariate distribution with a kernel
  63. density estimate.
  64. rugplot : Draw small vertical lines to show each observation in a
  65. distribution.
  66. Examples
  67. --------
  68. Show a default plot with a kernel density estimate and histogram with bin
  69. size determined automatically with a reference rule:
  70. .. plot::
  71. :context: close-figs
  72. >>> import seaborn as sns, numpy as np
  73. >>> sns.set_theme(); np.random.seed(0)
  74. >>> x = np.random.randn(100)
  75. >>> ax = sns.distplot(x)
  76. Use Pandas objects to get an informative axis label:
  77. .. plot::
  78. :context: close-figs
  79. >>> import pandas as pd
  80. >>> x = pd.Series(x, name="x variable")
  81. >>> ax = sns.distplot(x)
  82. Plot the distribution with a kernel density estimate and rug plot:
  83. .. plot::
  84. :context: close-figs
  85. >>> ax = sns.distplot(x, rug=True, hist=False)
  86. Plot the distribution with a histogram and maximum likelihood gaussian
  87. distribution fit:
  88. .. plot::
  89. :context: close-figs
  90. >>> from scipy.stats import norm
  91. >>> ax = sns.distplot(x, fit=norm, kde=False)
  92. Plot the distribution on the vertical axis:
  93. .. plot::
  94. :context: close-figs
  95. >>> ax = sns.distplot(x, vertical=True)
  96. Change the color of all the plot elements:
  97. .. plot::
  98. :context: close-figs
  99. >>> sns.set_color_codes()
  100. >>> ax = sns.distplot(x, color="y")
  101. Pass specific parameters to the underlying plot functions:
  102. .. plot::
  103. :context: close-figs
  104. >>> ax = sns.distplot(x, rug=True, rug_kws={"color": "g"},
  105. ... kde_kws={"color": "k", "lw": 3, "label": "KDE"},
  106. ... hist_kws={"histtype": "step", "linewidth": 3,
  107. ... "alpha": 1, "color": "g"})
  108. """
  109. if kde and not hist:
  110. axes_level_suggestion = "`kdeplot` (an axes-level function for kernel
  111. density plots)."
  112. else:
  113. axes_level_suggestion = "`histplot` (an axes-level function for
  114. histograms)."
  115. msg = "`distplot` is a deprecated function and will be removed in a future
  116. version. "\
  117. "Please adapt your code to use either `displot` (a figure-level function
  118. with "\
  119. "similar flexibility) or " + axes_level_suggestion
  120. warnings.warn(msg, FutureWarning)
  121. if ax is None:
  122. ax = plt.gca()
  123. Intelligently label the support axis
  124. label_ax = bool(axlabel)
  125. if axlabel is None and hasattr(a, "name"):
  126. axlabel = a.name
  127. if axlabel is not None:
  128. label_ax = True
  129. Support new-style API
  130. if x is not None:
  131. a = x
  132. Make a a 1-d float array
  133. a = np.asarray(a, float)
  134. if a.ndim > 1:
  135. a = a.squeeze()
  136. Drop null values from array
  137. a = remove_na(a)
  138. Decide if the hist is normed
  139. norm_hist = norm_hist or kde or fit is not None
  140. Handle dictionary defaults
  141. hist_kws = {} if hist_kws is None else hist_kws.copy()
  142. kde_kws = {} if kde_kws is None else kde_kws.copy()
  143. rug_kws = {} if rug_kws is None else rug_kws.copy()
  144. fit_kws = {} if fit_kws is None else fit_kws.copy()
  145. Get the color from the current color cycle
  146. if color is None:
  147. if vertical:
  148. line, = ax.plot(0, a.mean())
  149. else:
  150. line, = ax.plot(a.mean(), 0)
  151. color = line.get_color()
  152. line.remove()
  153. Plug the label into the right kwarg dictionary
  154. if label is not None:
  155. if hist:
  156. hist_kws["label"] = label
  157. elif kde:
  158. kde_kws["label"] = label
  159. elif rug:
  160. rug_kws["label"] = label
  161. elif fit:
  162. fit_kws["label"] = label
  163. if hist:
  164. if bins is None:
  165. bins = min(_freedman_diaconis_bins(a), 50)
  166. hist_kws.setdefault("alpha", 0.4)
  167. hist_kws.setdefault("density", norm_hist)
  168. orientation = "horizontal" if vertical else "vertical"
  169. hist_color = hist_kws.pop("color", color)
  170. ax.hist(a, bins, orientation=orientation, color=hist_color, **hist_kws)
  171. if hist_color != color:
  172. hist_kws["color"] = hist_color
  173. if kde:
  174. kde_color = kde_kws.pop("color", color)
  175. kdeplot(a, vertical=vertical, ax=ax, color=kde_color, **kde_kws)
  176. if kde_color != color:
  177. kde_kws["color"] = kde_color
  178. if rug:
  179. rug_color = rug_kws.pop("color", color)
  180. axis = "y" if vertical else "x"
  181. rugplot(a, axis=axis, ax=ax, color=rug_color, **rug_kws)
  182. if rug_color != color:
  183. rug_kws["color"] = rug_color
  184. if fit is not None:
  185. def pdf(x):
  186. return fit.pdf(x, *params)
  187. fit_color = fit_kws.pop("color", "282828")
  188. gridsize = fit_kws.pop("gridsize", 200)
  189. cut = fit_kws.pop("cut", 3)
  190. clip = fit_kws.pop("clip", (-np.inf, np.inf))
  191. bw = stats.gaussian_kde(a).scotts_factor() * a.std(ddof=1)
  192. x = _kde_support(a, bw, gridsize, cut, clip)
  193. params = fit.fit(a)
  194. y = pdf(x)
  195. if vertical:
  196. x, y = y, x
  197. ax.plot(x, y, color=fit_color, **fit_kws)
  198. if fit_color != "282828":
  199. fit_kws["color"] = fit_color
  200. if label_ax:
  201. if vertical:
  202. ax.set_ylabel(axlabel)
  203. else:
  204. ax.set_xlabel(axlabel)
  205. return ax

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=2104
赞同 0
评论 0 条
怕黑小蘑菇L0
粉丝 0 发表 11 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2944
【软件正版化】软件正版化工作要点  2863
统信UOS试玩黑神话:悟空  2823
信刻光盘安全隔离与信息交换系统  2717
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1251
grub引导程序无法找到指定设备和分区  1217
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  163
点击报名 | 京东2025校招进校行程预告  162
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  160
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  157
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!