Skip to content

Commit

Permalink
* added pandas/matplotlib output
Browse files Browse the repository at this point in the history
  • Loading branch information
o-murphy committed Oct 9, 2023
1 parent f40b987 commit 6d7eab9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py_ballisticcalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__copyright__ = ("",)

__credits__ = ["o-murphy"]
__version__ = "1.1.0b6"
__version__ = "1.1.0b7"


from .drag_model import * # pylint: disable=import-error
Expand Down
59 changes: 59 additions & 0 deletions py_ballisticcalc/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,62 @@ def danger_space(trajectory: TrajectoryData, target_height: [float, Distance]) -
else Set.Units.target_height(target_height)) >> Distance.Yard
traj_angle_tan = math.tan(trajectory.angle >> Angular.Radian)
return Distance.Yard(-(target_height / traj_angle_tan))

@staticmethod
def to_dataframe(trajectory: list[TrajectoryData]):
import pandas as pd
col_names = TrajectoryData._fields
trajectory = [p.in_def_units() for p in trajectory]
return pd.DataFrame(trajectory, columns=col_names)

def show_plot(self, shot, winds):
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
self._calc = TrajectoryCalc(self.ammo)
# self.update_elevation()
data = self._calc.trajectory(self.weapon, self.zero_atmo, shot, winds) # Step in 10-yard increments to produce smoother curves
df = self.to_dataframe(data)
ax = df.plot(x='distance', y=['drop'], ylabel=Set.Units.drop.symbol)

# zero_d = self.weapon.zero_distance >> Set.Units.distance
# zero = ax.plot([zero_d, zero_d], [df['drop'].min(), df['drop'].max()], linestyle='--')

try:
zero_g = self.zero_given_elevation(shot, winds)

for p in zero_g:
ax.plot([p.distance >> Set.Units.distance, p.distance >> Set.Units.distance],
[df['drop'].min(), p.drop >> Set.Units.drop], linestyle=':')
except ArithmeticError:
pass

sh = self.weapon.sight_height >> Set.Units.drop

# # scope line
x_values = [0, df.distance.max()] # Adjust these as needed
# y_values = [sh, sh - df.distance.max() * math.tan(self.elevation >> Angular.Degree)] # Adjust these as needed
y_values = [0, 0] # Adjust these as needed
ax.plot(x_values, y_values, linestyle='--', label='scope line')

# # # barrel line
# elevation = self.elevation >> Angular.Degree
#
# y = sh / math.cos(elevation)
# x0 = sh / math.sin(elevation)
# x1 = sh * math.sin(elevation)
# x_values = [0, x0]
# y_values = [-sh, 0]
# ax.plot(x_values, y_values, linestyle='-.', label='barrel line')

df.plot(x='distance', xlabel=Set.Units.distance.symbol,
y=['velocity'], ylabel=Set.Units.velocity.symbol,
secondary_y=True,
ylim=[0, df['velocity'].max()], ax=ax)
plt.title = f"{self.weapon.sight_height} {self.weapon.zero_distance}"

plt.show()
# ax = df.plot(y=[c.tableCols['Drop'][0]], ylabel=UNIT_DISPLAY[c.heightUnits].units)
# df.plot(y=[c.tableCols['Velocity'][0]], ylabel=UNIT_DISPLAY[c.bullet.velocityUnits].units, secondary_y=True,
# ylim=[0, df[c.tableCols['Velocity'][0]].max()], ax=ax)
# plt.show()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ version = {attr = "py_ballisticcalc.__version__"}
[project.optional-dependencies]
dev = ['Cython', 'setuptools']
lint = ['pylint', 'flake8']
plot = ['matplotlib', 'pandas']
15 changes: 15 additions & 0 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pyximport; pyximport.install(language_level=3)
from py_ballisticcalc import *

Settings.Units.velocity = Velocity.MPS

dm = DragModel(0.22, TableG7, 168, 0.308)
ammo = Ammo(dm, 1.22, Velocity(2600, Velocity.FPS))
atmosphere = Atmo.icao()
weapon = Weapon(3, 100, 11.24)

calc = Calculator(weapon, ammo, atmosphere)
calc.update_elevation()
shot = Shot(1200, Distance.Foot(0.2), sight_angle=calc.elevation, shot_angle=Angular.Mil(0))

calc.show_plot(shot, [Wind()])

0 comments on commit 6d7eab9

Please sign in to comment.