Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib 2d line line,=plot comma meaning

I'm walking through basic tutorials for matplotlib, and the example code that I'm working on is:

import numpy as np

import matplotlib.pylab as plt

x=[1,2,3,4]
y=[5,6,7,8]

line, = plt.plot(x,y,'-')

plt.show()

Does anyone know what the comma after line (line,=plt.plot(x,y,'-')) means? I thought it was a typo but obviously the whole code doesn't work if I omit the comma.

like image 314
user2418838 Avatar asked May 24 '13 20:05

user2418838


People also ask

What is Matplotlib plot a line in Python?

Matplotlib plot a line in 3D Matplotlib is the widely used data visualization library in Python. It provides a variety of plots and data visualization tools to create 2D plots from the data in lists or arrays in python. Matplotlib is a cross-platform library built on NumPy arrays.

How to plot simple line chart without any feature in Matplotlib?

# Importing packages import matplotlib.pyplot as plt # Define x and y values x = y = # Plot a simple line chart without any feature plt.plot (x, y) plt.show ()

How do I change the color of a line in Matplotlib?

Matplotlib plot line color You can change the line color in a line chart in python using matplotlib. You need to specify the parameter color in the plot () function of matplotlib. There are several colors available in python.

How to plot vertical line at a date in Matplotlib Python?

Matplotlib plot vertical line at date You can add dates as ticklabels and can plot vertical lines at a date in matplotlib python. You need to import the datetimes function from datetime module in python for creating the date-formatted values. You need mdates sub-module from the matplotlib.dates to format the dates for the plot.


1 Answers

plt.plot returns a list of the Line2D objects plotted, even if you plot only one line.

That comma is unpacking the single value into line.

ex

a, b = [1, 2]
a, = [1, ]
like image 107
tacaswell Avatar answered Oct 20 '22 11:10

tacaswell