How do you plot a line and bar graph in python?

To show a bar and line graph on the same plot in matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.

  • Make a two-dimensional, size-mutable, potentially heterogeneous tabular data.

  • Create a figure and a set of subplots.

  • Plot the bar and line with the dataframe obtained from Step 2.

  • To display the figure, use show[] method.

Example

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame[dict[data=[2, 4, 1, 5, 9, 6, 0, 7]]]
fig, ax = plt.subplots[]

df['data'].plot[kind='bar', color='red']
df['data'].plot[kind='line', marker='*', color='black', ms=10]

plt.show[]

Output

Updated on 09-Aug-2021 06:40:59

  • Related Questions & Answers
  • How to plot multiple Pandas columns on the Y-axis of a line graph [Matplotlib]?
  • How to plot a bar graph in Matplotlib from a Pandas series?
  • How to plot a line graph from histogram data in Matplotlib?
  • Plot a Line Graph for Pandas Dataframe with Matplotlib?
  • How to plot two violin plot series on the same graph using Seaborn?
  • How to show tick labels on top of a matplotlib plot?
  • How to show [0,0] on matplotlib graph at the bottom left corner?
  • How to write text above the bars on a bar plot [Python Matplotlib]?
  • Show decimal places and scientific notation on the axis of a Matplotlib plot
  • How to plot a dashed line on a Seaborn lineplot in Matplotlib?
  • Matplotlib – How to show the count values on the top of a bar in a countplot?
  • How to add a legend on Seaborn facetgrid bar plot using Matplotlib?
  • How to plot multiple histograms on same plot with Seaborn using Matplotlib?
  • Python - How to plot a Pandas DataFrame in a Bar Graph
  • How to animate a line plot in Matplotlib?

You have to expand x axis with xlim:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

width = .35 # width of a bar

m1_t = pd.DataFrame[{
 'abnormal' : [90,40,30,30,30,25,25,20,15,10],
 'fix' : [60,70,65,70,70,60,50,45,45,45],
 'normal' : [140,160,170,180,190,200,210,220,230,240],
 'bad_rate' : [210,100,100,70,70,75,70,60,65,60]}]

m1_t[['abnormal','fix','normal']].plot[kind='bar', width = width]
m1_t['bad_rate'].plot[secondary_y=True]

ax = plt.gca[]
plt.xlim[[-width, len[m1_t['normal']]-width]]
ax.set_xticklabels[['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10']]

plt.show[]

For future question post your dataframe.

A hands-on tutorial to create a dual-axis combo chart with Matplotlib, Seaborn, and Pandas plot[]

Photo by Firmbee.com on Unsplash

Visualizing data is vital to analyzing data. If you can’t see your data — and see it in multiple ways you‘ll have a hard time analyzing that data.

Source from Python Data [1]

There are many different graphs to visualize data in python and thankfully with Matplotlib, Seaborn, Pandas, Plotly, etc., you can make some pretty powerful visualizations during data analysis. Among them, the combo chart is a combination of two chart types [such as bar and line] on the same graph. It’s one of Excel’s most popular built-in plots and is widely used to show different types of information.

It’s pretty easy to plot multiple charts on the same graph, but working with a dual-axis can be a bit confusing at first. In this article, we’ll explore how to create a dual-axis combo chart with Matplotlib, Seaborn, and Pandas plot[]. This article is structured as follows:

  1. Why use dual-axis: the problem using the same axis
  2. Matplotlib
  3. Seaborn
  4. Pandas plot[]

Please check out the Notebook for the source code. More tutorials are available from Github Repo.

For the demo, we’ll use the London climate data from Wikipedia:

x_label = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
average_temp = [5.2, 5.3,7.6,9.9,13.3,16.5,18.7, 18.5, 15.7, 12.0, 8.0, 5.5]
average_percipitation_mm = [55.2, 40.9, 41.6, 43.7, 49.4, 45.1, 44.5, 49.5, 49.1, 68.5, 59.0, 55.2]
london_climate = pd.DataFrame[
{
'average_temp': average_temp,
'average_percipitation_mm': average_percipitation_mm
},
index=x_label
]

