Matplotlib 3d - setting ticks for another axis - python-3.x

How to set ticks to be on the opposite axes please? When I drop it the axes changes but not in this way. Thank you
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.show()

Related

How to color space between two lines with a colour transition?

How to colour space between two lines with a colour transition? For instance, with grey colour - the dark grey from the upper line should become lighter as proceeding to the lower line. Thank you
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
x = np.linspace(0, 1, 100)
y = 0.3*x
ax.set_ylim(-0.2, 0.6)
ax.plot(x, y)
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
ax.plot(x, y - 0.1*width_l)
plt.show()
Edit
And this, please? How to make the width of the coloured part the same?
import matplotlib.pyplot as plt
import numpy as np
import pywt
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
wavelet = pywt.ContinuousWavelet('morl')
psi, x = wavelet.wavefun(level=2)
cmap = plt.get_cmap('Greys_r')
ax.plot(x, psi)
ax.set_xlim(ax.get_xlim()[0], ax.get_xlim()[1])
y_a = ax.get_ylim()
ax.set_ylim(y_a[0],y_a[1]*1.3)
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
x_range = ax.get_xlim()[1] - ax.get_xlim()[0]
x_shift = x_range * 0.1
ax.plot([x[0]+x_shift, x[1]+x_shift], [psi[0], psi[1]])
ax.plot([x[2]-x_shift, x[3]-x_shift], [psi[2], psi[3]])
ax.plot([x[1], x[2]], [psi[1]-width_l*0.1, psi[2]-width_l*0.1])
for t in np.linspace(0, 1, 40):
ax.plot(x, psi - t * 0.1 * width_l, color=cmap(t/2 + 0.25))
plt.show()
You could draw a lot of parallel lines (or curves) using a color from a gray-scale colormap. The example code below uses a transformation u = t/2 + 0.25, so when t goes from 0 to 1, u would just go between 0.25 and 0.75 to select of specific range from the colormap, avoiding the very dark and very light parts.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
x = np.linspace(0, 1, 100)
y = 0.3 * x
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
ax.set_ylim(-0.2, 0.6)
cmap = plt.get_cmap('Greys_r')
for t in np.linspace(0, 1, 40):
u = t/2 + 0.25
ax.plot(x, y - t * 0.1 * width_l, color=cmap(u))
ax.plot(x, y)
ax.plot(x, y - 0.1 * width_l)
plt.show()

How to set linewidth of axis in 3d plot in python?

How to set linewidth of axis in 3d plot in python? Is it somehow possible with mpl.rcParams?
Code:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.show()
Try this:
ax.plot_surface(X, Y, Z, linewidth=1)

Can't plot anything with matplotlib

Whenever I try to plot something with matplotlib, I get the following error:
File "C:\Users\username\AppData\Local\Programs\Python\Python37-32\Lib\tkinter\__init__.py", line 2018, in __init__
baseName = os.path.basename(sys.argv[0])
builtins.IndexError: list index out of range
For example, i've tried the following code:
import matplotlib.pyplot as plt
import numpy as np
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
I have the latest version of matplotlib, please help.
Thank you
You need to import numpy.
import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

matplotlib: put two x-y plots in one

I am using the following codes to plot several data points (xi, yi)
import numpy as np
import matplotlib.pyplot as plt
xi = np.array(data_df[['col_A']])
yi = np.array(data_df[['col_B']])
plt.figure()
plt.plot(xi, yi)
x = np.linspace(0, 30, 30)
y= np.exp(x*0.16)
plt.plot(x, y)
plt.show()
I want the plot to look like this:
Thanks!
User subplots to plot more than 1 plots in 1 figure.You need to call plt.show() only once.
import numpy as np
import matplotlib.pyplot as plt
xi = np.array(data_df[['col_A']])
yi = np.array(data_df[['col_B']])
plt.figure()
plt.subplot(2,1,1)
plt.plot(xi, yi)
plt.subplot(2,1,2)
x = np.linspace(0, 30, 30)
y= np.exp(x*0.16)
plt.plot(x, y)
plt.show()

How to draw a graph using matplotlib?

draw a graph of equation in the form of y=mx+b in python3.x
example y = 5x + 9
This is a very general question. Try to be more specific. It depends how you want to draw it.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0., 5., 0.2)
y = 5 * x + 9
plt.plot(x, y)
plt.show()
or
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-1., 5., 0.2)
y = 5 * x + 9
fig, ax = plt.subplots()
ax.plot(x,y)
ax.grid(True, which='both')
ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
These are very basic drawing. You can create more sophisticated graphs, but you will have to be more specific in your question.
You can define your y(x) function and then plot it as follows:
import matplotlib.pyplot as plt
def y(x):
return [5*i+9 for i in x]
x = range(0,10)
plt.plot(x,y(x))
plt.show()
This produces follwing graph:
With turtle
You can as well get a graph with turtle with following code for example:
from turtle import Turtle, Screen
def y(x):
return 5*x+9
def plotter(turtle, x_range):
turtle.penup()
for x in x_range:
turtle.goto(x, y(x))
turtle.pendown()
screen = Screen()
screen.setworldcoordinates(0, 0, 9, 60)
turtle = Turtle(visible=False)
x = range(0,10)
plotter(turtle, x)
screen.exitonclick()
Which produces:

Resources