gpt4 book ai didi

python - 有没有一种简单的方法可以在通过自定义函数(楔形)显示数据的 matplotlib 图上使用对数刻度?

转载 作者:行者123 更新时间:2023-11-28 22:11:28 26 4
gpt4 key购买 nike

我正在使用楔形图绘制数据(同样适用于补丁/圆圈/等)。这很好用,但我想绘制数据对数图。

对于普通地 block ,有

plt.yscale('log')
plt.xscale('log')

但这在这里不起作用,结果是:

ValueError: Data has no positive values, and therefore can not be log-scaled.

我当然可以将我所有的数据转换为日志并相应地调整 xticks 和 yticks,但我想知道是否有 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()

结果:

enter image description here

关于python - 有没有一种简单的方法可以在通过自定义函数(楔形)显示数据的 matplotlib 图上使用对数刻度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55651394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com