# Python plotting

## The 7 most popular ways to plot data in Python | Opensource.com

"How do I make plots in Python?" used to have a simple answer: Matplotlib was the only way. Nowadays, Python is the language of data science, and there's a lot more choice. What should you use?

This guide will help you decide. It will show you how to use each of the four most popular Python plotting libraries—Matplotlib, Seaborn, Plotly, and Bokeh—plus a couple of great up-and-comers to consider: Altair, with its expressive API, and Pygal, with its beautiful SVG output. I'll also look at the very convenient plotting API provided by pandas.

For each library, I've included source code snippets, as well as a full web-based example using Anvil, our platform for building web apps with nothing but Python. Let's take a look.

### An example plot

Each library takes a slightly different approach to plotting data. To compare them, I'll make the same plot with each library and show you the source code. For my example data, I chose this grouped bar chart of British election results since 1966:

I compiled the dataset of British election history from Wikipedia: the number of seats in the UK parliament won by the Conservative, Labour, and Liberal parties (broadly defined) in each election from 1966 to 2019, plus the number of seats won by "others." You can download it as a CSV file.

### Matplotlib

Matplotlib is the oldest Python plotting library, and it's still the most popular. It was created in 2003 as part of the SciPy Stack, an open source scientific computing library similar to Matlab.

Matplotlib gives you precise control over your plots—for example, you can define the individual x-position of each bar in your barplot. Here is the code to graph this (which you can run here):

import matplotlib.pyplotas plt
import numpy as np
from votes import wide as df

# Initialise a figure. subplots() with no args gives one plot.
fig, ax = plt.subplots()

# A little data preparation
years = df['year']
x = np.arange(len(years))

# Plot each bar plot. Note: manually calculating the 'dodges' of the bars
ax.bar(x - 3*width/2, df['conservative'], width, label='Conservative', color='#0343df')
ax.bar(x - width/2, df['labour'], width, label='Labour', color='#e50000')
ax.bar(x + width/2, df['liberal'], width, label='Liberal', color='#ffff14')
ax.bar(x + 3*width/2, df['others'], width, label='Others', color='#929591')

# Customise some display properties
ax.set_ylabel('Seats')
ax.set_title('UK election results')
ax.set_xticks(x)    # This ensures we have one tick per year, otherwise we get fewer
ax.set_xticklabels(years.astype(str).values, rotation='vertical')
ax.legend()

# Ask Matplotlib to show the plot
plt.show()

And here are the election results plotted in Matplotlib:

### Seaborn

Seaborn is an abstraction layer on top of Matplotlib; it gives you a really neat interface to make a wide range of useful plot types very easily.

It doesn't compromise on power, though! Seaborn gives escape hatches to access the underlying Matplotlib objects, so you still have complete control.

Seaborn's code is simpler than the raw Matplotlib (runnable here):

import seaborn as sns

# Some boilerplate to initialise things
sns.set()
plt.figure()

# This is where the actual plot gets made
ax = sns.barplot(data=df, x="year", y="seats", hue="party", palette=['blue','red','yellow','grey'], saturation=0.6)

# Customise some display properties
ax.set_title('UK election results')
ax.grid(color='#cccccc')
ax.set_ylabel('Seats')
ax.set_xlabel(None)
ax.set_xticklabels(df["year"].unique().astype(str), rotation='vertical')

# Ask Matplotlib to show it
plt.show()

And produces this chart:

### Plotly

Plotly is a plotting ecosystem that includes a Python plotting library. It has three different interfaces:

• An object-oriented interface
• An imperative interface that allows you to specify your plot using JSON-like data structures
• A high-level interface similar to Seaborn called Plotly Express

Plotly plots are designed to be embedded in web apps. At its core, Plotly is actually a JavaScript library! It uses D3 and stack.gl to draw the plots.

You can build Plotly libraries in other languages by passing JSON to the JavaScript library. The official Python and R libraries do just that. At Anvil, we ported the Python Plotly API to run in the web browser.

Here's the source code in Plotly (which you can run here):

import plotly.graph_objectsas go
from votes import wide as df