1. Why use dual-axis: the problem using the same axis

It’s pretty easy to plot multiple charts on the same graph. For instance, with Matplotlib, we can simply call the chart function one after another.

plt.plot[x, y1, "-b", label="average temp"]
plt.bar[x, y2, width=0.5, alpha=0.5, color='orange', label="average percipitation mm", ]
plt.legend[loc="upper left"]
plt.show[]

Single-axis combo plot [Image by author]

This certainly creates a combo chart, but line and bar graphs use the same y-axis. It can be very difficult to view and interpret, especially when data are in different ranges.

We can solve this issue effectively using the dual-axis. But working with dual-axis can be a bit confusing at first. Let’s go ahead and explore how to create a dual-axis combo chart with Matplotlib, Seaborn, and Pandas plot[].

2. Matplotlib

The trick to make a dual-axis combo chart is to use two different axes that share the same x-axis. This is possible through the twinx[] method in Matplotlib. This generates two separate y-axes that share the same x-axis:

# Create figure and axis #1
fig, ax1 = plt.subplots[]
# plot line chart on axis #1
ax1.plot[x, average_temp]
ax1.set_ylabel['average temp']
ax1.set_ylim[0, 25]
ax1.legend[['average_temp'], loc="upper left"]
# set up the 2nd axis
ax2 = ax1.twinx[]
# plot bar chart on axis #2
ax2.bar[x, average_percipitation_mm, width=0.5, alpha=0.5, color='orange']
ax2.grid[False] # turn off grid #2
ax2.set_ylabel['average percipitation mm']
ax2.set_ylim[0, 90]
ax2.legend[['average_percipitation_mm'], loc="upper right"]
plt.show[]

The line chart is plotted on the ax1 as we normally do. The trick is to call ax2 = ax1.twinx[] to set up the 2nd axis and plot the bar chart on it. Note, ax2.grid[False] is called to hide the grid on the 2nd axis to avoid a grid display issue. Since the two charts are using the same x values, they share the same x-axis. By executing the code, you should get the following output:

A dual-axis combo chart using Matplotlib [Image by author]

3. Seaborn

Seaborn is a high-level Data Visualization library built on top of Matplotlib. Behind the scenes, it uses Matplotlib to draw its plots. For low-level configurations and setups, we can always call Matplotlib’s APIs, and that’s the trick to make a dual-axis combo chart in Seaborn:

# plot line graph on axis #1
ax1 = sns.lineplot[
x=london_climate.index,
y='average_temp',
data=london_climate_df,
sort=False,
color='blue'
]
ax1.set_ylabel['average temp']
ax1.set_ylim[0, 25]
ax1.legend[['average_temp'], loc="upper left"]
# set up the 2nd axis
ax2 = ax1.twinx[]
# plot bar graph on axis #2
sns.barplot[
x=london_climate.index,
y='average_percipitation_mm',
data=london_climate_df,
color='orange',
alpha=0.5,
ax = ax2 # Pre-existing axes for the plot
]
ax2.grid[b=False] # turn off grid #2
ax2.set_ylabel['average percipitation mm']
ax2.set_ylim[0, 90]
ax2.legend[['average_percipitation_mm'], loc="upper right"]
plt.show[]

sns.lineplot[] returns a Matplotlib axes object ax1. Then, similar to the Matplotlib solution, we also call ax2 = ax1.twinx[] to set up the 2nd axis. After that, the sns.barplot[] is called with the argument ax = ax2 to plot on the existing axes ax2. By executing the code, you should get the following output:

A dual-axis combo chart using Seaborn [Image by author]

You may notice that the legend has been displayed incorrectly with the bar chart in black color. One fix we can do is to create a custom legend using mpatches.Patch[]:

