0

I have a time-series dataset. I want to plot the data i.e. current, voltage, and power each on the y-axis. I tried to do it with the following code but facing some errors (Image link attached).

Need help with the following:

  • adjusting scale of each y-axis
  • setting color of each y-axis
  • setting legends

Any other suggestions for improvement of code or alternative solutions are most welcome.

fig, ax1 = plt.subplots(figsize=(12,10))
    
ax2 = ax1.twinx()
ax3 = ax1.twinx()
    
ax1.set_ylim(0, 10)
ax2.set_ylim(0, 100)
ax3.set_ylim(0, 1000)
    
ax1.set_ylabel("Output Current")
ax2.set_ylabel("Output Voltage")
ax3.set_ylabel("Output Power")

color1 = plt.cm.viridis(0)
color2 = plt.cm.viridis(0.5)
color3 = plt.cm.viridis(.9)

ax1 = output_params.Output_Voltage.plot(color=color1, label= 'Output Current')
ax2 = output_params.Output_Current.plot(color=color2, label='Output Voltage')
ax3 = output_params.Output_Power.plot(color=color3, label='Output Power')

lns = [ax1, ax2, ax3]
ax1.legend(handles=lns, loc='best')

# right, left, top, bottom
ax3.spines['right'].set_position(('outward', 60))

ax1.yaxis.label.set_color(ax1.get_color())
ax2.yaxis.label.set_color(ax2.get_color())
ax3.yaxis.label.set_color(ax3.get_color())

# Adjust spacings w.r.t. figsize
fig.tight_layout()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Nick
  • 1
  • 4
  • 1
    your code is incomplete (i.e. it is not a [MCVE]). What is `output_params`? A `pandas` data frame, perhaps? Why are you reassigning `ax1`, `ax2` and `ax3` when you plot? Also, what output do you expect `ax1.get_color()` to give? Axes objects do not have a`get_color` method. Perhaps you want the color of the line? Perhaps try `ax1.lines[0].get_color()` instead? – tmdavison Aug 16 '21 at 14:10
  • Does this answer your question? [multiple axis in matplotlib with different scales](https://stackoverflow.com/questions/9103166/multiple-axis-in-matplotlib-with-different-scales) – Trenton McKinney Aug 16 '21 at 15:51
  • @tmdavison Actually my whole code is quite big and has a lot of dependent variables. It wasn't possible to post full code. To answer your questions: Yeah ```output_params``` is a pandas time-series data frame. Also, with ```ax1.get_color()``` I am trying to color the respective y-axes. I was actually trying to adjust the code in 2nd answer of https://stackoverflow.com/questions/9103166/multiple-axis-in-matplotlib-with-different-scales question according to my needs but wasn't clearly successful. – Nick Aug 17 '21 at 08:33
  • @TrentonMcKinney I tried to adopt the code from this question but wasn't really successful. This question is not solving for time series data. – Nick Aug 17 '21 at 08:35
  • So in that Q&A, they use `p1.get_color()`, where `p1` is a `Line2D` object, not an `Axes` instance. When you use `DataFrame.plot()` from `pandas`, it doesn't return the `Line2D`, but instead it returns the `Axes` instance. Which is why I suggested using `ax1.lines[0].get_color()`, because that *should* give you the color of the line you plotted on `ax1`. – tmdavison Aug 17 '21 at 09:24

0 Answers0