#  Get a convenient list of x-values
years = df['year']
x =list(range(len(years)))

# Specify the plots
bar_plots =[
go.Bar(x=x, y=df['conservative'], name='Conservative', marker=go.bar.Marker(color='#0343df')),
go.Bar(x=x, y=df['labour'], name='Labour', marker=go.bar.Marker(color='#e50000')),
go.Bar(x=x, y=df['liberal'], name='Liberal', marker=go.bar.Marker(color='#ffff14')),
go.Bar(x=x, y=df['others'], name='Others', marker=go.bar.Marker(color='#929591')),
]

# Customise some display properties
layout = go.Layout(
title=go.layout.Title(text="Election results", x=0.5),
yaxis_title="Seats",
xaxis_tickmode="array",
xaxis_tickvals=list(range(27)),
xaxis_ticktext=tuple(df['year'].values),
)

# Make the multi-bar plot
fig = go.Figure(data=bar_plots, layout=layout)

# Tell Plotly to render it
fig.show()

And the election results plot:

### Bokeh

Bokeh (pronounced "BOE-kay") specializes in building interactive plots, so this standard example doesn't show it off to its best. Like Plotly, Bokeh's plots are designed to be embedded in web apps; it outputs its plots as HTML files.

Here is the code in Bokeh (which you can run here)

from bokeh.ioimport show, output_file
from bokeh.modelsimport ColumnDataSource, FactorRange, HoverTool
from bokeh.plottingimport figure
from bokeh.transformimport factor_cmap

# Specify a file to write the plot to
output_file("elections.html")

# Tuples of groups (year, party)
x =[(str(r['year']), r['party'])for r in df.iterrows()]
y = df['seats']

# Bokeh wraps your data in its own objects to support interactivity
source = ColumnDataSource(data=dict(x=x, y=y))

# Create a colourmap
cmap ={
'Conservative': '#0343df',
'Labour': '#e50000',
'Liberal': '#ffff14',
'Others': '#929591',
}
fill_color = factor_cmap('x', palette=list(cmap.values()), factors=list(cmap.keys()), start=1, end=2)

# Make the plot
p = figure(x_range=FactorRange(*x), width=1200, title="Election results")
p.vbar(x='x', top='y', width=0.9, source=source, fill_color=fill_color, line_color=fill_color)

# Customise some display properties
p.y_range.start=0
p.yaxis.axis_label='Seats'
p.xaxis.major_label_orientation=1
p.xgrid.grid_line_color=None

And the plot:

### Altair

Altair is based on a declarative plotting language (or "visualization grammar") called Vega. This means it's a well-thought-through API that scales well for complex plots, saving you from getting lost in nested-for-loop hell.

As with Bokeh, Altair outputs its plots as HTML files. Here's the code (which you can run here):

import altair as alt

# Set up the colourmap
cmap ={
'Conservative': '#0343df',
'Labour': '#e50000',
'Liberal': '#ffff14',
'Others': '#929591',
}

# Cast years to strings
df['year']= df['year'].astype(str)

# Here's where we make the plot
chart = alt.Chart(df).mark_bar().encode(
x=alt.X('party', title=None),
y='seats',
column=alt.Column('year', sort=list(df['year']), title=None),
color=alt.Color('party', scale=alt.Scale(domain=list(cmap.keys()),range=list(cmap.values())))
)

# Save it as an HTML file.
chart.save('altair-elections.html')

And the resulting chart:

### Pygal

Pygal focuses on visual appearance. It produces SVG plots by default, so you can zoom them forever or print them out without them getting pixellated. Pygal plots also come with some good interactivity features built-in, making Pygal another underrated candidate if you're looking to embed plots in a web app.

The source code looks like this (and you can run it here):

import pygal
from pygal.styleimport Style
from votes import wide as df

# Define the style
custom_style = Style(
colors=('#0343df','#e50000','#ffff14','#929591')
font_family='Roboto,Helvetica,Arial,sans-serif',
background='transparent',
label_font_size=14,
)

# Set up the bar plot, ready for data
c = pygal.Bar(
title="UK Election Results",
style=custom_style,
y_title='Seats',
width=1200,
x_label_rotation=270,
)

