簡體   English   中英

是否有一種簡單的方法可以通過自定義函數(楔形)在顯示數據的matplotlib圖上使用對數刻度?

[英]Is there an easy way to use logarithmic scale on matplotlib plots displaying data via a custom function (wedge)?

我正在使用楔子繪制數據(同樣適用於補丁/圓形/等)。 這很好用,但是我想對數繪制數據。

對於普通地塊,有

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

但這在這里不起作用,結果是:

ValueError:數據沒有正值,因此無法進行日志縮放。

我當然可以將所有數據轉換為相應地記錄和調整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()

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM