Python Forum
How to conditionally specify markers in matplotlib graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to conditionally specify markers in matplotlib graph
#1
Here's some code:

import datetime as dt
import pandas as pd
from matplotlib.ticker import LinearLocator

df = pd.DataFrame({'date':[dt.datetime(2018,8,25), dt.datetime(2018,8,26), dt.datetime(2018,8,27), dt.datetime(2018,8,28), 
                           dt.datetime(2018,8,29), dt.datetime(2018,8,30), dt.datetime(2018,8,31), dt.datetime(2018,9,1), 
                           dt.datetime(2018,9,2), dt.datetime(2018,9,3), dt.datetime(2018,9,4)], 'n':[10, 6, 7, 2, -3, -5,
                            1, 3, 7, 10, 16]})
markerlist = []
trade_date = [dt.datetime(2018,8,25), dt.datetime(2018,8,30), dt.datetime(2018,9,3)]
for i in df['date']:
    if i in trade_date:
        markerlist.append('d')
    else:
        markerlist.append('.')
fig, ax = plt.subplots()
ax.plot(df['date'], df['n'], marker=markerlist, linestyle='dashed', markersize=12)
plt.xticks(rotation = 45)
ax.get_xaxis().set_major_locator(LinearLocator(numticks=7)) #this works nicely to evenly space x-tick labels
plt.show()
This raises ValueError: Unrecognized marker style ['d', '.', '.', '.', '.', 'd', '.', '.', '.', 'd', '.']. What I really want is to iterate (?) through each point and plot accordingly.

Actually, I've been told before I want to print all points at once rather than iterate through... but the program needs to grab the corresponding marker from markerlist, which is why I say "iterate."

Also, I want this to be a line graph (solid is fine... I used dotted only because I stumbled upon that keyword string. In fact, maybe I don't even need to include that argument?). I tried this another way where I did iterate through the points (using a for i and .loc[] commands) and I lost the line connecting the dots (which I couldn't explain).

Any thoughts?
Reply
#2
I just wanted to bump this... does anyone have any thoughts? Thanks!
Reply
#3
(Mar-03-2022, 01:06 AM)Mark17 Wrote: I just wanted to bump this... does anyone have any thoughts? Thanks!

This is closer:

import datetime as dt
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator

df = pd.DataFrame({'date':[dt.datetime(2018,8,25), dt.datetime(2018,8,26), dt.datetime(2018,8,27), dt.datetime(2018,8,28), 
                           dt.datetime(2018,8,29), dt.datetime(2018,8,30), dt.datetime(2018,8,31), dt.datetime(2018,9,1), 
                           dt.datetime(2018,9,2), dt.datetime(2018,9,3), dt.datetime(2018,9,4)], 'n':[10, 6, 7, 2, -3, -5,
                            1, 3, 7, 10, 16]})
markerlist = []
trade_date = [dt.datetime(2018,8,25), dt.datetime(2018,8,30), dt.datetime(2018,9,3)]
fig, ax = plt.subplots()
for i in df['date']:
    if i in trade_date:
        markerlist.append('d')
    else:
        markerlist.append('.')

for xp, yp, m in zip(df['date'], df['n'], markerlist):
   ax.plot(xp, yp, m, color='green', linestyle='solid', markersize=12)

#ax.plot(df['date'], df['n'], marker=markerlist, linestyle='solid', markersize=12)
plt.xticks(rotation = 45)
ax.get_xaxis().set_major_locator(LinearLocator(numticks=7)) #this works nicely to evenly space x-tick labels
plt.show()
Now I get the differential markers... and the color argument fixes everything as green (still not sure why it was multi-colored without this). I just need a line connecting the points, now, and everything would be good.
Reply
#4
Solved! Bottom line: need to do one ax.plot() for the line and a separate ax.plot() for the markers (points).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Embed Matplotlib Graph to Tkinter? dimidgen 0 241 Mar-04-2024, 07:40 PM
Last Post: dimidgen
  How to conditionally execute correctly? lolita7777 5 1,192 Nov-16-2022, 07:50 AM
Last Post: lolita7777
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,959 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,256 Mar-11-2021, 10:52 AM
Last Post: buran
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,590 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  using paramater markers masmithrpg 0 1,620 Dec-31-2019, 01:57 PM
Last Post: masmithrpg
  Conditionally lazy load modules amb85 1 2,106 Dec-10-2019, 07:24 AM
Last Post: Gribouillis
  Change the scale on a Matplotlib Interactive Graph khatharsis 0 2,858 Oct-13-2019, 06:14 AM
Last Post: khatharsis
  How to Find & Count String Patterns Between two Markers in a HTML file ahmedwaqas92 3 2,979 Aug-19-2019, 10:12 AM
Last Post: ahmedwaqas92
  Plot a graph using matplotlib.pyplot Tibas 1 3,476 Feb-23-2018, 10:47 AM
Last Post: Tibas

Forum Jump:

User Panel Messages

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