Python 有没有一种简单的方法可以在matplotlib图上使用对数比例,通过自定义函数(楔形)显示数据?
我使用楔子绘制数据(同样适用于面片/圆/等)。 这工作得很好,但我想绘制对数数据 对于普通地块,有Python 有没有一种简单的方法可以在matplotlib图上使用对数比例,通过自定义函数(楔形)显示数据?,python,matplotlib,plot,logarithm,Python,Matplotlib,Plot,Logarithm,我使用楔子绘制数据(同样适用于面片/圆/等)。 这工作得很好,但我想绘制对数数据 对于普通地块,有 plt.yscale('log') plt.xscale('log') 但这在这里不起作用,结果是: ValueError:数据没有正值,因此无法进行日志缩放 当然,我可以将所有数据转换为日志,并相应地调整xtick和ytick,但我想知道,是否有一种matplotlib自动化方法可以实现这一点 请参见下面我的代码的工作部分: import matplotlib.pylot as plt fro
plt.yscale('log')
plt.xscale('log')
但这在这里不起作用,结果是:
ValueError:数据没有正值,因此无法进行日志缩放
当然,我可以将所有数据转换为日志,并相应地调整xtick和ytick,但我想知道,是否有一种matplotlib自动化方法可以实现这一点
请参见下面我的代码的工作部分:
import matplotlib.pylot as plt
from matplotlib.patches import Wedge
import seaborn as sns
import numpy as np
from matplotlib.patches import Wedge
def dual_half_circle(center, radius, angle=0, ax=None, colors=('w','k'),
**kwargs):
"""
Add two half circles to the axes *ax* (or the current axes) with the
specified facecolors *colors* rotated at *angle* (in degrees).
"""
if ax is None:
ax = plt.gca()
theta1, theta2 = angle, angle + 180
w1 = Wedge(center, radius, theta1, theta2, fc=colors[0], **kwargs)
w2 = Wedge(center, radius, theta2, theta1, fc=colors[1], **kwargs)
for wedge in [w1, w2]:
ax.add_artist(wedge)
return [w1, w2]
fig, ax = plt.subplots(figsize=(30,15))
for i in range(10):
dual_half_circle((100*i, 100*i), radius=10, angle=90, ax=ax,colors=('r','b'))
plt.xlim(0,1000)
plt.ylim(0,1000)
plt.show()
谢谢你的帮助 该错误是由x和y限制引起的。选择一个大于0的值,一切正常
调整代码:
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
def dual_half_circle(center, radius, angle=0, ax=None, colors=('w','k'),
**kwargs):
"""
Add two half circles to the axes *ax* (or the current axes) with the
specified facecolors *colors* rotated at *angle* (in degrees).
"""
if ax is None:
ax = plt.gca()
theta1, theta2 = angle, angle + 180
w1 = Wedge(center, radius, theta1, theta2, fc=colors[0], **kwargs)
w2 = Wedge(center, radius, theta2, theta1, fc=colors[1], **kwargs)
for wedge in [w1, w2]:
ax.add_artist(wedge)
return [w1, w2]
_, ax = plt.subplots(figsize=(30, 15))
for i in range(10):
dual_half_circle((100*i, 100*i), radius=10, angle=90, ax=ax,colors=('r', 'b'))
plt.xlim(1, 1000)
plt.ylim(1, 1000)
plt.xscale('log')
plt.yscale('log')
plt.show()
结果:
没错。你知道我的函数中有什么不同导致了错误吗?相应地编辑了答案:)谢谢,但是ValueError仍然出现。我不认为这是原因可能是matplotlib版本?我使用的是matplotlib 3.03和python 3.73。我假设依赖关系就是问题所在,并接受您的答案,除非有人提出了同样适用于matplotlib 2.x的依赖关系