0

I have this dataframe and I want to line plot it. As I have plotted it.

enter image description here

Graph is

enter image description here

Code to generate is

fig, ax = plt.subplots(figsize=(15, 5))
date_time = pd.to_datetime(df.Date)
df = df.set_index(date_time)
plt.xticks(rotation=90)
pd.DataFrame(df,  columns=df.columns).plot.line( ax=ax, 
xticks=pd.to_datetime(frame.Date))

I want a marker of innovationScore with value(where innovationScore is not 0) on open, close line. I want to show that that is the change when InnovationScore changes.

shahid hamdam
  • 751
  • 1
  • 10
  • 24
  • Please do not post data/code/error messages as images. Post the text directly here on SO instead. Please read also [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide an MCVE. – Mr. T Jan 08 '21 at 14:02
  • Okay I will keep this in mind – shahid hamdam Jan 08 '21 at 14:03
  • 1
    I suggest you edit your question to attract more specific answers. I don't understand how your examples relate to each other and what the expected outcome is. Most likely, you problem occurs because of datetime format mismatch between pandas and matplotlib. But we don't know. – Mr. T Jan 08 '21 at 14:05
  • What code did generate the output? Something is not right here because the x-axis is index-based, not date-based as expected. I also fail to see the Open line - is it identical or doesn't it show? – Mr. T Jan 08 '21 at 14:19
  • can you show current code? – adir abargil Jan 08 '21 at 14:21
  • @adirabargil check now – shahid hamdam Jan 08 '21 at 14:39

2 Answers2

2

You have to address two problems - marking the corresponding spots on your curves and using the dates on the x-axis. The first problem can be solved by identifying the dates, where the score is not zero, then plotting markers on top of the curve at these dates. The second problem is more of a structural nature - pandas often interferes with matplotlib when it comes to date time objects. Using pandas standard plotting functions is good because it addresses common problems. But mixing pandas with matplotlib plotting (and to set the markers, you have to revert to matplotlib afaik) is usually a bad idea because they do not necessarily present the date time in the same format.

import pandas as pd
from matplotlib import pyplot as plt

#fake data generation, the following code block is just for illustration
import numpy as np
np.random.seed(1234)
n = 50
date_range = pd.date_range("20180101", periods=n, freq="D")
choice = np.zeros(10)
choice[0] = 3
df = pd.DataFrame({"Date": date_range, 
                   "Open": np.random.randint(100, 150, n), 
                   "Close": np.random.randint(100, 150, n), 
                   "Innovation Score": np.random.choice(choice, n)})


fig, ax = plt.subplots()

#plot the three curves
l = ax.plot(df["Date"], df[["Open", "Close", "Innovation Score"]])
ax.legend(iter(l), ["Open", "Close", "Innovation Score"])

#filter dataset for score not zero
IS = df[df["Innovation Score"] > 0]
#plot markers on these positions
ax.plot(IS["Date"], IS[["Open", "Close"]], "ro")
#and/or set vertical lines to indicate the position
ax.vlines(IS["Date"], 0, max(df[["Open", "Close"]].max()), ls="--")

#label x-axis score not zero
ax.set_xticks(IS["Date"])
#beautify the output
ax.set_xlabel("Month")
ax.set_ylabel("Artifical score people take seriously")
fig.autofmt_xdate() 
plt.show()

Sample output:

![enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
1

You can achieve it like this:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], "ro-") # r is red, o is for larger marker, - is for line
plt.plot([3, 2, 1], "b.-") # b is blue, . is for small marker, - is for line

plt.show()

Check out also example here for another approach:

https://matplotlib.org/3.3.3/gallery/lines_bars_and_markers/markevery_prop_cycle.html

I very often get inspiration from this list of examples:

https://matplotlib.org/3.3.3/gallery/index.html

Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28