(切片(无,无,无),无),在matplotlib图中
我想绘制x轴由“ 0-5”,“ 5-10”等间隔组成的图。 ,11 Intervals)映射X标签。但是,在下一步PLT.PLOT(CORREX,11 Intervals)中,有一个错误警告ME (slice>(none,none,none,none),无),无)
。
我在网上搜索以获得帮助,最多的答案建议使用ILOC/LOC来修复它。但是我的变量类型中有numpy,甚至将其更改为DataFrame根本没有帮助。如何解决这个问题?
以下是我的代码和数据:
def DateCalculate():
Xindex = ["0-5", "5-10", "10-15", "15-20", "20-25", "25-30", "30-50", "50-70", "70-100", "100-150", "150-200"]
correX = np.array(range(0, 11))
plt.xticks(correX ,Xindex)
plt.plot(correX, res)
plt.show()
可变res中的数据:
0
(-0.001, 5.0] 58
(5.0, 10.0] 46
(10.0, 15.0] 12
(15.0, 20.0] 9
(20.0, 25.0] 7
(25.0, 30.0] 5
(30.0, 50.0] 15
(50.0, 70.0] 7
(70.0, 100.0] 13
(100.0, 150.0] 12
(150.0, 200.0] 13
[11 rows x 1 columns]
type is DataFrame
以下是跟踪代码:
exception has occurred: KeyError (note: full exception trace is shown but execution is paused at: _run_module_as_main)
(slice(None, None, None), None)
File "D:\pycharm\python3.10\Lib\site-packages\pandas\core\indexes\range.py", line 388, in get_loc
raise KeyError(key)
File "D:\pycharm\python3.10\Lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\cbook\__init__.py", line 1327, in _check_1d
ndim = x[:, None].ndim
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\axes\_base.py", line 488, in _plot_args
y = _check_1d(xy[1])
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\axes\_base.py", line 312, in __call__
yield from self._plot_args(this, kwargs)
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\axes\_axes.py", line 1632, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\pyplot.py", line 2757, in plot
return gca().plot(
File "D:\pycharm\simulate\python_simulate\PolyHack\DataFasterRead.py", line 34, in DateCalculate
plt.plot(correX, res)
File "D:\pycharm\simulate\python_simulate\PolyHack\DataFasterRead.py", line 37, in <module>
DateCalculate()
这是代码停止的地方:
@doc(Int64Index.get_loc)
def get_loc(self, key, method=None, tolerance=None):
if method is None and tolerance is None:
if is_integer(key) or (is_float(key) and key.is_integer()):
new_key = int(key)
try:
return self._range.index(new_key)
except ValueError as err:
raise KeyError(key) from err
raise KeyError(key) # <-- (bug occurred in here)
return super().get_loc(key, method=method, tolerance=tolerance)
I want to draw a plot in which the x-axis consists of intervals, like "0-5", "5-10" etc. I have 11 intervals, so I use plt.xticks(np.array(0, 11), 11Intervals) to map the x labels. However in next step plt.plot(correX, 11Intervals), there is an error warning me (slice(None, None, None), None)
.
I search on the web to get help, and the most answer suggests using iloc/loc to fix it. But my variables type in there is Numpy, even changing it to DataFrame doesn't help at all. How to solve this problem?
Below are my codes and data:
def DateCalculate():
Xindex = ["0-5", "5-10", "10-15", "15-20", "20-25", "25-30", "30-50", "50-70", "70-100", "100-150", "150-200"]
correX = np.array(range(0, 11))
plt.xticks(correX ,Xindex)
plt.plot(correX, res)
plt.show()
data in variable res:
0
(-0.001, 5.0] 58
(5.0, 10.0] 46
(10.0, 15.0] 12
(15.0, 20.0] 9
(20.0, 25.0] 7
(25.0, 30.0] 5
(30.0, 50.0] 15
(50.0, 70.0] 7
(70.0, 100.0] 13
(100.0, 150.0] 12
(150.0, 200.0] 13
[11 rows x 1 columns]
type is DataFrame
Here are the trace codes:
exception has occurred: KeyError (note: full exception trace is shown but execution is paused at: _run_module_as_main)
(slice(None, None, None), None)
File "D:\pycharm\python3.10\Lib\site-packages\pandas\core\indexes\range.py", line 388, in get_loc
raise KeyError(key)
File "D:\pycharm\python3.10\Lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\cbook\__init__.py", line 1327, in _check_1d
ndim = x[:, None].ndim
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\axes\_base.py", line 488, in _plot_args
y = _check_1d(xy[1])
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\axes\_base.py", line 312, in __call__
yield from self._plot_args(this, kwargs)
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\axes\_axes.py", line 1632, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "D:\pycharm\python3.10\Lib\site-packages\matplotlib\pyplot.py", line 2757, in plot
return gca().plot(
File "D:\pycharm\simulate\python_simulate\PolyHack\DataFasterRead.py", line 34, in DateCalculate
plt.plot(correX, res)
File "D:\pycharm\simulate\python_simulate\PolyHack\DataFasterRead.py", line 37, in <module>
DateCalculate()
And this is where codes stopped:
@doc(Int64Index.get_loc)
def get_loc(self, key, method=None, tolerance=None):
if method is None and tolerance is None:
if is_integer(key) or (is_float(key) and key.is_integer()):
new_key = int(key)
try:
return self._range.index(new_key)
except ValueError as err:
raise KeyError(key) from err
raise KeyError(key) # <-- (bug occurred in here)
return super().get_loc(key, method=method, tolerance=tolerance)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论