如何在对数刻度的Kivy图上显示刻度线?

时间:2019-11-02 01:18:28

标签: python graph kivy

如果使用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类的方式有问题吗?

1 个答案:

答案 0 :(得分:1)

很难找到文档,但是在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的文档说:

  

请参阅: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
相关问题