# Add four data sets to the bar plot

# Define the X-labels
c.x_labels= df['year']

# Write this to an SVG file
c.render_to_file('pygal.svg')

And the chart:

### Pandas

Pandas is an extremely popular data science library for Python. It allows you to do all sorts of data manipulation scalably, but it also has a convenient plotting API. Because it operates directly on data frames, the pandas example is the most concise code snippet in this article—even shorter than the Seaborn code!

The pandas API is a wrapper around Matplotlib, so you can also use the underlying Matplotlib API to get fine-grained control of your plots.

Here's the election results plot in pandas. The code is beautifully concise!

from matplotlib.colorsimport ListedColormap
from votes import wide as df

cmap = ListedColormap(['#0343df','#e50000','#ffff14','#929591'])

ax = df.plot.bar(x='year', colormap=cmap)

ax.set_xlabel(None)
ax.set_ylabel('Seats')
ax.set_title('UK election results')

plt.show()

And the resulting chart:

To run this example, check out here.

Python offers many ways to plot the same data without much code. While you can get started quickly creating charts with any of these methods, they do take some local configuration. Anvil offers a beautiful web-based experience for Python development if you're in need. Happy plotting!

Sours: https://opensource.com/article/20/4/plot-data-python

## Matplotlib: Visualization with Python¶

Matplotlib is a welcoming, inclusive project, and everyone within the community is expected to abide by our code of conduct.

### Get help  If you have questions, be sure to check the FAQ, the API docs. The full text search is a good way to discover the docs including the many examples.

### News To keep up to date with what's going on in Matplotlib, see the what's new page or browse the source code. Anything that could require changes to your existing code is logged in the API changes file. ### Development Matplotlib is hosted on GitHub.

It is a good idea to ping us on Discourse as well. Mailing lists

### Citing Matplotlib¶

Matplotlib is the brainchild of John Hunter (1968-2012), who, along with its many contributors, have put an immeasurable amount of time and effort into producing a piece of software utilized by thousands of scientists worldwide.

If Matplotlib contributes to a project that leads to a scientific publication, please acknowledge this work by citing the project. A ready-made citation entry is available.

### Open source¶ Matplotlib is a Sponsored Project of NumFOCUS, a 501(c)(3) nonprofit charity in the United States. NumFOCUS provides Matplotlib with fiscal, legal, and administrative support to help ensure the health and sustainability of the project. Visit numfocus.org for more information.

Donations to Matplotlib are managed by NumFOCUS. For donors in the United States, your gift is tax-deductible to the extent provided by law. As with any donation, you should consult with your tax adviser about your particular tax situation.

Please consider donating to the Matplotlib project through the NumFOCUS organization or to the John Hunter Technology Fellowship.

The Matplotlib license is based on the Python Software Foundation (PSF) license.

There is an active developer community and a long list of people who have made significant contributions.

Sours: https://matplotlib.org/

## Earth Data Analytics Online Certificate

Jenny Palomino, Leah Wasser

### Chapter One - Introduction to Plotting with Matplotlib

In this chapter, you will learn how to create and customize plots in Python using matplotlib, including how to create different types of plots and customize plot colors and labels.

### Learning Objectives

After completing this chapter, you will be able to:

• Explain the matplotlib object-oriented approach to plotting.
• Use matplotlib to create scatter, line and bar plots.
• Customize the labels, colors and look of your matplotlib plot.
• Create figures with multiple plots.
• Save figure as an image file (e.g. .png format).

### What You Need

Be sure that you have followed the instructions on Setting up Git, Bash, and Conda on your computer to install the tools for your operating system (Windows, Mac, Linux).

Be sure that you have completed the chapter on Jupyter Notebook.

### Overview of Plotting with Matplotlib

Matplotlib is a Python plotting package that makes it simple to create two-dimensional plots from data stored in a variety of data structures including lists, numpy arrays, and pandas dataframes.

Matplotlib uses an object oriented approach to plotting. This means that plots can be built step-by-step by adding new elements to the plot.

There are two primary objects associated with a matplotlib plot:

