如何在对数比例的Kivy图上显示记号?

2024-04-26 07:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我使用Kivy Garden graph绘制一个线性图,我可以显示x和y记号/标签。如果轴是对数标度的,我就找不到给它贴标签的方法

我有一个MWE,它在y-线性和y-对数图上绘制一个函数,但是y轴标签不会显示在对数图上

from kivy.lang import Builder
from kivy.garden.graph import Graph, MeshLinePlot
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.properties import BooleanProperty
import math
Builder.load_string("""
<MyLayout>:
    orientation: 'vertical'
    spacing: 10
    LinearGraph:
    LogGraph:
""")

class MyLayout(BoxLayout):
    pass

class LinearGraph(Graph):
    def __init__(self,**kwargs):
        super(LinearGraph, self).__init__(**kwargs)
        self.xlabel='X'
        self.ylabel='Y'
        self.x_ticks_major=25
        self.x_ticks_minor=5
        self.x_grid_label=True
        self.y_ticks_major=1
        self.y_grid_label=True
        self.xmin=0
        self.xmax=100
        self.ymin=0.1
        self.ymax=10
        self.ylog=False
        self.x_grid=True
        self.y_grid=True
        self.plot=MeshLinePlot(color=[1,1,1,1])
        self.add_plot(self.plot)
        self.plot.points=[(x, math.sin(x / 10.)+2) for x in range(0, 101)]

class LogGraph(Graph):
    def __init__(self,**kwargs):
        super(LogGraph, self).__init__(**kwargs)
        self.xlabel='X'
        self.ylabel='Y'
        self.x_ticks_major=25
        self.x_ticks_minor=5
        self.x_grid_label=True
        self.y_ticks_major=1
        self.y_grid_label=True
        self.xmin=0
        self.xmax=100
        self.ymin=0.1
        self.ymax=10
        self.ylog=True
        self.x_grid=True
        self.y_grid=True
        self.plot=MeshLinePlot(color=[1,1,1,1])
        self.add_plot(self.plot)
        self.plot.points=[(x, math.sin(x / 10.)+2) for x in range(0, 101)]


class MainscreenApp(App):
    def build(self):
        return MyLayout()

if __name__=="__main__":
    MainscreenApp().run()

我不希望滴答声消失——我如何使用Graph类有问题吗


Tags: fromimportselftrueplotinit对数label
1条回答
网友
1楼 · 发布于 2024-04-26 07:22:42

很难找到文档,但是在kivy.garden.graph的代码中,它说:

x_ticks_major = BoundedNumericProperty(0, min=0)
'''Distance between major tick marks on the x-axis.

Determines the distance between the major tick marks. Major tick marks
start from min and re-occur at every ticks_major until :data:`xmax`.
If :data:`xmax` doesn't overlap with a integer multiple of ticks_major,
no tick will occur at :data:`xmax`. Zero indicates no tick marks.

If :data:`xlog` is true, then this indicates the distance between ticks
in multiples of current decade. E.g. if :data:`xmin` is 0.1 and
ticks_major is 0.1, it means there will be a tick at every 10th of the
decade, i.e. 0.1 ... 0.9, 1, 2... If it is 0.3, the ticks will occur at
0.1, 0.3, 0.6, 0.9, 2, 5, 8, 10. You'll notice that it went from 8 to 10
instead of to 20, that's so that we can say 0.5 and have ticks at every
half decade, e.g. 0.1, 0.5, 1, 5, 10, 50... Similarly, if ticks_major is
1.5, there will be ticks at 0.1, 5, 100, 5,000... Also notice, that there's
always a major tick at the start. Finally, if e.g. :data:`xmin` is 0.6
and this 0.5 there will be ticks at 0.6, 1, 5...

y_ticks_major的文档说:

See :data:x_ticks_major

请注意,如果它是log标度,则y_ticks_major的值不是直观的

可以使用以下值(例如)获取Y轴上的记号、标签和网格:

    self.y_grid_label=True
    self.ymin=0.1
    self.ymax=10
    self.ylog=True
    self.y_grid=True 
    self.y_ticks_major=0.25
    self.y_ticks_minor=5

相关问题 更多 >