Spaces:
Sleeping
Sleeping
File size: 48,097 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 |
import os
from tempfile import TemporaryDirectory
import pytest
from sympy.concrete.summations import Sum
from sympy.core.numbers import (I, oo, pi)
from sympy.core.relational import Ne
from sympy.core.symbol import Symbol, symbols
from sympy.functions.elementary.exponential import (LambertW, exp, exp_polar, log)
from sympy.functions.elementary.miscellaneous import (real_root, sqrt)
from sympy.functions.elementary.piecewise import Piecewise
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.functions.elementary.miscellaneous import Min
from sympy.functions.special.hyper import meijerg
from sympy.integrals.integrals import Integral
from sympy.logic.boolalg import And
from sympy.core.singleton import S
from sympy.core.sympify import sympify
from sympy.external import import_module
from sympy.plotting.plot import (
Plot, plot, plot_parametric, plot3d_parametric_line, plot3d,
plot3d_parametric_surface)
from sympy.plotting.plot import (
unset_show, plot_contour, PlotGrid, MatplotlibBackend, TextBackend)
from sympy.plotting.series import (
LineOver1DRangeSeries, Parametric2DLineSeries, Parametric3DLineSeries,
ParametricSurfaceSeries, SurfaceOver2DRangeSeries)
from sympy.testing.pytest import skip, warns, raises, warns_deprecated_sympy
from sympy.utilities import lambdify as lambdify_
from sympy.utilities.exceptions import ignore_warnings
unset_show()
matplotlib = import_module(
'matplotlib', min_module_version='1.1.0', catch=(RuntimeError,))
class DummyBackendNotOk(Plot):
""" Used to verify if users can create their own backends.
This backend is meant to raise NotImplementedError for methods `show`,
`save`, `close`.
"""
def __new__(cls, *args, **kwargs):
return object.__new__(cls)
class DummyBackendOk(Plot):
""" Used to verify if users can create their own backends.
This backend is meant to pass all tests.
"""
def __new__(cls, *args, **kwargs):
return object.__new__(cls)
def show(self):
pass
def save(self):
pass
def close(self):
pass
def test_basic_plotting_backend():
x = Symbol('x')
plot(x, (x, 0, 3), backend='text')
plot(x**2 + 1, (x, 0, 3), backend='text')
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot_and_save_1(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
y = Symbol('y')
with TemporaryDirectory(prefix='sympy_') as tmpdir:
###
# Examples from the 'introduction' notebook
###
p = plot(x, legend=True, label='f1', adaptive=adaptive, n=10)
p = plot(x*sin(x), x*cos(x), label='f2', adaptive=adaptive, n=10)
p.extend(p)
p[0].line_color = lambda a: a
p[1].line_color = 'b'
p.title = 'Big title'
p.xlabel = 'the x axis'
p[1].label = 'straight line'
p.legend = True
p.aspect_ratio = (1, 1)
p.xlim = (-15, 20)
filename = 'test_basic_options_and_colors.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p.extend(plot(x + 1, adaptive=adaptive, n=10))
p.append(plot(x + 3, x**2, adaptive=adaptive, n=10)[1])
filename = 'test_plot_extend_append.png'
p.save(os.path.join(tmpdir, filename))
p[2] = plot(x**2, (x, -2, 3), adaptive=adaptive, n=10)
filename = 'test_plot_setitem.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot(sin(x), (x, -2*pi, 4*pi), adaptive=adaptive, n=10)
filename = 'test_line_explicit.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot(sin(x), adaptive=adaptive, n=10)
filename = 'test_line_default_range.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3)), adaptive=adaptive, n=10)
filename = 'test_line_multiple_range.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
raises(ValueError, lambda: plot(x, y))
#Piecewise plots
p = plot(Piecewise((1, x > 0), (0, True)), (x, -1, 1), adaptive=adaptive, n=10)
filename = 'test_plot_piecewise.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot(Piecewise((x, x < 1), (x**2, True)), (x, -3, 3), adaptive=adaptive, n=10)
filename = 'test_plot_piecewise_2.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# test issue 7471
p1 = plot(x, adaptive=adaptive, n=10)
p2 = plot(3, adaptive=adaptive, n=10)
p1.extend(p2)
filename = 'test_horizontal_line.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# test issue 10925
f = Piecewise((-1, x < -1), (x, And(-1 <= x, x < 0)), \
(x**2, And(0 <= x, x < 1)), (x**3, x >= 1))
p = plot(f, (x, -3, 3), adaptive=adaptive, n=10)
filename = 'test_plot_piecewise_3.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot_and_save_2(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
with TemporaryDirectory(prefix='sympy_') as tmpdir:
#parametric 2d plots.
#Single plot with default range.
p = plot_parametric(sin(x), cos(x), adaptive=adaptive, n=10)
filename = 'test_parametric.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
#Single plot with range.
p = plot_parametric(
sin(x), cos(x), (x, -5, 5), legend=True, label='parametric_plot',
adaptive=adaptive, n=10)
filename = 'test_parametric_range.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
#Multiple plots with same range.
p = plot_parametric((sin(x), cos(x)), (x, sin(x)),
adaptive=adaptive, n=10)
filename = 'test_parametric_multiple.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
#Multiple plots with different ranges.
p = plot_parametric(
(sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5)),
adaptive=adaptive, n=10)
filename = 'test_parametric_multiple_ranges.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
#depth of recursion specified.
p = plot_parametric(x, sin(x), depth=13,
adaptive=adaptive, n=10)
filename = 'test_recursion_depth.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
#No adaptive sampling.
p = plot_parametric(cos(x), sin(x), adaptive=False, n=500)
filename = 'test_adaptive.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
#3d parametric plots
p = plot3d_parametric_line(
sin(x), cos(x), x, legend=True, label='3d_parametric_plot',
adaptive=adaptive, n=10)
filename = 'test_3d_line.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot3d_parametric_line(
(sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3)),
adaptive=adaptive, n=10)
filename = 'test_3d_line_multiple.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot3d_parametric_line(sin(x), cos(x), x, n=30,
adaptive=adaptive)
filename = 'test_3d_line_points.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# 3d surface single plot.
p = plot3d(x * y, adaptive=adaptive, n=10)
filename = 'test_surface.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# Multiple 3D plots with same range.
p = plot3d(-x * y, x * y, (x, -5, 5), adaptive=adaptive, n=10)
filename = 'test_surface_multiple.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# Multiple 3D plots with different ranges.
p = plot3d(
(x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3)),
adaptive=adaptive, n=10)
filename = 'test_surface_multiple_ranges.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# Single Parametric 3D plot
p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y,
adaptive=adaptive, n=10)
filename = 'test_parametric_surface.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# Multiple Parametric 3D plots.
p = plot3d_parametric_surface(
(x*sin(z), x*cos(z), z, (x, -5, 5), (z, -5, 5)),
(sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5)),
adaptive=adaptive, n=10)
filename = 'test_parametric_surface.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# Single Contour plot.
p = plot_contour(sin(x)*sin(y), (x, -5, 5), (y, -5, 5),
adaptive=adaptive, n=10)
filename = 'test_contour_plot.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# Multiple Contour plots with same range.
p = plot_contour(x**2 + y**2, x**3 + y**3, (x, -5, 5), (y, -5, 5),
adaptive=adaptive, n=10)
filename = 'test_contour_plot.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# Multiple Contour plots with different range.
p = plot_contour(
(x**2 + y**2, (x, -5, 5), (y, -5, 5)),
(x**3 + y**3, (x, -3, 3), (y, -3, 3)),
adaptive=adaptive, n=10)
filename = 'test_contour_plot.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot_and_save_3(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
with TemporaryDirectory(prefix='sympy_') as tmpdir:
###
# Examples from the 'colors' notebook
###
p = plot(sin(x), adaptive=adaptive, n=10)
p[0].line_color = lambda a: a
filename = 'test_colors_line_arity1.png'
p.save(os.path.join(tmpdir, filename))
p[0].line_color = lambda a, b: b
filename = 'test_colors_line_arity2.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot(x*sin(x), x*cos(x), (x, 0, 10), adaptive=adaptive, n=10)
p[0].line_color = lambda a: a
filename = 'test_colors_param_line_arity1.png'
p.save(os.path.join(tmpdir, filename))
p[0].line_color = lambda a, b: a
filename = 'test_colors_param_line_arity1.png'
p.save(os.path.join(tmpdir, filename))
p[0].line_color = lambda a, b: b
filename = 'test_colors_param_line_arity2b.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot3d_parametric_line(
sin(x) + 0.1*sin(x)*cos(7*x),
cos(x) + 0.1*cos(x)*cos(7*x),
0.1*sin(7*x),
(x, 0, 2*pi), adaptive=adaptive, n=10)
p[0].line_color = lambdify_(x, sin(4*x))
filename = 'test_colors_3d_line_arity1.png'
p.save(os.path.join(tmpdir, filename))
p[0].line_color = lambda a, b: b
filename = 'test_colors_3d_line_arity2.png'
p.save(os.path.join(tmpdir, filename))
p[0].line_color = lambda a, b, c: c
filename = 'test_colors_3d_line_arity3.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot3d(sin(x)*y, (x, 0, 6*pi), (y, -5, 5), adaptive=adaptive, n=10)
p[0].surface_color = lambda a: a
filename = 'test_colors_surface_arity1.png'
p.save(os.path.join(tmpdir, filename))
p[0].surface_color = lambda a, b: b
filename = 'test_colors_surface_arity2.png'
p.save(os.path.join(tmpdir, filename))
p[0].surface_color = lambda a, b, c: c
filename = 'test_colors_surface_arity3a.png'
p.save(os.path.join(tmpdir, filename))
p[0].surface_color = lambdify_((x, y, z), sqrt((x - 3*pi)**2 + y**2))
filename = 'test_colors_surface_arity3b.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y,
(x, -1, 1), (y, -1, 1), adaptive=adaptive, n=10)
p[0].surface_color = lambda a: a
filename = 'test_colors_param_surf_arity1.png'
p.save(os.path.join(tmpdir, filename))
p[0].surface_color = lambda a, b: a*b
filename = 'test_colors_param_surf_arity2.png'
p.save(os.path.join(tmpdir, filename))
p[0].surface_color = lambdify_((x, y, z), sqrt(x**2 + y**2 + z**2))
filename = 'test_colors_param_surf_arity3.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
@pytest.mark.parametrize("adaptive", [True])
def test_plot_and_save_4(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
y = Symbol('y')
###
# Examples from the 'advanced' notebook
###
with TemporaryDirectory(prefix='sympy_') as tmpdir:
i = Integral(log((sin(x)**2 + 1)*sqrt(x**2 + 1)), (x, 0, y))
p = plot(i, (y, 1, 5), adaptive=adaptive, n=10, force_real_eval=True)
filename = 'test_advanced_integral.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot_and_save_5(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
y = Symbol('y')
with TemporaryDirectory(prefix='sympy_') as tmpdir:
s = Sum(1/x**y, (x, 1, oo))
p = plot(s, (y, 2, 10), adaptive=adaptive, n=10)
filename = 'test_advanced_inf_sum.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p = plot(Sum(1/x, (x, 1, y)), (y, 2, 10), show=False,
adaptive=adaptive, n=10)
p[0].only_integers = True
p[0].steps = True
filename = 'test_advanced_fin_sum.png'
# XXX: This should be fixed in experimental_lambdify or by using
# ordinary lambdify so that it doesn't warn. The error results from
# passing an array of values as the integration limit.
#
# UserWarning: The evaluation of the expression is problematic. We are
# trying a failback method that may still work. Please report this as a
# bug.
with ignore_warnings(UserWarning):
p.save(os.path.join(tmpdir, filename))
p._backend.close()
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot_and_save_6(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
with TemporaryDirectory(prefix='sympy_') as tmpdir:
filename = 'test.png'
###
# Test expressions that can not be translated to np and generate complex
# results.
###
p = plot(sin(x) + I*cos(x))
p.save(os.path.join(tmpdir, filename))
with ignore_warnings(RuntimeWarning):
p = plot(sqrt(sqrt(-x)))
p.save(os.path.join(tmpdir, filename))
p = plot(LambertW(x))
p.save(os.path.join(tmpdir, filename))
p = plot(sqrt(LambertW(x)))
p.save(os.path.join(tmpdir, filename))
#Characteristic function of a StudentT distribution with nu=10
x1 = 5 * x**2 * exp_polar(-I*pi)/2
m1 = meijerg(((1 / 2,), ()), ((5, 0, 1 / 2), ()), x1)
x2 = 5*x**2 * exp_polar(I*pi)/2
m2 = meijerg(((1/2,), ()), ((5, 0, 1/2), ()), x2)
expr = (m1 + m2) / (48 * pi)
with warns(
UserWarning,
match="The evaluation with NumPy/SciPy failed",
test_stacklevel=False,
):
p = plot(expr, (x, 1e-6, 1e-2), adaptive=adaptive, n=10)
p.save(os.path.join(tmpdir, filename))
@pytest.mark.parametrize("adaptive", [True, False])
def test_plotgrid_and_save(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
y = Symbol('y')
with TemporaryDirectory(prefix='sympy_') as tmpdir:
p1 = plot(x, adaptive=adaptive, n=10)
p2 = plot_parametric((sin(x), cos(x)), (x, sin(x)), show=False,
adaptive=adaptive, n=10)
p3 = plot_parametric(
cos(x), sin(x), adaptive=adaptive, n=10, show=False)
p4 = plot3d_parametric_line(sin(x), cos(x), x, show=False,
adaptive=adaptive, n=10)
# symmetric grid
p = PlotGrid(2, 2, p1, p2, p3, p4)
filename = 'test_grid1.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
# grid size greater than the number of subplots
p = PlotGrid(3, 4, p1, p2, p3, p4)
filename = 'test_grid2.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
p5 = plot(cos(x),(x, -pi, pi), show=False, adaptive=adaptive, n=10)
p5[0].line_color = lambda a: a
p6 = plot(Piecewise((1, x > 0), (0, True)), (x, -1, 1), show=False,
adaptive=adaptive, n=10)
p7 = plot_contour(
(x**2 + y**2, (x, -5, 5), (y, -5, 5)),
(x**3 + y**3, (x, -3, 3), (y, -3, 3)), show=False,
adaptive=adaptive, n=10)
# unsymmetric grid (subplots in one line)
p = PlotGrid(1, 3, p5, p6, p7)
filename = 'test_grid3.png'
p.save(os.path.join(tmpdir, filename))
p._backend.close()
@pytest.mark.parametrize("adaptive", [True, False])
def test_append_issue_7140(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p1 = plot(x, adaptive=adaptive, n=10)
p2 = plot(x**2, adaptive=adaptive, n=10)
plot(x + 2, adaptive=adaptive, n=10)
# append a series
p2.append(p1[0])
assert len(p2._series) == 2
with raises(TypeError):
p1.append(p2)
with raises(TypeError):
p1.append(p2._series)
@pytest.mark.parametrize("adaptive", [True, False])
def test_issue_15265(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
eqn = sin(x)
p = plot(eqn, xlim=(-S.Pi, S.Pi), ylim=(-1, 1), adaptive=adaptive, n=10)
p._backend.close()
p = plot(eqn, xlim=(-1, 1), ylim=(-S.Pi, S.Pi), adaptive=adaptive, n=10)
p._backend.close()
p = plot(eqn, xlim=(-1, 1), adaptive=adaptive, n=10,
ylim=(sympify('-3.14'), sympify('3.14')))
p._backend.close()
p = plot(eqn, adaptive=adaptive, n=10,
xlim=(sympify('-3.14'), sympify('3.14')), ylim=(-1, 1))
p._backend.close()
raises(ValueError,
lambda: plot(eqn, adaptive=adaptive, n=10,
xlim=(-S.ImaginaryUnit, 1), ylim=(-1, 1)))
raises(ValueError,
lambda: plot(eqn, adaptive=adaptive, n=10,
xlim=(-1, 1), ylim=(-1, S.ImaginaryUnit)))
raises(ValueError,
lambda: plot(eqn, adaptive=adaptive, n=10,
xlim=(S.NegativeInfinity, 1), ylim=(-1, 1)))
raises(ValueError,
lambda: plot(eqn, adaptive=adaptive, n=10,
xlim=(-1, 1), ylim=(-1, S.Infinity)))
def test_empty_Plot():
if not matplotlib:
skip("Matplotlib not the default backend")
# No exception showing an empty plot
plot()
# Plot is only a base class: doesn't implement any logic for showing
# images
p = Plot()
raises(NotImplementedError, lambda: p.show())
@pytest.mark.parametrize("adaptive", [True, False])
def test_issue_17405(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
f = x**0.3 - 10*x**3 + x**2
p = plot(f, (x, -10, 10), adaptive=adaptive, n=30, show=False)
# Random number of segments, probably more than 100, but we want to see
# that there are segments generated, as opposed to when the bug was present
# RuntimeWarning: invalid value encountered in double_scalars
with ignore_warnings(RuntimeWarning):
assert len(p[0].get_data()[0]) >= 30
@pytest.mark.parametrize("adaptive", [True, False])
def test_logplot_PR_16796(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p = plot(x, (x, .001, 100), adaptive=adaptive, n=30,
xscale='log', show=False)
# Random number of segments, probably more than 100, but we want to see
# that there are segments generated, as opposed to when the bug was present
assert len(p[0].get_data()[0]) >= 30
assert p[0].end == 100.0
assert p[0].start == .001
@pytest.mark.parametrize("adaptive", [True, False])
def test_issue_16572(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p = plot(LambertW(x), show=False, adaptive=adaptive, n=30)
# Random number of segments, probably more than 50, but we want to see
# that there are segments generated, as opposed to when the bug was present
assert len(p[0].get_data()[0]) >= 30
@pytest.mark.parametrize("adaptive", [True, False])
def test_issue_11865(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
k = Symbol('k', integer=True)
f = Piecewise((-I*exp(I*pi*k)/k + I*exp(-I*pi*k)/k, Ne(k, 0)), (2*pi, True))
p = plot(f, show=False, adaptive=adaptive, n=30)
# Random number of segments, probably more than 100, but we want to see
# that there are segments generated, as opposed to when the bug was present
# and that there are no exceptions.
assert len(p[0].get_data()[0]) >= 30
def test_issue_11461():
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p = plot(real_root((log(x/(x-2))), 3), show=False, adaptive=True)
with warns(
RuntimeWarning,
match="invalid value encountered in",
test_stacklevel=False,
):
# Random number of segments, probably more than 100, but we want to see
# that there are segments generated, as opposed to when the bug was present
# and that there are no exceptions.
assert len(p[0].get_data()[0]) >= 30
@pytest.mark.parametrize("adaptive", [True, False])
def test_issue_11764(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p = plot_parametric(cos(x), sin(x), (x, 0, 2 * pi),
aspect_ratio=(1,1), show=False, adaptive=adaptive, n=30)
assert p.aspect_ratio == (1, 1)
# Random number of segments, probably more than 100, but we want to see
# that there are segments generated, as opposed to when the bug was present
assert len(p[0].get_data()[0]) >= 30
@pytest.mark.parametrize("adaptive", [True, False])
def test_issue_13516(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
pm = plot(sin(x), backend="matplotlib", show=False, adaptive=adaptive, n=30)
assert pm.backend == MatplotlibBackend
assert len(pm[0].get_data()[0]) >= 30
pt = plot(sin(x), backend="text", show=False, adaptive=adaptive, n=30)
assert pt.backend == TextBackend
assert len(pt[0].get_data()[0]) >= 30
pd = plot(sin(x), backend="default", show=False, adaptive=adaptive, n=30)
assert pd.backend == MatplotlibBackend
assert len(pd[0].get_data()[0]) >= 30
p = plot(sin(x), show=False, adaptive=adaptive, n=30)
assert p.backend == MatplotlibBackend
assert len(p[0].get_data()[0]) >= 30
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot_limits(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p = plot(x, x**2, (x, -10, 10), adaptive=adaptive, n=10)
backend = p._backend
xmin, xmax = backend.ax.get_xlim()
assert abs(xmin + 10) < 2
assert abs(xmax - 10) < 2
ymin, ymax = backend.ax.get_ylim()
assert abs(ymin + 10) < 10
assert abs(ymax - 100) < 10
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot3d_parametric_line_limits(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
v1 = (2*cos(x), 2*sin(x), 2*x, (x, -5, 5))
v2 = (sin(x), cos(x), x, (x, -5, 5))
p = plot3d_parametric_line(v1, v2, adaptive=adaptive, n=60)
backend = p._backend
xmin, xmax = backend.ax.get_xlim()
assert abs(xmin + 2) < 1e-2
assert abs(xmax - 2) < 1e-2
ymin, ymax = backend.ax.get_ylim()
assert abs(ymin + 2) < 1e-2
assert abs(ymax - 2) < 1e-2
zmin, zmax = backend.ax.get_zlim()
assert abs(zmin + 10) < 1e-2
assert abs(zmax - 10) < 1e-2
p = plot3d_parametric_line(v2, v1, adaptive=adaptive, n=60)
backend = p._backend
xmin, xmax = backend.ax.get_xlim()
assert abs(xmin + 2) < 1e-2
assert abs(xmax - 2) < 1e-2
ymin, ymax = backend.ax.get_ylim()
assert abs(ymin + 2) < 1e-2
assert abs(ymax - 2) < 1e-2
zmin, zmax = backend.ax.get_zlim()
assert abs(zmin + 10) < 1e-2
assert abs(zmax - 10) < 1e-2
@pytest.mark.parametrize("adaptive", [True, False])
def test_plot_size(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p1 = plot(sin(x), backend="matplotlib", size=(8, 4),
adaptive=adaptive, n=10)
s1 = p1._backend.fig.get_size_inches()
assert (s1[0] == 8) and (s1[1] == 4)
p2 = plot(sin(x), backend="matplotlib", size=(5, 10),
adaptive=adaptive, n=10)
s2 = p2._backend.fig.get_size_inches()
assert (s2[0] == 5) and (s2[1] == 10)
p3 = PlotGrid(2, 1, p1, p2, size=(6, 2),
adaptive=adaptive, n=10)
s3 = p3._backend.fig.get_size_inches()
assert (s3[0] == 6) and (s3[1] == 2)
with raises(ValueError):
plot(sin(x), backend="matplotlib", size=(-1, 3))
def test_issue_20113():
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
# verify the capability to use custom backends
plot(sin(x), backend=Plot, show=False)
p2 = plot(sin(x), backend=MatplotlibBackend, show=False)
assert p2.backend == MatplotlibBackend
assert len(p2[0].get_data()[0]) >= 30
p3 = plot(sin(x), backend=DummyBackendOk, show=False)
assert p3.backend == DummyBackendOk
assert len(p3[0].get_data()[0]) >= 30
# test for an improper coded backend
p4 = plot(sin(x), backend=DummyBackendNotOk, show=False)
assert p4.backend == DummyBackendNotOk
assert len(p4[0].get_data()[0]) >= 30
with raises(NotImplementedError):
p4.show()
with raises(NotImplementedError):
p4.save("test/path")
with raises(NotImplementedError):
p4._backend.close()
def test_custom_coloring():
x = Symbol('x')
y = Symbol('y')
plot(cos(x), line_color=lambda a: a)
plot(cos(x), line_color=1)
plot(cos(x), line_color="r")
plot_parametric(cos(x), sin(x), line_color=lambda a: a)
plot_parametric(cos(x), sin(x), line_color=1)
plot_parametric(cos(x), sin(x), line_color="r")
plot3d_parametric_line(cos(x), sin(x), x, line_color=lambda a: a)
plot3d_parametric_line(cos(x), sin(x), x, line_color=1)
plot3d_parametric_line(cos(x), sin(x), x, line_color="r")
plot3d_parametric_surface(cos(x + y), sin(x - y), x - y,
(x, -5, 5), (y, -5, 5),
surface_color=lambda a, b: a**2 + b**2)
plot3d_parametric_surface(cos(x + y), sin(x - y), x - y,
(x, -5, 5), (y, -5, 5),
surface_color=1)
plot3d_parametric_surface(cos(x + y), sin(x - y), x - y,
(x, -5, 5), (y, -5, 5),
surface_color="r")
plot3d(x*y, (x, -5, 5), (y, -5, 5),
surface_color=lambda a, b: a**2 + b**2)
plot3d(x*y, (x, -5, 5), (y, -5, 5), surface_color=1)
plot3d(x*y, (x, -5, 5), (y, -5, 5), surface_color="r")
@pytest.mark.parametrize("adaptive", [True, False])
def test_deprecated_get_segments(adaptive):
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
f = sin(x)
p = plot(f, (x, -10, 10), show=False, adaptive=adaptive, n=10)
with warns_deprecated_sympy():
p[0].get_segments()
@pytest.mark.parametrize("adaptive", [True, False])
def test_generic_data_series(adaptive):
# verify that no errors are raised when generic data series are used
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol("x")
p = plot(x,
markers=[{"args":[[0, 1], [0, 1]], "marker": "*", "linestyle": "none"}],
annotations=[{"text": "test", "xy": (0, 0)}],
fill={"x": [0, 1, 2, 3], "y1": [0, 1, 2, 3]},
rectangles=[{"xy": (0, 0), "width": 5, "height": 1}],
adaptive=adaptive, n=10)
assert len(p._backend.ax.collections) == 1
assert len(p._backend.ax.patches) == 1
assert len(p._backend.ax.lines) == 2
assert len(p._backend.ax.texts) == 1
def test_deprecated_markers_annotations_rectangles_fill():
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
p = plot(sin(x), (x, -10, 10), show=False)
with warns_deprecated_sympy():
p.markers = [{"args":[[0, 1], [0, 1]], "marker": "*", "linestyle": "none"}]
assert len(p._series) == 2
with warns_deprecated_sympy():
p.annotations = [{"text": "test", "xy": (0, 0)}]
assert len(p._series) == 3
with warns_deprecated_sympy():
p.fill = {"x": [0, 1, 2, 3], "y1": [0, 1, 2, 3]}
assert len(p._series) == 4
with warns_deprecated_sympy():
p.rectangles = [{"xy": (0, 0), "width": 5, "height": 1}]
assert len(p._series) == 5
def test_back_compatibility():
if not matplotlib:
skip("Matplotlib not the default backend")
x = Symbol('x')
y = Symbol('y')
p = plot(sin(x), adaptive=False, n=5)
assert len(p[0].get_points()) == 2
assert len(p[0].get_data()) == 2
p = plot_parametric(cos(x), sin(x), (x, 0, 2), adaptive=False, n=5)
assert len(p[0].get_points()) == 2
assert len(p[0].get_data()) == 3
p = plot3d_parametric_line(cos(x), sin(x), x, (x, 0, 2),
adaptive=False, n=5)
assert len(p[0].get_points()) == 3
assert len(p[0].get_data()) == 4
p = plot3d(cos(x**2 + y**2), (x, -pi, pi), (y, -pi, pi), n=5)
assert len(p[0].get_meshes()) == 3
assert len(p[0].get_data()) == 3
p = plot_contour(cos(x**2 + y**2), (x, -pi, pi), (y, -pi, pi), n=5)
assert len(p[0].get_meshes()) == 3
assert len(p[0].get_data()) == 3
p = plot3d_parametric_surface(x * cos(y), x * sin(y), x * cos(4 * y) / 2,
(x, 0, pi), (y, 0, 2*pi), n=5)
assert len(p[0].get_meshes()) == 3
assert len(p[0].get_data()) == 5
def test_plot_arguments():
### Test arguments for plot()
if not matplotlib:
skip("Matplotlib not the default backend")
x, y = symbols("x, y")
# single expressions
p = plot(x + 1)
assert isinstance(p[0], LineOver1DRangeSeries)
assert p[0].expr == x + 1
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x + 1"
assert p[0].rendering_kw == {}
# single expressions custom label
p = plot(x + 1, "label")
assert isinstance(p[0], LineOver1DRangeSeries)
assert p[0].expr == x + 1
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "label"
assert p[0].rendering_kw == {}
# single expressions with range
p = plot(x + 1, (x, -2, 2))
assert p[0].ranges == [(x, -2, 2)]
# single expressions with range, label and rendering-kw dictionary
p = plot(x + 1, (x, -2, 2), "test", {"color": "r"})
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {"color": "r"}
# multiple expressions
p = plot(x + 1, x**2)
assert isinstance(p[0], LineOver1DRangeSeries)
assert p[0].expr == x + 1
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x + 1"
assert p[0].rendering_kw == {}
assert isinstance(p[1], LineOver1DRangeSeries)
assert p[1].expr == x**2
assert p[1].ranges == [(x, -10, 10)]
assert p[1].get_label(False) == "x**2"
assert p[1].rendering_kw == {}
# multiple expressions over the same range
p = plot(x + 1, x**2, (x, 0, 5))
assert p[0].ranges == [(x, 0, 5)]
assert p[1].ranges == [(x, 0, 5)]
# multiple expressions over the same range with the same rendering kws
p = plot(x + 1, x**2, (x, 0, 5), {"color": "r"})
assert p[0].ranges == [(x, 0, 5)]
assert p[1].ranges == [(x, 0, 5)]
assert p[0].rendering_kw == {"color": "r"}
assert p[1].rendering_kw == {"color": "r"}
# multiple expressions with different ranges, labels and rendering kws
p = plot(
(x + 1, (x, 0, 5)),
(x**2, (x, -2, 2), "test", {"color": "r"}))
assert isinstance(p[0], LineOver1DRangeSeries)
assert p[0].expr == x + 1
assert p[0].ranges == [(x, 0, 5)]
assert p[0].get_label(False) == "x + 1"
assert p[0].rendering_kw == {}
assert isinstance(p[1], LineOver1DRangeSeries)
assert p[1].expr == x**2
assert p[1].ranges == [(x, -2, 2)]
assert p[1].get_label(False) == "test"
assert p[1].rendering_kw == {"color": "r"}
# single argument: lambda function
f = lambda t: t
p = plot(lambda t: t)
assert isinstance(p[0], LineOver1DRangeSeries)
assert callable(p[0].expr)
assert p[0].ranges[0][1:] == (-10, 10)
assert p[0].get_label(False) == ""
assert p[0].rendering_kw == {}
# single argument: lambda function + custom range and label
p = plot(f, ("t", -5, 6), "test")
assert p[0].ranges[0][1:] == (-5, 6)
assert p[0].get_label(False) == "test"
def test_plot_parametric_arguments():
### Test arguments for plot_parametric()
if not matplotlib:
skip("Matplotlib not the default backend")
x, y = symbols("x, y")
# single parametric expression
p = plot_parametric(x + 1, x)
assert isinstance(p[0], Parametric2DLineSeries)
assert p[0].expr == (x + 1, x)
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
# single parametric expression with custom range, label and rendering kws
p = plot_parametric(x + 1, x, (x, -2, 2), "test",
{"cmap": "Reds"})
assert p[0].expr == (x + 1, x)
assert p[0].ranges == [(x, -2, 2)]
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {"cmap": "Reds"}
p = plot_parametric((x + 1, x), (x, -2, 2), "test")
assert p[0].expr == (x + 1, x)
assert p[0].ranges == [(x, -2, 2)]
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {}
# multiple parametric expressions same symbol
p = plot_parametric((x + 1, x), (x ** 2, x + 1))
assert p[0].expr == (x + 1, x)
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
assert p[1].expr == (x ** 2, x + 1)
assert p[1].ranges == [(x, -10, 10)]
assert p[1].get_label(False) == "x"
assert p[1].rendering_kw == {}
# multiple parametric expressions different symbols
p = plot_parametric((x + 1, x), (y ** 2, y + 1, "test"))
assert p[0].expr == (x + 1, x)
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
assert p[1].expr == (y ** 2, y + 1)
assert p[1].ranges == [(y, -10, 10)]
assert p[1].get_label(False) == "test"
assert p[1].rendering_kw == {}
# multiple parametric expressions same range
p = plot_parametric((x + 1, x), (x ** 2, x + 1), (x, -2, 2))
assert p[0].expr == (x + 1, x)
assert p[0].ranges == [(x, -2, 2)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
assert p[1].expr == (x ** 2, x + 1)
assert p[1].ranges == [(x, -2, 2)]
assert p[1].get_label(False) == "x"
assert p[1].rendering_kw == {}
# multiple parametric expressions, custom ranges and labels
p = plot_parametric(
(x + 1, x, (x, -2, 2), "test1"),
(x ** 2, x + 1, (x, -3, 3), "test2", {"cmap": "Reds"}))
assert p[0].expr == (x + 1, x)
assert p[0].ranges == [(x, -2, 2)]
assert p[0].get_label(False) == "test1"
assert p[0].rendering_kw == {}
assert p[1].expr == (x ** 2, x + 1)
assert p[1].ranges == [(x, -3, 3)]
assert p[1].get_label(False) == "test2"
assert p[1].rendering_kw == {"cmap": "Reds"}
# single argument: lambda function
fx = lambda t: t
fy = lambda t: 2 * t
p = plot_parametric(fx, fy)
assert all(callable(t) for t in p[0].expr)
assert p[0].ranges[0][1:] == (-10, 10)
assert "Dummy" in p[0].get_label(False)
assert p[0].rendering_kw == {}
# single argument: lambda function + custom range + label
p = plot_parametric(fx, fy, ("t", 0, 2), "test")
assert all(callable(t) for t in p[0].expr)
assert p[0].ranges[0][1:] == (0, 2)
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {}
def test_plot3d_parametric_line_arguments():
### Test arguments for plot3d_parametric_line()
if not matplotlib:
skip("Matplotlib not the default backend")
x, y = symbols("x, y")
# single parametric expression
p = plot3d_parametric_line(x + 1, x, sin(x))
assert isinstance(p[0], Parametric3DLineSeries)
assert p[0].expr == (x + 1, x, sin(x))
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
# single parametric expression with custom range, label and rendering kws
p = plot3d_parametric_line(x + 1, x, sin(x), (x, -2, 2),
"test", {"cmap": "Reds"})
assert isinstance(p[0], Parametric3DLineSeries)
assert p[0].expr == (x + 1, x, sin(x))
assert p[0].ranges == [(x, -2, 2)]
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {"cmap": "Reds"}
p = plot3d_parametric_line((x + 1, x, sin(x)), (x, -2, 2), "test")
assert p[0].expr == (x + 1, x, sin(x))
assert p[0].ranges == [(x, -2, 2)]
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {}
# multiple parametric expression same symbol
p = plot3d_parametric_line(
(x + 1, x, sin(x)), (x ** 2, 1, cos(x), {"cmap": "Reds"}))
assert p[0].expr == (x + 1, x, sin(x))
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
assert p[1].expr == (x ** 2, 1, cos(x))
assert p[1].ranges == [(x, -10, 10)]
assert p[1].get_label(False) == "x"
assert p[1].rendering_kw == {"cmap": "Reds"}
# multiple parametric expression different symbols
p = plot3d_parametric_line((x + 1, x, sin(x)), (y ** 2, 1, cos(y)))
assert p[0].expr == (x + 1, x, sin(x))
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
assert p[1].expr == (y ** 2, 1, cos(y))
assert p[1].ranges == [(y, -10, 10)]
assert p[1].get_label(False) == "y"
assert p[1].rendering_kw == {}
# multiple parametric expression, custom ranges and labels
p = plot3d_parametric_line(
(x + 1, x, sin(x)),
(x ** 2, 1, cos(x), (x, -2, 2), "test", {"cmap": "Reds"}))
assert p[0].expr == (x + 1, x, sin(x))
assert p[0].ranges == [(x, -10, 10)]
assert p[0].get_label(False) == "x"
assert p[0].rendering_kw == {}
assert p[1].expr == (x ** 2, 1, cos(x))
assert p[1].ranges == [(x, -2, 2)]
assert p[1].get_label(False) == "test"
assert p[1].rendering_kw == {"cmap": "Reds"}
# single argument: lambda function
fx = lambda t: t
fy = lambda t: 2 * t
fz = lambda t: 3 * t
p = plot3d_parametric_line(fx, fy, fz)
assert all(callable(t) for t in p[0].expr)
assert p[0].ranges[0][1:] == (-10, 10)
assert "Dummy" in p[0].get_label(False)
assert p[0].rendering_kw == {}
# single argument: lambda function + custom range + label
p = plot3d_parametric_line(fx, fy, fz, ("t", 0, 2), "test")
assert all(callable(t) for t in p[0].expr)
assert p[0].ranges[0][1:] == (0, 2)
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {}
def test_plot3d_plot_contour_arguments():
### Test arguments for plot3d() and plot_contour()
if not matplotlib:
skip("Matplotlib not the default backend")
x, y = symbols("x, y")
# single expression
p = plot3d(x + y)
assert isinstance(p[0], SurfaceOver2DRangeSeries)
assert p[0].expr == x + y
assert p[0].ranges[0] == (x, -10, 10) or (y, -10, 10)
assert p[0].ranges[1] == (x, -10, 10) or (y, -10, 10)
assert p[0].get_label(False) == "x + y"
assert p[0].rendering_kw == {}
# single expression, custom range, label and rendering kws
p = plot3d(x + y, (x, -2, 2), "test", {"cmap": "Reds"})
assert isinstance(p[0], SurfaceOver2DRangeSeries)
assert p[0].expr == x + y
assert p[0].ranges[0] == (x, -2, 2)
assert p[0].ranges[1] == (y, -10, 10)
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {"cmap": "Reds"}
p = plot3d(x + y, (x, -2, 2), (y, -4, 4), "test")
assert p[0].ranges[0] == (x, -2, 2)
assert p[0].ranges[1] == (y, -4, 4)
# multiple expressions
p = plot3d(x + y, x * y)
assert p[0].expr == x + y
assert p[0].ranges[0] == (x, -10, 10) or (y, -10, 10)
assert p[0].ranges[1] == (x, -10, 10) or (y, -10, 10)
assert p[0].get_label(False) == "x + y"
assert p[0].rendering_kw == {}
assert p[1].expr == x * y
assert p[1].ranges[0] == (x, -10, 10) or (y, -10, 10)
assert p[1].ranges[1] == (x, -10, 10) or (y, -10, 10)
assert p[1].get_label(False) == "x*y"
assert p[1].rendering_kw == {}
# multiple expressions, same custom ranges
p = plot3d(x + y, x * y, (x, -2, 2), (y, -4, 4))
assert p[0].expr == x + y
assert p[0].ranges[0] == (x, -2, 2)
assert p[0].ranges[1] == (y, -4, 4)
assert p[0].get_label(False) == "x + y"
assert p[0].rendering_kw == {}
assert p[1].expr == x * y
assert p[1].ranges[0] == (x, -2, 2)
assert p[1].ranges[1] == (y, -4, 4)
assert p[1].get_label(False) == "x*y"
assert p[1].rendering_kw == {}
# multiple expressions, custom ranges, labels and rendering kws
p = plot3d(
(x + y, (x, -2, 2), (y, -4, 4)),
(x * y, (x, -3, 3), (y, -6, 6), "test", {"cmap": "Reds"}))
assert p[0].expr == x + y
assert p[0].ranges[0] == (x, -2, 2)
assert p[0].ranges[1] == (y, -4, 4)
assert p[0].get_label(False) == "x + y"
assert p[0].rendering_kw == {}
assert p[1].expr == x * y
assert p[1].ranges[0] == (x, -3, 3)
assert p[1].ranges[1] == (y, -6, 6)
assert p[1].get_label(False) == "test"
assert p[1].rendering_kw == {"cmap": "Reds"}
# single expression: lambda function
f = lambda x, y: x + y
p = plot3d(f)
assert callable(p[0].expr)
assert p[0].ranges[0][1:] == (-10, 10)
assert p[0].ranges[1][1:] == (-10, 10)
assert p[0].get_label(False) == ""
assert p[0].rendering_kw == {}
# single expression: lambda function + custom ranges + label
p = plot3d(f, ("a", -5, 3), ("b", -2, 1), "test")
assert callable(p[0].expr)
assert p[0].ranges[0][1:] == (-5, 3)
assert p[0].ranges[1][1:] == (-2, 1)
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {}
# test issue 25818
# single expression, custom range, min/max functions
p = plot3d(Min(x, y), (x, 0, 10), (y, 0, 10))
assert isinstance(p[0], SurfaceOver2DRangeSeries)
assert p[0].expr == Min(x, y)
assert p[0].ranges[0] == (x, 0, 10)
assert p[0].ranges[1] == (y, 0, 10)
assert p[0].get_label(False) == "Min(x, y)"
assert p[0].rendering_kw == {}
def test_plot3d_parametric_surface_arguments():
### Test arguments for plot3d_parametric_surface()
if not matplotlib:
skip("Matplotlib not the default backend")
x, y = symbols("x, y")
# single parametric expression
p = plot3d_parametric_surface(x + y, cos(x + y), sin(x + y))
assert isinstance(p[0], ParametricSurfaceSeries)
assert p[0].expr == (x + y, cos(x + y), sin(x + y))
assert p[0].ranges[0] == (x, -10, 10) or (y, -10, 10)
assert p[0].ranges[1] == (x, -10, 10) or (y, -10, 10)
assert p[0].get_label(False) == "(x + y, cos(x + y), sin(x + y))"
assert p[0].rendering_kw == {}
# single parametric expression, custom ranges, labels and rendering kws
p = plot3d_parametric_surface(x + y, cos(x + y), sin(x + y),
(x, -2, 2), (y, -4, 4), "test", {"cmap": "Reds"})
assert isinstance(p[0], ParametricSurfaceSeries)
assert p[0].expr == (x + y, cos(x + y), sin(x + y))
assert p[0].ranges[0] == (x, -2, 2)
assert p[0].ranges[1] == (y, -4, 4)
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {"cmap": "Reds"}
# multiple parametric expressions
p = plot3d_parametric_surface(
(x + y, cos(x + y), sin(x + y)),
(x - y, cos(x - y), sin(x - y), "test"))
assert p[0].expr == (x + y, cos(x + y), sin(x + y))
assert p[0].ranges[0] == (x, -10, 10) or (y, -10, 10)
assert p[0].ranges[1] == (x, -10, 10) or (y, -10, 10)
assert p[0].get_label(False) == "(x + y, cos(x + y), sin(x + y))"
assert p[0].rendering_kw == {}
assert p[1].expr == (x - y, cos(x - y), sin(x - y))
assert p[1].ranges[0] == (x, -10, 10) or (y, -10, 10)
assert p[1].ranges[1] == (x, -10, 10) or (y, -10, 10)
assert p[1].get_label(False) == "test"
assert p[1].rendering_kw == {}
# multiple parametric expressions, custom ranges and labels
p = plot3d_parametric_surface(
(x + y, cos(x + y), sin(x + y), (x, -2, 2), "test"),
(x - y, cos(x - y), sin(x - y), (x, -3, 3), (y, -4, 4),
"test2", {"cmap": "Reds"}))
assert p[0].expr == (x + y, cos(x + y), sin(x + y))
assert p[0].ranges[0] == (x, -2, 2)
assert p[0].ranges[1] == (y, -10, 10)
assert p[0].get_label(False) == "test"
assert p[0].rendering_kw == {}
assert p[1].expr == (x - y, cos(x - y), sin(x - y))
assert p[1].ranges[0] == (x, -3, 3)
assert p[1].ranges[1] == (y, -4, 4)
assert p[1].get_label(False) == "test2"
assert p[1].rendering_kw == {"cmap": "Reds"}
# lambda functions instead of symbolic expressions for a single 3D
# parametric surface
p = plot3d_parametric_surface(
lambda u, v: u, lambda u, v: v, lambda u, v: u + v,
("u", 0, 2), ("v", -3, 4))
assert all(callable(t) for t in p[0].expr)
assert p[0].ranges[0][1:] == (-0, 2)
assert p[0].ranges[1][1:] == (-3, 4)
assert p[0].get_label(False) == ""
assert p[0].rendering_kw == {}
# lambda functions instead of symbolic expressions for multiple 3D
# parametric surfaces
p = plot3d_parametric_surface(
(lambda u, v: u, lambda u, v: v, lambda u, v: u + v,
("u", 0, 2), ("v", -3, 4)),
(lambda u, v: v, lambda u, v: u, lambda u, v: u - v,
("u", -2, 3), ("v", -4, 5), "test"))
assert all(callable(t) for t in p[0].expr)
assert p[0].ranges[0][1:] == (0, 2)
assert p[0].ranges[1][1:] == (-3, 4)
assert p[0].get_label(False) == ""
assert p[0].rendering_kw == {}
assert all(callable(t) for t in p[1].expr)
assert p[1].ranges[0][1:] == (-2, 3)
assert p[1].ranges[1][1:] == (-4, 5)
assert p[1].get_label(False) == "test"
assert p[1].rendering_kw == {}
|