• object: the overall figure space that can contain one or more plots.
• objects: the individual plots that are rendered within the figure.

You can think of the figure object as your plot canvas. You can think about the axis object as an individual plot.

A figure can hold one or more axis objects. This structure allows you to create figures with one or more plots on them.

While Matplotlib contains many modules that provide different plotting functionality, the most commonly used module is pyplot.

Pyplot provides methods that can be used to add different components to objects, including creating the individual plots as objects, also known as subplots.

The pyplot module is typically imported using the alias as demonstrated below.

### Create Plots Using Matplotlib

To create a plot using matplotlib’s object oriented approach, you first create the figure (which you can call ) and at least one axis (which you can call ) using the function from the module:

Notice that the and are created at the same time by setting them equal to the output of the function. As no other arguments have been provided, the result is a figure with one plot that is empty but ready for data.

Data Tip: In the example above, and are variable names for the figure and axis objects. You can call these items whatever you want. For example, you might see , or , used.

### Change Figure Size

You can change the size of your figure using the argument to specify a width and height for your figure:

### Multi-plot Figures

Using matplotlib’s object-oriented approach makes it easier to include more than one plot in a figure by creating additional objects.

When adding more than one object, it is good practice to give them distinct names (such as and ), so you can easily work with each individually.

You will need to provide new arguments to for the layout of the figure: number of rows and columns:

In this example, indicates that you want the plot layout to be 1 row across 2 columns.

Conversely, indicates that you want the plot layout to be 2 rows across one column.

Because you have defined , the figure space remains the same size regardless of how many rows or columns you request.

You can play around with both the number of rows and columns as well as to arrive at the look that you want.

You can continue to add as many as objects as you need to create the overall layout of the desired figure and continue adjusting the as needed.

A key benefit of the matplotlib object oriented approach is that each is its own object and can be customized independently of the other plots in the figure.

You will learn how to take advantage of this capability to customize individual plots on the next page of this chapter.

Customize Plots with Matplotlib

Tags Reproducible science and programming: pythonData exploration and analysis: data visualization

Updated:

#### Share on Sours: https://www.earthdatascience.org/courses/scientists-guide-to-plotting-data-in-python/plot-with-matplotlib/introduction-to-matplotlib-plots/

## Top 50 matplotlib Visualizations – The Master Plots (with full python code)

A compilation of the Top 50 matplotlib plots most useful in data analysis and visualization. This list lets you choose what visualization to show for what situation using python’s matplotlib and seaborn library.

### Introduction

The charts are grouped based on the 7 different purposes of your visualization objective. For example, if you want to picturize the relationship between 2 variables, check out the plots under the ‘Correlation’ section. Or if you want to show how a value changed over time, look under the ‘Change’ section and so on.

An effective chart is one which:

1. Conveys the right and necessary information without distorting facts.
2. Simple in design, you don’t have to strain in order to get it.
3. Aesthetics support the information rather than overshadow it.

__Related Posts: __
Matplotlib Full Tutorial
Matplotlib Subplots

### Contents

(right click and open in new page if the links don’t work)

Correlation

1. Scatter plot
2. Bubble plot with Encircling
3. Scatter plot with line of best fit
4. Jittering with stripplot
5. Counts Plot
6. Marginal Histogram
7. Marginal Boxplot
8. Correlogram
9. Pairwise Plot

Deviation

1. Diverging Bars
2. Diverging Texts
3. Diverging Dot Plot
4. Diverging Lollipop Chart with Markers
5. Area Chart

Ranking

1. Ordered Bar Chart
2. Lollipop Chart
3. Dot Plot
4. Slope Chart
5. Dumbbell Plot

Distribution

1. Histogram for Continuous Variable
2. Histogram for Categorical Variable
3. Density Plot
4. Density Curves with Histogram
5. Joy Plot
6. Distributed Dot Plot
7. Box Plot
8. Dot + Box Plot
9. Violin Plot
10. Population Pyramid
11. Categorical Plots

Composition

1. Waffle Chart
2. Pie Chart
3. Treemap
4. Bar Chart

Change

