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.

6
  • 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 and provide an MCVE. Commented Jan 8, 2021 at 14:02
  • Okay I will keep this in mind Commented Jan 8, 2021 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. Commented Jan 8, 2021 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? Commented Jan 8, 2021 at 14:19
  • can you show current code? Commented Jan 8, 2021 at 14:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I wanted. Just a little problem. The dates are all messed up. Is there any way to set xticks as they are set in my question?
Thank you, you are great
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

2 Comments

This is not what I want. It is just marking points of line. I want to mark points of one list on other line.
Then plot another curve with dots only, use numpy to get it: x = date[InnovationScore > 0], y = open[InnovationScore > 0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.