Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python legend in 3dplot

I am plotting a 3d plot in python 2.7

When I try to plot a 3d plot with color and marker as in 2D plot() function. I come across an error.

So I tried to plot line separately and measured points with markers separately using scatter() function.

When I create legend entries my legend looks like this

enter image description here

But I don't want to have duplicate legend entries instead

  • I want my legend entries to group with colour, or
  • Is it possible have both marker and line as a single entry so that there are only 5 entries in my legend

I found a similar question to this (How to make custom legend in matplotlib) but it does not solve my problem

I am appending a code similar to my problem

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 1')
ax.scatter(x, y, z, label='parametric curve 1',marker = 'o')
x = r * np.sin(theta + 1)
y = r * np.cos(theta + 1)
ax.plot(x, y, z, label='parametric curve 2')
ax.scatter(x, y, z, label='parametric curve 2',marker = 'o')
ax.legend()

plt.show()

The above code gives me a plot shown below Plot

But I want my legend to have only two entries

like image 410
sristisravan Avatar asked Jan 12 '16 16:01

sristisravan


People also ask

How do you plot 3 axis in Python?

Using subplots() method, create a figure and a set of subplots. Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales. Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2. Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.

How do I mark a legend in Matplotlib?

In the matplotlib library, there's a function called legend() which is used to Place a legend on the axes. The attribute Loc in legend() is used to specify the location of the legend. Default value of loc is loc=”best” (upper left).


1 Answers

Are you using the standard Matplotlib library to generate these 3D plots? If so, starting from the example in the documentation (http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#line-plots) it seems to work fine:

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 1', marker='o')
x = r * np.sin(theta + 1)
y = r * np.cos(theta + 1)
ax.plot(x, y, z, label='parametric curve 2', marker='o')
ax.legend()

plt.show()

enter image description here

like image 102
Ferdinand van Wyk Avatar answered Sep 19 '22 03:09

Ferdinand van Wyk