1. Time Series Plot
2. Time Series with Peaks and Troughs Annotated
3. Autocorrelation Plot
4. Cross Correlation Plot
5. Time Series Decomposition Plot
6. Multiple Time Series
7. Plotting with different scales using secondary Y axis
8. Time Series with Error Bands
9. Stacked Area Chart
10. Area Chart Unstacked
11. Calendar Heat Map
12. Seasonal Plot

Groups

1. Dendrogram
2. Cluster Plot
3. Andrews Curve
4. Parallel Coordinates

### Setup

Run this once before the plot’s code. The individual charts, however, may redefine its own aesthetics.

### Correlation

The plots under correlation is used to visualize the relationship between 2 or more variables. That is, how does one variable change with respect to another.

### 1. Scatter plot

Scatteplot is a classic and fundamental plot used to study the relationship between two variables. If you have multiple groups in your data you may want to visualise each group in a different color. In , you can conveniently do this using .

Show Code ### 2. Bubble plot with Encircling

Sometimes you want to show a group of points within a boundary to emphasize their importance. In this example, you get the records from the dataframe that should be encircled and pass it to the described in the code below.

Show Code ### 3. Scatter plot with linear regression line of best fit

If you want to understand how two variables change with respect to each other, the line of best fit is the way to go. The below plot shows how the line of best fit differs amongst various groups in the data. To disable the groupings and to just draw one line-of-best-fit for the entire dataset, remove the parameter from the call below.

Show Code ### Each regression line in its own column

Alternately, you can show the best fit line for each group in its own column. You cando this by setting the parameter inside the .

Show Code ### 4. Jittering with stripplot

Often multiple datapoints have exactly the same X and Y values. As a result, multiple points get plotted over each other and hide. To avoid this, jitter the points slightly so you can visually see them. This is convenient to do using seaborn’s .

Show Code ### 5. Counts Plot

Another option to avoid the problem of points overlap is the increase the size of the dot depending on how many points lie in that spot. So, larger the size of the point more is the concentration of points around that. ### 6. Marginal Histogram

Marginal histograms have a histogram along the X and Y axis variables. This is used to visualize the relationship between the X and Y along with the univariate distribution of the X and the Y individually. This plot if often used in exploratory data analysis (EDA).

Show Code ### 7. Marginal Boxplot

Marginal boxplot serves a similar purpose as marginal histogram. However, the boxplot helps to pinpoint the median, 25th and 75th percentiles of the X and the Y.

Show Code ### 8. Correllogram

Correlogram is used to visually see the correlation metric between all possible pairs of numeric variables in a given dataframe (or 2D array). ### 9. Pairwise Plot

Pairwise plot is a favorite in exploratory analysis to understand the relationship between all possible pairs of numeric variables. It is a must have tool for bivariate analysis.  ### 10. Diverging Bars

If you want to see how the items are varying based on a single metric and visualize the order and amount of this variance, the diverging bars is a great tool. It helps to quickly differentiate the performance of groups in your data and is quite intuitive and instantly conveys the point. ### 11. Diverging Texts

Diverging texts is similar to diverging bars and it preferred if you want to show the value of each items within the chart in a nice and presentable way. ### 12. Diverging Dot Plot

Divering dot plot is also similar to the diverging bars. However compared to diverging bars, the absence of bars reduces the amount of contrast and disparity between the groups.

Show Code ### 13. Diverging Lollipop Chart with Markers

Lollipop with markers provides a flexible way of visualizing the divergence by laying emphasis on any significant datapoints you want to bring attention to and give reasoning within the chart appropriately.

Show Code ### 14. Area Chart

By coloring the area between the axis and the lines, the area chart throws more emphasis not just on the peaks and troughs but also the duration of the highs and lows. The longer the duration of the highs, the larger is the area under the line.

Show Code ### 15. Ordered Bar Chart

Ordered bar chart conveys the rank order of the items effectively. But adding the value of the metric above the chart, the user gets the precise information from the chart itself.

Show Code ### 16. Lollipop Chart

Lollipop chart serves a similar purpose as a ordered bar chart in a visually pleasing way.

Show Code ### 17. Dot Plot

