Python plot multiple lines

Multiple plots in one figure in Python

I am new to python and am trying to plot multiple lines in the same figure using matplotlib. The value of my Y-axis is stored in a dictionary and I make corresponding values in X-axis in the following code My code is like this:

for i in range(len(ID)): AxisY= PlotPoints[ID[i]] if len(AxisY)> 5: AxisX= [len(AxisY)] for i in range(1,len(AxisY)): AxisX.append(AxisX[i-1]-1) plt.plot(AxisX,AxisY) plt.xlabel('Lead Time (in days)') plt.ylabel('Proportation of Events Scheduled') ax = plt.gca() ax.invert_xaxis() ax.yaxis.tick_right() ax.yaxis.set_label_position("right") plt.show() 

But I am getting separate figures with a single plot one by one. Can anybody help me figure out what is wrong with my code? Why can’t I produce multiple-line plotting? Thanks a lot!

4 Answers 4

This is very simple to do:

import matplotlib.pyplot as plt plt.plot(, , 'line type', label='label here') plt.plot(, , 'line type', label='label here') plt.legend(loc='best') plt.show() 

You can keep adding plt.plot as many times as you like. As for line type , you need to first specify the color. So for blue, it’s b . And for a normal line it’s — . An example would be:

plt.plot(total_lengths, sort_times_heap, 'b-', label="Heap") 

Since I don’t have a high enough reputation to comment I’ll answer liang question on Feb 20 at 10:01 as an answer to the original question.

In order for the for the line labels to show you need to add plt.legend to your code. to build on the previous example above that also includes title, ylabel and xlabel:

import matplotlib.pyplot as plt plt.plot(, , 'line type', label='label here') plt.plot(, , 'line type', label='label here') plt.title('title') plt.ylabel('ylabel') plt.xlabel('xlabel') plt.legend() plt.show() 

EDIT: I just realised after reading your question again, that i did not answer your question. You want to enter multiple lines in the same plot. However, I’ll leave it be, because this served me very well multiple times. I hope you find usefull someday

Читайте также:  Javascript createelement script tag

I found this a while back when learning python

import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = plt.figure() # create figure window gs = gridspec.GridSpec(a, b) # Creates grid 'gs' of a rows and b columns ax = plt.subplot(gs[x, y]) # Adds subplot 'ax' in grid 'gs' at position [x,y] ax.set_ylabel('Foo') #Add y-axis label 'Foo' to graph 'ax' (xlabel for x-axis) fig.add_subplot(ax) #add 'ax' to figure 

you can make different sizes in one figure as well, use slices in that case:

 gs = gridspec.GridSpec(3, 3) ax1 = plt.subplot(gs[0,:]) # row 0 (top) spans all(3) columns 

consult the docs for more help and examples. This little bit i typed up for myself once, and is very much based/copied from the docs as well. Hope it helps. I remember it being a pain in the #$% to get acquainted with the slice notation for the different sized plots in one figure. After that i think it’s very simple 🙂

Источник

Matplotlib: Plot Multiple Line Plots On Same and Different Scales

Matplotlib is one of the most widely used data visualization libraries in Python. From simple to complex visualizations, it’s the go-to library for most.

In this tutorial, we’ll take a look at how to plot multiple line plots in Matplotlib — on the same Axes or Figure .

If you’d like to read more about plotting line plots in general, as well as customizing them, make sure to read our guide on Plotting Lines Plots with Matplotlib.

Plot Multiple Line Plots in Matplotlib

Depending on the style you’re using, OOP or MATLAB-style, you’ll either use the plt instance, or the ax instance to plot, with the same approach.

To plot multiple line plots in Matplotlib, you simply repeatedly call the plot() function, which will apply the changes to the same Figure object:

import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 6] y = [2, 4, 6, 5, 6, 8] y2 = [5, 3, 7, 8, 9, 6] fig, ax = plt.subplots() ax.plot(x, y) ax.plot(x, y2) plt.show() 

Without setting any customization flags, the default colormap will apply, drawing both line plots on the same Figure object, and adjusting the color to differentiate between them:

Now, let’s generate some random sequences using NumPy, and customize the line plots a tiny bit by setting a specific color for each, and labeling them:

import matplotlib.pyplot as plt import numpy as np line_1 = np.random.randint(low = 0, high = 50, size = 50) line_2 = np.random.randint(low = -15, high = 100, size = 50) fig, ax = plt.subplots() ax.plot(line_1, color = 'green', label = 'Line 1') ax.plot(line_2, color = 'red', label = 'Line 2') ax.legend(loc = 'upper left') plt.show() 

We don’t have to supply the X-axis values to a line plot, in which case, the values from 0..n will be applied, where n is the last element in the data you’re plotting. In our case, we’ve got two sequences of data — line_1 and line_2 , which will both be plotted on the same X-axis.

While plotting, we’ve assigned colors to them, using the color argument, and labels for the legend, using the label argument. This results in:

Plot Multiple Line Plots with Different Scales

Sometimes, you might have two datasets, fit for line plots, but their values are significantly different, making it hard to compare both lines. For example, if line_1 had an exponentially increasing sequence of numbers, while line_2 had a linearly increasing sequence — surely and quickly enough, line_1 would have values so much larger than line_2 , that the latter fades out of view.

