Matplotlib:如何在对数对数图中绘制y = -x?

时间:2019-09-06 11:06:19

标签: python matplotlib

我正在用Matplotlib绘制对数图以利用相关性 在某些节点之间使用它们的分类性。

我已经成功绘制了y = x线,但是现在我正在努力解决y = -x。

这是我正在使用的代码:

ValueError                                Traceback (most recent call last)
<ipython-input-22-baae0fe41359> in <module>
     24 ]).agg({
     25     'net_tons': sum,
---> 26     'cost_eur': sum
     27 }).reset_index()
     28 

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, arg, *args, **kwargs)
   1453     @Appender(_shared_docs["aggregate"])
   1454     def aggregate(self, arg=None, *args, **kwargs):
-> 1455         return super().aggregate(arg, *args, **kwargs)
   1456 
   1457     agg = aggregate

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, func, *args, **kwargs)
    227         func = _maybe_mangle_lambdas(func)
    228 
--> 229         result, how = self._aggregate(func, _level=_level, *args, **kwargs)
    230         if how is None:
    231             return result

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
    504 
    505                 try:
--> 506                     result = _agg(arg, _agg_1dim)
    507                 except SpecificationError:
    508 

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\base.py in _agg(arg, func)
    454                 result = OrderedDict()
    455                 for fname, agg_how in arg.items():
--> 456                     result[fname] = func(fname, agg_how)
    457                 return result
    458 

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\base.py in _agg_1dim(name, how, subset)
    438                         "nested dictionary is ambiguous " "in aggregation"
    439                     )
--> 440                 return colg.aggregate(how, _level=(_level or 0) + 1)
    441 
    442             def _agg_2dim(name, how):

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, func_or_funcs, *args, **kwargs)
    855             cyfunc = self._is_cython_func(func_or_funcs)
    856             if cyfunc and not args and not kwargs:
--> 857                 return getattr(self, cyfunc)()
    858 
    859             if self.grouper.nkeys > 1:

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\groupby.py in f(self, **kwargs)
   1375 
   1376                 # apply a non-cython aggregation
-> 1377                 result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
   1378 
   1379                 # coerce the resulting columns if we can

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, func_or_funcs, *args, **kwargs)
    858 
    859             if self.grouper.nkeys > 1:
--> 860                 return self._python_agg_general(func_or_funcs, *args, **kwargs)
    861 
    862             try:

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\groupby.py in _python_agg_general(self, func, *args, **kwargs)
    918                 output[name] = self._try_cast(values[mask], result)
    919 
--> 920         return self._wrap_aggregated_output(output)
    921 
    922     def _wrap_applied_output(self, *args, **kwargs):

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\generic.py in _wrap_aggregated_output(self, output, names)
    953             output=output, index=self.grouper.result_index, names=names
    954         )
--> 955         return self._reindex_output(result)._convert(datetime=True)
    956 
    957     def _wrap_transformed_output(self, output, names=None):

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\groupby\groupby.py in _reindex_output(self, output)
   2469         levels_list = [ping.group_index for ping in groupings]
   2470         index, _ = MultiIndex.from_product(
-> 2471             levels_list, names=self.grouper.names
   2472         ).sortlevel()
   2473 

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\indexes\multi.py in from_product(cls, iterables, sortorder, names)
    537 
    538         codes, levels = _factorize_from_iterables(iterables)
--> 539         codes = cartesian_product(codes)
    540         return MultiIndex(levels, codes, sortorder=sortorder, names=names)
    541 

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\reshape\util.py in cartesian_product(X)
     56             np.repeat(np.asarray(com.values_from_object(x)), b[i]), np.product(a[i])
     57         )
---> 58         for i, x in enumerate(X)
     59     ]

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\pandas\core\reshape\util.py in <listcomp>(.0)
     56             np.repeat(np.asarray(com.values_from_object(x)), b[i]), np.product(a[i])
     57         )
---> 58         for i, x in enumerate(X)
     59     ]

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\numpy\core\fromnumeric.py in repeat(a, repeats, axis)
    469 
    470     """
--> 471     return _wrapfunc(a, 'repeat', repeats, axis=axis)
    472 
    473 

~\AppData\Local\Continuum\miniconda3\envs\jose\lib\site-packages\numpy\core\fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
     54 def _wrapfunc(obj, method, *args, **kwds):
     55     try:
---> 56         return getattr(obj, method)(*args, **kwds)
     57 
     58     # An AttributeError occurs if the object does not have

ValueError: negative dimensions are not allowed
  • 这是我要显示的最后一个情节:
    • 红线是我从粘贴的代码中得到的
    • 我需要蓝线

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以尝试

plt.plot(x, .35*1500/x, ":", color="b")

答案 1 :(得分:2)

您不能在对数对数图中绘制y = -x(对于正x值),因为您无法在其中绘制负值。您显示的蓝线也不会表示负线性关系(例如y = -mx + n),因为在对数对数图中它看起来不是笔直的。您可以像这样向下获得“直线”线:

import numpy as np
import matplotlib.pyplot as plt

plt.xscale("log")
plt.yscale("log")
plt.grid()
x = np.arange(0, 1500)
k = 0.35
plt.plot(x, k * x, ":", color="r")
plt.plot(x, np.exp(-np.log(x) + np.log(x[-1] * k)), color="b")
plt.title("Assortativity")
plt.show()

输出:

Plot