The dot plot conveys the rank order of the items. And since it is aligned along the horizontal axis, you can visualize how far the points are from each other more easily. ### 18. Slope Chart

Slope chart is most suitable for comparing the ‘Before’ and ‘After’ positions of a given person/item.

Show Code ### 19. Dumbbell Plot

Dumbbell plot conveys the ‘before’ and ‘after’ positions of various items along with the rank ordering of the items. Its very useful if you want to visualize the effect of a particular project / initiative on different objects.

Show Code ### 20. Histogram for Continuous Variable

Histogram shows the frequency distribution of a given variable. The below representation groups the frequency bars based on a categorical variable giving a greater insight about the continuous variable and the categorical variable in tandem.

Show Code ### 21. Histogram for Categorical Variable

The histogram of a categorical variable shows the frequency distribution of a that variable. By coloring the bars, you can visualize the distribution in connection with another categorical variable representing the colors.

Show Code ### 22. Density Plot

Density plots are a commonly used tool visualise the distribution of a continuous variable. By grouping them by the ‘response’ variable, you can inspect the relationship between the X and the Y. The below case if for representational purpose to describe how the distribution of city mileage varies with respect the number of cylinders.

Show Codes ### 23. Density Curves with Histogram

Density curve with histogram brings together the collective information conveyed by the two plots so you can have them both in a single figure instead of two.

Show Code ### 24. Joy Plot

Joy Plot allows the density curves of different groups to overlap, it is a great way to visualize the distribution of a larger number of groups in relation to each other. It looks pleasing to the eye and conveys just the right information clearly. It can be easily built using the package which is based on . ### 25. Distributed Dot Plot

Distributed dot plot shows the univariate distribution of points segmented by groups. The darker the points, more is the concentration of data points in that region. By coloring the median differently, the real positioning of the groups becomes apparent instantly.

Show Code ### 26. Box Plot

Box plots are a great way to visualize the distribution, keeping the median, 25th 75th quartiles and the outliers in mind. However, you need to be careful about interpreting the size the boxes which can potentially distort the number of points contained within that group. So, manually providing the number of observations in each box can help overcome this drawback.

For example, the first two boxes on the left have boxes of the same size even though they have 5 and 47 obs respectively. So writing the number of observations in that group becomes necessary. ### 27. Dot + Box Plot

Dot + Box plot Conveys similar information as a boxplot split in groups. The dots, in addition, gives a sense of how many data points lie within each group. ### 28. Violin Plot

Violin plot is a visually pleasing alternative to box plots. The shape or area of the violin depends on the number of observations it holds. However, the violin plots can be harder to read and it not commonly used in professional settings. ### 29. Population Pyramid

Population pyramid can be used to show either the distribution of the groups ordered by the volumne. Or it can also be used to show the stage-by-stage filtering of the population as it is used below to show how many people pass through each stage of a marketing funnel. ### 30. Categorical Plots

Categorical plots provided by the library can be used to visualize the counts distribution of 2 ore more categorical variables in relation to each other.

Sours: https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/

## Graph Plotting in Python | Set 1

This series will introduce you to graphing in python with Matplotlib, which is arguably the most popular graphing and data visualization library for Python.
Installation
The easiest way to install matplotlib is to use pip. Type following command in terminal:

pip install matplotlib

Getting started ( Plotting a line)

### Python

Output: The code seems self-explanatory. Following steps were followed:

• Define the x-axis and corresponding y-axis values as lists.
• Plot them on canvas using .plot() function.
• Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
• Give a title to your plot using .title() function.
• Finally, to view your plot, we use .show() function.

Plotting two or more lines on same plot

### Python

Output: • Here, we plot two lines on the same graph. We differentiate between them by giving them a name(label) which is passed as an argument of the .plot() function.
• The small rectangular box giving information about the type of line and its color is called a legend. We can add a legend to our plot using .legend() function.

Customization of Plots

Here, we discuss some elementary customizations applicable to almost any plot.

### Python

Output: As you can see, we have done several customizations like

