0

I want to add secondary y-axis. I have my data in CSV with three column date, lscc and cc. I want to add LSCC as first y-axis and CC as secondry. so far I have done this

df=pd.read_csv("E29Lsccvalue.csv", index_col='Date' )
plt.ylabel("")
plt.xlabel("Low level Similarity Class Cohesion (LSCC) Evolution")
df.plot(kind="line", marker='o',legend=None)
plt.xticks(rotation=90)
plt.show()

thanks

mishi
  • 31
  • 1
  • 9

2 Answers2

1

Although no reproducible date is provided, I guess you can achieve the desired result by doing this:

ax = df.plot(secondary_y='CC')

eventually adding all your ax customization required

edit: dotted line customization

Suppose you need a dotted vertical line at a certain position on your x-axis (in this example, at position 2 from your pandas index), use axvline and ':' as linestyle (dots)

ax = a.plot(secondary_y='Price')
ax.axvline(a.index.values[2], linestyle=':')
Jacopo Repossi
  • 375
  • 2
  • 10
  • hey thanks alot. one more question how can i add a dotted line on a specific date? – mishi Feb 03 '19 at 23:08
  • I edited my answer – Jacopo Repossi Feb 03 '19 at 23:15
  • hey thanks I have tried the solution for dotted line but the thing is my x-axis is date and axvline isnt accepting the date value – mishi Feb 04 '19 at 00:39
  • axvline accepts a position, not a value! – Jacopo Repossi Feb 04 '19 at 00:41
  • but its nt accepting the position im specifying. Im doing it like this import matplotlib.pyplot as plt df=pd.read_csv("E29Lsccvalue.csv") axes1 =df.plot(kind="line", marker="o", legend=None,secondary_y='CC',figsize=(6,6),rot=90) plt.xlabel("Date") plt.ylabel("Low level Similarity Class Cohesion (LSCC)") axes1.set_ylabel("Class Cohesion(CC)") line=df.iloc[20][0] print(line) axes1.axvline(line, linestyle=':', color='r') plt.show() – mishi Feb 04 '19 at 10:12
  • It's quite hard to understand what you did, but it seems like your `line` is again a value and not the index position on the x-axis. `iloc` is returning the 20th element and you are accessing the first value with [0]...try `df.index.values[20]` instead – Jacopo Repossi Feb 04 '19 at 11:46
  • I tried that but it dnt show a dotted line I tried to print this index value and im getting the date but its not visible on graph – mishi Feb 04 '19 at 11:59
  • why don't you open a new post with some code example so that it's easier to address that specific problem? – Jacopo Repossi Feb 04 '19 at 13:22
1

Within matplotlib I have used twinx() when I want to utilize the existing X-axis I have created, yet plot more data on top with a different Y axis. In your case with df as the first plot object:

axCC = df.twinx() # second axis sharing the same X axis of the original

Then you can include plots, labels, and other parameters referenced to this axis through calls such as:

axCC.set_ylabel("ExampleLabel",color="tab:red")
axCC.plot(xData,yData,color="blue")

Etc, etc.

A fully functional example with more detail is shown here

whisperquiet
  • 27
  • 2
  • 8