0

I am trying to create a line graph plot showing the residual error for 18 of my iterations of a program:

Graph1

As you can see, the xaxis is using decimals.

Code:

fig, ax = plt.subplots()
ax.plot(cscResidualError,linestyle='--', marker='o', color='b')
ax.set_yscale('log')
plt.title("CSC Residual Error")
plt.xlabel("Iteration #")
plt.ylabel("Residual Error")
plt.figure(2)
plt.savefig("CSC Graph")

When I use xsticks:

Graph2

It all shifts up and does not align with points.

Code:

fig, ax = plt.subplots()
ax.plot(cscResidualError,linestyle='--', marker='o', color='b')
ax.set_yscale('log')
plt.title("CSC Residual Error")
plt.xlabel("Iteration #")
plt.ylabel("Residual Error")
plt.xticks(range(1,len(cscResidualError)+1))
plt.figure(2)
plt.savefig("CSC Graph")

Data:

cscResidualError = [
0.38915721867614916,
0.14078627143431255,
0.0601407351727453,
0.026160551464395596,
0.010909825159991874,
0.0037606157046234353,
0.0014798491987814617,
0.0005147342493009802,
0.00022841941948465328,
0.00009143826264755746,
0.000037994973446398104,
0.000015510527981215652,
0.000006235837935018515,
0.000002454482759005122,
0.0000009932668806359943,
0.000000404640351575657,
0.00000016779591717735326,
0.00000006865543708870457
]

Any help appreciated.

  • Try this: `plt.xticks(range(0,len(cscResidualError)))` – r-beginners Oct 06 '22 at 13:00
  • @r-beginners That makes the points line up with the dots, but the axis is wrong as it labels Iteration 1 as Iteration 0. – Lemon Pepper Oct 06 '22 at 13:24
  • 1
    If your x's don't start with `0`, you also need to tell that to the `plot` command. `xs = np.arange(1, len(cscResidualError)+1); ax.plot(xs, cscResidualError, ...); ax.set_xticks(xs)`. Also note that your call to `plt.figure()` creates a new empty figure which will be the one saved by `plt.savefig()`. – JohanC Oct 06 '22 at 14:39

0 Answers0