• setting the line-width, line-style, line-color.
• setting the marker, marker’s face color, marker’s size.
• overriding the x and y-axis range. If overriding is not done, pyplot module uses the auto-scale feature to set the axis range and scale.

Bar Chart

### Python

Output : • Here, we use plt.bar() function to plot a bar chart.
• x-coordinates of the left side of bars are passed along with the heights of bars.
• you can also give some names to x-axis coordinates by defining tick_labels

Histogram

### Python

Output: • Here, we use plt.hist() function to plot a histogram.
• frequencies are passed as the ages list.
• The range could be set by defining a tuple containing min and max values.
• The next step is to “bin” the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. Here we have defined bins = 10. So, there are a total of 100/10 = 10 intervals.

Scatter plot

### Python

Output: • Here, we use plt.scatter() function to plot a scatter plot.
• As a line, we define x and corresponding y-axis values here as well.
• marker argument is used to set the character to use as a marker. Its size can be defined using the s parameter.

Pie-chart

### Python

The output of above program looks like this: • Here, we plot a pie chart by using plt.pie() method.
• First of all, we define the labels using a list called activities.
• Then, a portion of each label can be defined using another list called slices.
• Color for each label is defined using a list called colors.
• shadow = True will show a shadow beneath each label in pie chart.
• startangle rotates the start of the pie chart by given degrees counterclockwise from the x-axis.
• explode is used to set the fraction of radius with which we offset each wedge.
• autopct is used to format the value of each label. Here, we have set it to show the percentage value only upto 1 decimal place.

Plotting curves of given equation

### Python

The output

of above program looks like this: Here, we use NumPy which is a general-purpose array-processing package in python.

• To set the x-axis values, we usethe np.arange() method in which the first two arguments are for range and the third one for step-wise increment. The result is a NumPy array.
• To get corresponding y-axis values, we simply use the predefined np.sin() method on the NumPy array.
• Finally, we plot the points by passing x and y arrays to the plt.plot() function.

So, in this part, we discussed various types of plots we can create in matplotlib. There are more plots that haven’t been covered but the most significant ones are discussed here –

This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

Sours: https://www.geeksforgeeks.org/graph-plotting-in-python-set-1/

### Overview

Teaching: 15 min
Exercises: 15 min

Questions
• How can I plot my data?

• How can I save my plot for publishing?

Objectives
• Create a time series plot showing a single data set.

• Create a scatter plot showing relationship between two data sets.

### is the most widely used scientific plotting library in Python.

• Commonly use a sub-library called .
• The Jupyter Notebook will render plots inline by default.
• Simple plots are then (fairly) simple to create. ### Display All Open Figures

In our Jupyter Notebook example, running the cell should generate the figure directly below the code. The figure is also included in the Notebook document for future viewing. However, other Python environments like an interactive Python session started from a terminal or a Python script executed via the command line require an additional command to display the figure.

Instruct to show a figure:

This command can also be used within a Notebook - for instance, to display multiple figures if several are created by a single cell.

### Plot data directly from a .

• We can also plot Pandas dataframes.
• This implicitly uses .
• Before plotting, we convert the column headings from a to data type, since they represent numerical values ### Select and transform data, then plot it.

• By default, plots with the rows as the X axis.
• We can transpose the data in order to plot multiple series. ### Many styles of plot are available.

• For example, do a bar plot using a fancier style. ### Data can also be plotted by calling the function directly.

• The command is
• The color and format of markers can also be specified as an additional optional argument e.g., is a blue line, is a green dashed line.

### Get Australia data from dataframe ### Can plot many sets of data together.

Often when plotting multiple datasets on the same figure it is desirable to have a legend describing the data.

This can be done in in two stages:

• Provide a label for each dataset in the figure:
• Instruct to create the legend.

By default matplotlib will attempt to place the legend in a suitable position. If you would rather specify a position this can be done with the argument, e.g to place the legend in the upper left corner of the plot, specify • Plot a scatter plot correlating the GDP of Australia and New Zealand
• Use either or  ### Minima and Maxima

Fill in the blanks below to plot the minimum GDP per capita over time for all the countries in Europe. Modify it again to plot the maximum GDP per capita over time for Europe.