Let’s use NumPy to make an exponentially increasing sequence of numbers, and plot it next to another line on the same Axes , linearly:

import matplotlib.pyplot as plt import numpy as np linear_sequence = np.linspace(0, 10, 10) # [0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 10] exponential_sequence = np.exp(linear_sequence) # [1.00e+00, 3.03e+00, 9.22e+00, 2.80e+01, 8.51e+01, 2.58e+02, 7.85e+02, 2.38e+03, 7.25e+03, 2.20e+04] fig, ax = plt.subplots() ax.plot(linear_sequence) ax.plot(exponential_sequence) plt.show() 

Running this code results in:

The exponential growth in the exponential_sequence goes out of proportion very fast, and it looks like there’s absolutely no difference in the linear_sequence , since it’s so minuscule relative to the exponential trend of the other sequence.

Now, let’s plot the exponential_sequence on a logarithmic scale, which will produce a visually straight line, since the Y-scale will exponentially increase. If we plot it on a logarithmic scale, and the linear_sequence just increases by the same constant, we’ll have two overlapping lines and we will only be able to see the one plotted after the first.

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Let’s change up the linear_sequence a bit to make it observable once we plot both:

import matplotlib.pyplot as plt import numpy as np # Sequences linear_sequence = [1, 2, 3, 4, 5, 6, 7, 10, 15, 20] exponential_sequence = np.exp(np.linspace(0, 10, 10)) fig, ax = plt.subplots() # Plot linear sequence, and set tick labels to the same color ax.plot(linear_sequence, color='red') ax.tick_params(axis='y', labelcolor='red') # Generate a new Axes instance, on the twin-X axes (same position) ax2 = ax.twinx() # Plot exponential sequence, set scale to logarithmic and change tick color ax2.plot(exponential_sequence, color='green') ax2.set_yscale('log') ax2.tick_params(axis='y', labelcolor='green') plt.show() 

This time around, we’ll have to use the OOP interface, since we’re creating a new Axes instance. One Axes has one scale, so we create a new one, in the same position as the first one, and set its scale to a logarithmic one, and plot the exponential sequence.

We’ve also changed the tick label colors to match the color of the line plots themselves, otherwise, it’d be hard to distinguish which line is on which scale.

Plot Multiple Line Plots with Multiple Y-Axis

Finally, we can apply the same scale (linear, logarithmic, etc), but have different values on the Y-axis of each line plot. This is achieved through having multiple Y-axis, on different Axes objects, in the same position.

For example, the linear_sequence won’t go above 20 on the Y-axis, while the exponential_sequence will go up to 20000. We can plot them both linearly, simply by plotting them on different Axes objects, in the same position, each of which set the Y-axis ticks automatically to accommodate for the data we’re feeding in:

import matplotlib.pyplot as plt import numpy as np # Sequences linear_sequence = [1, 2, 3, 4, 5, 6, 7, 10, 15, 20] exponential_sequence = np.exp(np.linspace(0, 10, 10)) fig, ax = plt.subplots() # Plot linear sequence, and set tick labels to the same color ax.plot(linear_sequence, color='red') ax.tick_params(axis='y', labelcolor='red') # Generate a new Axes instance, on the twin-X axes (same position) ax2 = ax.twinx() # Plot exponential sequence, set scale to logarithmic and change tick color ax2.plot(exponential_sequence, color='green') ax2.tick_params(axis='y', labelcolor='green') plt.show() 

We’ve again created another Axes in the same position as the first one, so we can plot on the same place in the Figure but different Axes objects, which allows us to set values for each Y-axis individually.

Without setting the Y-scale to logarithmic this time, both will be plotted linearly:

Conclusion

In this tutorial, we’ve gone over how to plot multiple Line Plots on the same Figure or Axes in Matplotlib and Python. We’ve covered how to plot on the same Axes with the same scale and Y-axis, as well as how to plot on the same Figure with different and identical Y-axis scales.

If you’re interested in Data Visualization and don’t know where to start, make sure to check out our bundle of books on Data Visualization in Python:

Data Visualization in Python

Become dangerous with Data Visualization

✅ 30-day no-question money-back guarantee

✅ Updated regularly for free (latest update in April 2021)

✅ Updated with bonus resources and guides

Data Visualization in Python with Matplotlib and Pandas is a book designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and allow them to build a strong foundation for advanced work with these libraries — from simple plots to animated 3D plots with interactive buttons.

It serves as an in-depth guide that’ll teach you everything you need to know about Pandas and Matplotlib, including how to construct plot types that aren’t built into the library itself.

Data Visualization in Python, a book for beginner to intermediate Python developers, guides you through simple data manipulation with Pandas, covers core plotting libraries like Matplotlib and Seaborn, and shows you how to take advantage of declarative and experimental libraries like Altair. More specifically, over the span of 11 chapters this book covers 9 Python libraries: Pandas, Matplotlib, Seaborn, Bokeh, Altair, Plotly, GGPlot, GeoPandas, and VisPy.

It serves as a unique, practical guide to Data Visualization, in a plethora of tools you might use in your career.

Источник

Оцените статью