用python画尺子;对数坐标尺绘制;matplotlib坐标轴多重刻度

提供一个思路那就是 用 twiny方法,把多个x轴放在一起,就可以实现多重刻度

效果图如下:用matplotlib画了一个对数尺子;-) 某种意义上而言是x轴的三重刻度了。

用我的源码改一改可以画任意的尺子(话说也没有多少人有画尺子的需求吧),或者有画多重刻度图表需求的小伙伴们可以参考一下,

源码写得很乱,大家凑合看,看不懂在下面评论问我,随缘解答。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator
import pandas as pd

# 指定 Excel 文件的路径
file_path = '对数尺表.xlsx'

# 使用 read_excel 函数读取数据
data = pd.read_excel(file_path,dtype=np.float32)

# 显示数据的前几行
distance  = data['假值(尺子上的distance)'].values

# 创建数据
x1 = np.linspace(0, 10, 100)
y = x1-x1

# 创建图形和主Axes
fig, ax1 = plt.subplots(figsize=(20, 1))

# 主X轴
ax1.plot(x1, y,linewidth=0)
  
# 创建第二个X轴
ax2 = ax1.twiny()  # 创建与ax1共享Y轴的新Axes
x2 = x1  # 为了演示,我们用2倍的x1
ax2.plot(x2, y,linewidth=0 )
 
# 创建第三个X轴
ax3 = ax1.twiny()  # 创建与ax1共享Y轴的新Axes
x3 = x1  # 为了演示,我们用0.5倍的x1
ax3.plot(x3, y,linewidth=0 )

ax4 = ax1.twiny()  # 创建与ax1共享Y轴的新Axes
x4 = x1  # 为了演示,我们用0.5倍的x1
ax4.plot(x4, y,linewidth=0 )

# 设置X轴的位置
ax1.xaxis.tick_bottom()  # 显示主X轴的刻度
ax2.xaxis.tick_bottom()  # 显示第二个X轴的刻度
ax3.xaxis.tick_bottom()  # 显示第三个X轴的刻度
ax4.xaxis.tick_bottom()

major_tick_locations = distance*10
major_locator = FixedLocator(major_tick_locations)
ax1.xaxis.set_major_locator(major_locator)
# ax1.set_xticks(major_tick_locations,labels1)
ax1.tick_params(which='both', length=12, width=0.4, color='black', direction='in')
ax1.set_xticklabels([])


# [1,2,3,4,5,6,7,8,9,10]
index2 = data['真值(尺子上的刻度)'].isin([1,2,3,4,5,6,7,8,9,10])
distance2 = data['假值(尺子上的distance)'].values[index2]
labels2 = ['1','2','3','4','5','6','7','8','9','10']
major_tick_locations2 = distance2*10
major_locator2 = FixedLocator(major_tick_locations2)
ax2.xaxis.set_major_locator(major_locator2)
ax2.set_xticks(major_tick_locations2,labels2)
ax2.tick_params(which='both', length=20, width=0.6, color='black', direction='in',
                pad=-32,labelsize=14)


distanceNumner = np.array([1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,
                           2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,]
    ,dtype=np.float32)
index3 = data['真值(尺子上的刻度)'].isin(distanceNumner)
distance3 = data['假值(尺子上的distance)'].values[index3]
labels3 =  [dis[-1] for dis in distanceNumner.astype(str)  ]
major_tick_locations3 =distance3*10
major_locator3 = FixedLocator(major_tick_locations3)
ax3.xaxis.set_major_locator(major_locator3)
ax3.set_xticks(major_tick_locations3,labels3)
ax3.tick_params(which='both', length=15, width=0.6, color='black', direction='in',pad=-22)
# 设置图例



distanceNumner4 = np.array([1.05,1.15,1.25,1.35,
                            2.1,2.2,2.3,2.4,2.6,2.7,2.8,2.9,
                            3.1,3.2,3.3,3.4,3.6,3.7,3.8,3.9,
                            4.1,4.2,4.3,4.4,4.6,4.7,4.8,4.9,]
                           ,dtype=np.float32)

index4 = data['真值(尺子上的刻度)'].isin(distanceNumner4)
distance4 = data['假值(尺子上的distance)'].values[index4]
major_tick_locations4 =distance4*10
major_locator4 = FixedLocator(major_tick_locations4)
ax4.xaxis.set_major_locator(major_locator4)
ax4.tick_params(which='both', length=15, width=0.6, color='black', direction='in')
ax4.set_xticklabels([])



ax1.set_yticklabels([])
ax1.spines['top'].set_visible(False)


ax1.spines['bottom'].set_visible(False)
ax2.spines['bottom'].set_visible(False)
ax3.spines['bottom'].set_visible(False)
ax4.spines['bottom'].set_visible(False)


ax1.set_yticks([])
ax1.set_yticklabels([])

# 隐藏 轴线条
for ax_i in [ax1,ax2,ax3,ax4]:
    for spine in ax_i.spines.values():
        spine.set_visible(False)

plt.subplots_adjust(bottom=0)


# 保存为SVG文件
plt.savefig('rular.svg', format='svg',transparent=True)

plt.imshow

### 回答1: 可以使用 Python 的 `matplotlib` 库来出心形。具体的实现方法如下: 1. 首先需要导入 `matplotlib.pyplot` 库,并使用 `figure` 函数创建一个新图形。 2. 然后使用 `plot` 函数绘制出心形的曲线。可以使用参数 `t` 控制心形的形状。可以算出心形的两个数学函数,x=16sin^3(t),y=13cos(t)-5cos(2t)-2cos(3t)-cos(4t) 3. 使用 `show` 函数显示图形。 下面是一个示例代码: ``` import numpy as np import matplotlib.pyplot as plt t = np.linspace(0, 2 * np.pi, 1000) x = 16 * np.power(np.sin(t), 3) y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t) plt.plot(x, y) plt.axis("equal") # 设置坐标轴比例 plt.show() ``` 运行上述代码后,会弹出一个新窗口,显示出一个心形。 ### 回答2: 要用Python一个心形,可以使用turtle库来实现。下面是一个简单的代码示例: ```python import turtle # 设置布的尺寸 turtle.setup(800, 600) # 设置笔的颜色和粗细 turtle.pensize(5) turtle.pencolor("red") # 绘制左侧的半个心形 turtle.left(45) turtle.circle(100, 180) # 绘制右侧的半个心形 turtle.seth(45) turtle.circle(100, 180) # 隐藏笔 turtle.hideturtle() # 结束绘制 turtle.done() ``` 运行以上代码,就可以在一个布上看到用Python绘制的一个心形图案。这段代码使用了turtle库的一些基本函数,比如`setup()`用于设置布的尺寸,`pensize()`设置笔的粗细,`pencolor()`设置笔的颜色,`left()`设置旋转角度,`circle()`用于绘制圆弧,`hideturtle()`隐藏笔,`done()`结束绘制。通过适当的调整参数和初始角度,可以出不同尺寸和形态的心形图案。 ### 回答3: 可以使用turtle模块来使用Python一个心形,代码如下: ```python import turtle def draw_heart(): turtle.speed(5) turtle.penup() turtle.goto(0, -100) # 移动到图的起始位置 turtle.pendown() turtle.begin_fill() turtle.left(140) # 旋转角度 turtle.forward(224) # 前进 curve() turtle.left(120) curve() turtle.forward(224) turtle.end_fill() turtle.hideturtle() # 隐藏海龟图标 def curve(): for i in range(200): turtle.right(1) # 右转角度 turtle.forward(1) # 前进 turtle.setup(600, 400) # 设置窗口大小 turtle.color('red') # 设置笔颜色 turtle.fillcolor('pink') # 设置填充颜色 draw_heart() turtle.done() ``` 这段代码使用turtle模块来绘制一个心形图案。首先,我们导入turtle模块。然后定义了一个函数`draw_heart()`来心形。在函数内部,我们设置了笔的速度,然后将笔移动到图的起始位置,后续使用`pendown()`方法开始绘制。我们使用`begin_fill()`和`end_fill()`方法来进行填充,填充颜色为粉色。然后,我们通过一系列的移动和旋转来绘制两个弧线,形成了一个心形图案。最后,使用`hideturtle()`方法隐藏海龟图标。接下来,我们设置了窗口的大小,笔的颜色以及填充颜色,并调用`draw_heart()`函数来绘制心形图案。最后,使用`turtle.done()`来保持窗口的显示。 运行代码后,就可以在窗口中看到绘制出的心形图案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值