import matplotlib.patches as mpatches# plot line graph on axis #1
ax1 = sns.lineplot[
x=london_climate.index,
y='average_temp',
data=london_climate_df,
sort=False,
color='blue'
]
ax1.set_ylabel['average temp']
ax1.set_ylim[0, 25]
ax1_patch = mpatches.Patch[color='blue', label='average temp']
ax1.legend[handles=[ax1_patch], loc="upper left"]
# set up the 2nd axis
ax2 = ax1.twinx[]
# plot bar chart on axis #2
sns.barplot[
x=london_climate.index,
y='average_percipitation_mm',
data=london_climate_df,
color='orange',
alpha=0.5,
ax = ax2 # Pre-existing axes for the plot
]
ax2.grid[False] # turn off grid #2
ax2.set_ylabel['average percipitation mm']
ax2.set_ylim[0, 90]
ax2_patch = mpatches.Patch[color='orange', label='average percipitation mm']
ax2.legend[handles=[ax2_patch], loc="upper right"]
plt.show[]

mpatches.Patch[] creates a Matplotlib 2D artist object and we pass it to the axes’ legend. By executing the code, you shall get the following output:

A dual-axis combo chart using Seaborn [Image by author]

4. Pandas plot

Pandas use plot[] method to create charts. Under the hood, it calls Matplotlib’s API to create the chart by default [Note: the backend defaults to Matplotlib]. The trick is to set the argument secondary_y to True to allow the 2nd chart to be plotted on the secondary y-axis.

# Create the figure and axes object
fig, ax = plt.subplots[]
# Plot the first x and y axes:
london_climate.plot[
use_index=True,
kind='bar',
y='average_percipitation_mm',
ax=ax,
color='orange'
]
# Plot the second x and y axes.
# By secondary_y = True a second y-axis is requested
london_climate.plot[
use_index=True,
y='average_temp',
ax=ax,
secondary_y=True,
color='blue'
]
plt.show[]

Note, we also set use_index=True to use the index as ticks for the x-axis. By executing the code, you should get the following output:

A dual-axis combo chart using Pandas plot[] [Image by author]

Conclusion

In this article, we have learned how to create a dual-axis combo chart with Matplotlib, Seaborn, and Pandas plot[]. The combo chart is one of the most popular charts in Data Visualization, knowing how to create a dual-axis combo chart can be very helpful to analyzing data.

Thanks for reading. Please check out the Notebook for the source code and stay tuned if you are interested in the practical aspect of machine learning. More tutorials are available from the Github Repo.

References

  • [1] Python Data — Visualizing data- overlaying charts in python

How do you plot a bar and line graph in Python?

MatPlotLib with Python.
Set the figure size and adjust the padding between and around the subplots..
Make a two-dimensional, size-mutable, potentially heterogeneous tabular data..
Create a figure and a set of subplots..
Plot the bar and line with the dataframe obtained from Step 2..
To display the figure, use show[] method..

How do you plot a line in Python?

Simple Line Plots.
%matplotlib inline import matplotlib.pyplot as plt plt. style. use['seaborn-whitegrid'] import numpy as np. ... .
fig = plt. figure[] ax = plt. axes[] ... .
In [3]: fig = plt. figure[] ax = plt. ... .
In [4]: plt. plot[x, np. ... .
In [5]: plt. plot[x, np. ... .
plt. plot[x, x + 0, '-g'] # solid green plt. ... .
In [9]: plt. ... .
In [10]: plt..

How do you make a bar graph with two sets of data in Python?

arange[ ] function from numpy library is used to create a range of values. Plotting the multiple bars using plt. bar[ ] function in matplotlib library. To avoid overlapping of bars in each group, the bars are shifted 0.25 units from the X-axis in this example.

How do you plot a bargraph?

Collect your data. The first thing you have to do is to collect all of your data. ... .
Draw an x and a y-axis. This will look like a large "L" shape. ... .
Label the x-axis. ... .
Label the y-axis. ... .
Draw your bars. ... .
Interpret the data..

Chủ Đề