### Solution ### Correlations

Modify the example in the notes to create a scatter plot showing the relationship between the minimum and maximum GDP per capita among the countries in Asia for each year in the data set. What relationship do you see (if any)?

### Solution No particular correlations can be seen between the minimum and maximum gdp values year on year. It seems the fortunes of asian countries do not rise and fall together.

You might note that the variability in the maximum is much higher than that of the minimum. Take a look at the maximum and the max indexes:

### Solution Seems the variability in this value is due to a sharp drop after 1972. Some geopolitics at play perhaps? Given the dominance of oil producing countries, maybe the Brent crude index would make an interesting comparison? Whilst Myanmar consistently has the lowest gdp, the highest gdb nation has varied more notably.

### More Correlations

This short program creates a plot showing the correlation between GDP and life expectancy for 2007, normalizing marker size by population:

Using online help and other resources, explain what each argument to does.

### Solution A good place to look is the documentation for the plot function - help(data_all.plot).

kind - As seen already this determines the kind of plot to be drawn.

x and y - A column name or index that determines what data will be placed on the x and y axes of the plot

s - Details for this can be found in the documentation of plt.scatter. A single number or one value for each data point. Determines the size of the plotted points.

### Saving your plot to a file

If you are satisfied with the plot you see you may want to save it to a file, perhaps to include it in a publication. There is a function in the matplotlib.pyplot module that accomplishes this: savefig. Calling this function, e.g. with

will save the current figure to the file . The file format will automatically be deduced from the file name extension (other formats are pdf, ps, eps and svg).

Note that functions in refer to a global figure variable and after a figure has been displayed to the screen (e.g. with ) matplotlib will make this variable refer to a new empty figure. Therefore, make sure you call before the plot is displayed to the screen, otherwise you may find a file with an empty plot.

When using dataframes, data is often generated and plotted to screen in one line, and seems not to be a possible approach. One possibility to save the figure to file is then to

• save a reference to the current figure in a local variable (with )
• call the class method from that variable.

Whenever you are generating plots to go into a paper or a presentation, there are a few things you can do to make sure that everyone can understand your plots.

• Always make sure your text is large enough to read. Use the parameter in , , , and , and with to increase the text size of the numbers on your axes.
• Similarly, you should make your graph elements easy to see. Use to increase the size of your scatterplot markers and to increase the sizes of your plot lines.
• Using color (and nothing else) to distinguish between different plot elements will make your plots unreadable to anyone who is colorblind, or who happens to have a black-and-white office printer. For lines, the parameter lets you use different types of lines. For scatterplots, lets you change the shape of your points. If you’re unsure about your colors, you can use Coblis or Color Oracle to simulate what your plots would look like to those with colorblindness.

### Key Points

• is the most widely used scientific plotting library in Python.

• Plot data directly from a Pandas dataframe.

• Select and transform data, then plot it.

• Many styles of plot are available: see the Python Graph Gallery for more options.

• Can plot many sets of data together.

Sours: https://swcarpentry.github.io/python-novice-gapminder/09-plotting/index.html

## Matplotlib Plotting

❮ PreviousNext ❯

### Plotting x and y points

The function is used to draw points (markers) in a diagram.

By default, the function draws a line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot function.

### Example

Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

### Result: Try it Yourself »

The x-axis is the horizontal axis.

The y-axis is the vertical axis.

### Plotting Without Line

To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.

### Example

Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, 'o')
plt.show()

### Result: Try it Yourself »

### Multiple Points

You can plot as many points as you like, just make sure you have the same number of points in both axis.

### Example

Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

### Result: Try it Yourself »

### Default X-Points

If we do not specify the points in the x-axis, they will get the default values 0, 1, 2, 3, (etc. depending on the length of the y-points.

So, if we take the same example as above, and leave out the x-points, the diagram will look like this:

### Example

Plotting without x-points:

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints)
plt.show()

### Result: Try it Yourself »

The x-points in the example above is [0, 1, 2, 3, 4, 5].

❮ PreviousNext ❯

Sours: https://www.w3schools.com/python/matplotlib_plotting.asp

549 550 551 552 553