Python Forum
Matplotlib graphing help (dual y axis, groupby, filter)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matplotlib graphing help (dual y axis, groupby, filter)
#1
Hi everyone,

I'm new to python and I'm trying to plot some graphs.

I'm trying to plot multiple series of numerical data that are defined by an ID column. For reference it is environmental data at different site locations, so one column is the value and another column is the site ID. I've posted some code below using simple dummy data, I've just adapted it from a tutorial.

I want to use .groupby('A1') and have 2 different data series plotted, but I can't figure out where in the code to use the .groupby?

I would also like to be able to filter by column A1, to be able to show any combination of X,Y,Z values. In my real data I have many series and I want to be able to plot different combinations of them.

I also want to be able to filter by the index, in my real data the index is the date, so I'd like to be able to filter by date and by column A1.

For instance something like dfA[1:2] and dfA.A1 == "X" (or ideally an array of what I want to filter) and dfA.groupby['A1'] all working together.

I'm just having issues to get all this working at the same time. Any help would be appreciated!

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.style as style
import matplotlib.dates as mdates
from datetime import datetime

A = {'Aindex': [1, 2, 3,1,2,3], 'A1': ['X', 'X', 'X','Y','Y','Y'], 'A2': [20,30,40,40,30,20]}
B = {'Bindex': [1, 2, 3], 'B1': [3, 3, 3]}

dfA = pd.DataFrame(data=A)
dfB = pd.DataFrame(data=B)

dfA.set_index('Aindex', inplace=True)
dfB.set_index('Bindex', inplace=True)

datay1 = dfA.A2
datay2 = dfB.B1
datax1 = dfA.index
datax2 = dfB.index

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('x')
ax1.set_ylabel('y1', color=color)
ax1.plot(datax1, datay1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('y2', color=color)  # we already handled the x-label with ax1
ax2.plot(datax2, datay2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 232 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  Create dual folder on different path/drive based on the date agmoraojr 2 378 Jan-21-2024, 10:02 AM
Last Post: snippsat
  matplotlib x-axis text move bottom upward jacklee26 3 930 May-31-2023, 04:28 AM
Last Post: jacklee26
  x-axis labels with Matplotlib Mark17 8 2,125 Mar-23-2022, 06:10 PM
Last Post: Mark17
  How to make x-axis readable with matplotlib Mark17 7 3,820 Mar-01-2022, 04:30 PM
Last Post: DPaul
  matplotlib x axis range goes over the set range Pedroski55 5 3,112 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,233 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  graphing inequalities Dasiey12 0 3,425 Mar-30-2021, 01:47 AM
Last Post: Dasiey12
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,940 Mar-21-2021, 05:17 PM
Last Post: yome
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,759 Mar-17-2021, 01:19 PM
Last Post: silviover_junior

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020