python – Improving plotting using Matprolib

Program description:

You are given a set of two functions: $$f=x^3-6x^2+x+5; g=(x-2)^2-6$$
Plot them using Matprolib.

My solution:

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interpolate # for smoothing

def func(x):
    return (pow(x, 3) - 6 * pow(x, 2) + x + 5, pow((x-2), 2) - 6)
# plt.plot((1,5, -3, 0.5), (1, 25, 9, 0.5))
# plt.plot(1, 7, "r+")
# plt.plot(-1, 7, "bo")
f = ()
f_1 = ()
x = ()
interval = (int(x) for x in input("Define segment (i.e. a b): ").split(' '))
for val in range(interval(0), interval(1) + 1):
    x.append(val)
    f.append(func(val)(0))
    f_1.append(func(val)(1))

linear_space = np.linspace(interval(0), interval(1), 300)
a_BSpline = interpolate.make_interp_spline(x, f)
b_BSpline = interpolate.make_interp_spline(x, f_1)

f_new = a_BSpline(linear_space)
f_1_new = b_BSpline(linear_space)

plt.plot(linear_space, f_new, color="#47c984",
linestyle="solid", linewidth=1)

plt.plot(linear_space, f_1_new, color="#fc6703",
linestyle="solid", linewidth=1)

plt.gca().spines("left").set_position("zero")
plt.gca().spines("bottom").set_position("zero")
plt.show()

Input: -10 10

Output:

1

Question:
Is there any way to make this code more concise?

Thank you in advance.

plotting – Exporting selected column of multiple table in a single excel file

   Please suggest how to export multiple tables in a single excel file.
   I am trying using given below code:
   d1 = {{1, 26.00}, {0.8, 35.43}, {0.6, 33.40}, {0.4, 31.11}, {0, 
  56.67}};
    d2 = {{1, 45.58}, {0.8, 50.16}, {0.6, 56.18}, {0.4, 57.03}, {0, 
     33.17}};
   d3 = {{1, 10.23}, {0.8, 9.13}, {0.6, 6.88}, {0.4, 10.11}, {0, 5.86}};
     d11 = TableForm[d1]; d22 = TableForm[d2]; d33 = TableForm[d3];
    Export["d1diles.xls", {d11, d22, d33}, "XLS"]
   Also, 1st column is common in all table so I just want to take only one times it.

plotting – How to change color of the points and remove the joined line in the given code?

Like this?

{data1, data2} = Transpose[Table[{2 n, 3 n + 100}, {n, 20}]];
a = ListPlot[data1, Joined -> True, 
   PlotStyle -> {Directive[Red, Thickness[Scaled[10^-2]]], 
     Directive[Blue, Thickness[Scaled[10^-2]]]}, Mesh -> All, 
   MeshStyle -> Directive[PointSize[Large], Red], 
   ClippingStyle -> False];
b = ListPlot[data2, Mesh -> 20, PlotStyle -> All, 
   ColorFunction -> Function[{x, y}, Blend[{Red, Blue}, x]]];
Show[a, b, PlotRange -> All]

enter image description here

Making and plotting a Fourier transform

I need to find the Fourier transform g(p) of the function f(x) where

enter image description here

and plot $f(x)$ on one plot and the real part of the Fourier transform $g(p)$, its imaginary part and its absolute value on a separate plot. Finally, I need to calculate the Fourier transform of $g(p)$ and confirm that I get the original function $f(x)$.

plotting – How to turn three two-dimensional plots into one 3D-plot

I am trying to make a complex function grapher (code below), and have made real and imaginary output 3d graphs, and have created functions to “slice” the graph into two dimensional contour plots for any given x, y or z, (leaving the other two variables as the axes for this). Often seeing layers of a graph is a lot easier to understand than the 3d planes the first function creates. I would like to create a “skeleton graph”, in which you can see the x y and z intercept plots on the 3d graph so that you can understand the general shape of the function. I am unable to do this, and am asking for help with this. Code left below.

complexfunctiongraph(f_) := 
 Show(Plot3D(
   z = Re(f(x + (y*I))), {x, Xrange((1)), Xrange((2))}, {y, 
    Yrange((1)), Yrange((2))}, 
   PlotRange -> {Zrange((1)), Zrange((2))}, Boxed -> False, 
   AxesStyle -> {Red, Green, Blue}, 
   AxesLabel -> {"Re Input", "Im Input", "Output"}, 
   PlotStyle -> {Green, Thick}), 
  Plot3D(z = Im(f(x + (y*I))), {x, Xrange((1)), Xrange((2))}, {y, 
    Yrange((1)), Yrange((2))}, 
   PlotRange -> {Zrange((1)), Zrange((2))}, Boxed -> False, 
   AxesStyle -> {Red, Green, Blue}, 
   AxesLabel -> {"Re Input", "Im Input", "Output"}, 
   PlotStyle -> {Blue, Opacity(.5)}))
complexfunctionzeroX(f_, n_) :=
 Show(ContourPlot(
   Re(f(n + (y*I))) == z, {y, Yrange((1)), Yrange((2))}, {z, 
    Zrange((1)), Zrange((2))}, ContourStyle -> Green, 
   FrameStyle -> {Blue, Green}, FrameLabel -> {"IM input", "Output"}),
   ContourPlot(
   Im(f(n + (y*I))) == z, {y, Yrange((1)), Yrange((2))}, {z, 
    Zrange((1)), Zrange((2))}, ContourStyle -> {Blue, Opacity(.5)}, 
   AxesStyle -> {Blue, Green}, AxesLabel -> {"IM imput", "Output"}))
complexfunctionzeroY(f_, n_) :=
 Show(ContourPlot(
   Re(f(x + (n*I))) == z, {x, Xrange((1)), Xrange((2))}, {z, 
    Zrange((1)), Zrange((2))}, ContourStyle -> Green, 
   FrameStyle -> {Blue, Red}, FrameLabel -> {"RE input", "Output"}), 
  ContourPlot(
   Im(f(x + (n*I))) == z, {x, Xrange((1)), Xrange((2))}, {z, 
    Zrange((1)), Zrange((2))}, ContourStyle -> {Blue, Opacity(.5)}, 
   FrameStyle -> {Blue, Red}, FrameLabel -> {"RE input", "Output"}))
complexfunctionzeroZ(f_, n_) := 
 Show(ContourPlot(
   Re(f(x + (y*I))) == n, {x, Xrange((1)), Xrange((2))}, {y, 
    Yrange((1)), Yrange((2))}, ContourStyle -> Green, 
   FrameStyle -> {Red, Green}, FrameLabel -> {"RE input", "IM input"}),
  ContourPlot(
   Im(f(x + (y*I))) == n, {x, Xrange((1)), Xrange((2))}, {y, 
    Yrange((1)), Yrange((2))}, ContourStyle -> {Blue, Opacity(.5)}, 
   FrameStyle -> {Red, Green}, 
   FrameLabel -> {"RE input", "IM input"}))
Xrange = {-10, 10};
Yrange = {-10, 10};
Zrange = {-3, 3};

plotting – Exported plot range of PNG not displayed properly

   I am tring to export the plotted graphics as PNG, but it's range is not coming properly:

   px = Plot[Sin[x], {x, -15, 15}, Frame -> True, 
  ImagePadding -> {{30, 15}, {0, 10}}, ImageSize -> 300, 
  FrameStyle -> Thickness[.003], 
  FrameTicksStyle -> 
  Directive[Black, FontSize -> Scaled[.015], Thickness[.003]], 
  FrameTicks -> linearFrameTicks, PlotRange -> {{-10, 10}, All}];
    py = Plot[Sin[2 x], {x, -15, 15}, Frame -> True, 
    ImagePadding -> {{30, 15}, {20, 0}}, ImageSize -> 300, 
    FrameStyle -> Thickness[.003], 
    FrameTicksStyle -> 
   Directive[Black, FontSize -> Scaled[.015], Thickness[.003]], 
    FrameTicks -> linearFrameTicks, PlotRange -> {{-10, 10}, All}];
     pz = Column[{px, py}, Spacings -> 0]
   Export["pz.png", 
     Import[Export["pz.pdf", pz, ImageSize -> 290, 
     ImageResolution -> 1000]], ImageResolution -> 1000];
     SystemOpen["pz.png"]
          [![plot][1]][1]


            [1]: https://i.stack.imgur.com/QE2XA.png

plotting – plot an inverse function

I have a phrase that in it x is a function of y but I need to plot y as a function of x. I tried InverseFunction but because my phrase is not single valued in some points mathematica can not give me plot. After that I tried Parametric plot but these code has not any answer. Infact I wanna to inverse x and y axes. what should I do?

0.008 y + 
 125. (2. - 
    0.0000271476 (659.714/(1 - 0.169673 y) + (
       2 (2.30388*10^-6 + 0.169673 y)^2)/(1 - 0.169673 y)^2) - (
    0.00271476 (2.30388*10^-6 + 0.169673 y))/(1 - 0.169673 y))^2 y=x 






Plot({0.008 y + 
 125. (2. - 
    0.0000271476 (659.714/(1 - 0.169673 y) + (
       2 (2.30388*10^-6 + 0.169673 y)^2)/(1 - 0.169673 y)^2) - (
    0.00271476 (2.30388*10^-6 + 0.169673 y))/(1 - 0.169673 y))^2 y}, {y, 5.6, 6}) 

plotting – Figures as a one column panel with same x axis but changing y axis

If I’ve understood, it seems like you just want a variation of the answer you received in Plot Figures in two columns.

ResourceFunction["PlotGrid"][
 {
  {Plot[x, {x, 0, 1}, Frame -> True, 
    PlotLabel -> "Some Label"]}, {Plot[x, {x, 0, 1}, Frame -> True, 
    PlotLabel -> "Some Label"]}, {Plot[x^2, {x, 0, 1}, Frame -> True, 
    PlotLabel -> "Some Label"]}, {Plot[x^2, {x, 0, 1}, Frame -> True, 
    PlotLabel -> "Some Label"]}
  }, Spacings -> {0, 30}]

enter image description here

Essentially each grid row has a single element.

You can adjust Spacings to overlap Axes labels and show a single Axis on the bottom graph.

plotting – Relative error with bands plot

The Around expression you mention seems to work fine for me:

a = Around(Range(20, 30), Scaled(0.1))

(* Out: {Around(20., 2.), Around(21., 2.1), Around(22., 2.2), 
         Around(23., 2.3), Around(24., 2.4), Around(25., 2.5), 
         Around(26., 2.6), Around(27., 2.7), 
         Around(28., 2.8), Around(29., 2.9), 
         Around(30., 3.)} *)

From there, I think you might want to plot a vs. b, i.e. the a values as ordinates and the values as abscissae. If that’s what you want, then that works too, but you will want to Transpose the {b, a} set of data:

a = Around(Range(20, 30), Scaled(0.1))
b = Range(0, 10)

ListPlot(
  Transpose@{b, a},
  IntervalMarkers -> "Bands",
  PlotTheme -> "Scientific",
  PlotRangePadding -> {Automatic, Scaled(0.15)}
)

plot with error bands

plotting – Plot a quadratic bezier triangular patch

i need to construct a triangular patch by using the 6 control points
(1/2,√3/4,2),(1/4,√3/4,2),(3/4,√3/4,2),(0,0,1),(1/2,0,2),(1,0,1) however, the surface seems not like a triangular patch. Anyone can help me to solve the problem, thank you.

here is the code:

pts = {{{1/2, Sqrt(3)/2, 1}, {0, 0, 1}}, {{1/4, Sqrt(3)/4, 2}, {1/2, 

         0, 2}}, {{3/4, Sqrt(3)/4, 2}, {1, 0, 1}}};
    f = `BezierFunction`

(pts)
Show(Graphics3D({`PointSize`(Medium), Red, Map(Point, pts)}), 
 Graphics3D({Gray, Line(pts), Line(Transpose(pts))}), 
 ParametricPlot3D(f(u, v), {u, 0, 1}, {v, 0, 1}, 
  ColorFunction -> "Rainbow"), Mesh -> Full, Axes -> True, 
 AxesLabel -> {"x", "y", "z"})

enter image description here