Issue
I'm trying to follow these examples,
Using loops to create multiple matplotlib graphs with dual y-axes
How do you plot two different y-axes using a loop with twinx?
How to use "ax.twinx()" with a loop?
however they don't work for different reasons , for example, df2
or region
is not defined, or no answers/comments. My goal is to read a csv file, and on the fly add columns to a single plot
(They all share the same x-axis, but have different y-axes scales/ranges.)
But when I try to programmatically generate the axes, it has an error because I haven't declared an array of axes to hold the return of twinx():
import matplotlib.pyplot as plt
#Single canvas and axes in which to put all the data
fig, ax = plt.subplots(1, 1)
#Generate additional Y-axes and save into array for easy access
for i in range (5):
axIdx [ i ] = ax.twinx() #ERROR
#Loop thru each axis and add csv data, positioning the legend and spines appropriately
time = [0,1,2,3]
y1 = [10,11,12,13]
axIdx[0].plot(time,y1,color="green", label='y1')
axIdx[0].legend(loc='upper right', bbox_to_anchor=(1.2, 1), ncol=2, borderaxespad=0)
axIdx[0].set_ylabel("y1",color="black")
axIdx[0].tick_params(axis='y',colors="black")
axIdx[0].spines['right'].set_color("black")
y2 = [7.3,1.2,3.14,88.8]
axIdx[1].plot(time,y2,color="red", label='y2')
axIdx[1].legend(loc='upper right', bbox_to_anchor=(1.4, 1), ncol=2, borderaxespad=0)
axIdx[1].set_ylabel("y1",color="black")
axIdx[1].tick_params(axis='y',colors="black")
axIdx[1].spines['right'].set_color("black")
plt.tight_layout()
plt.show()
How is this normally done? I tried using a dictionary to import the csv data, but I still need a way to programmatically loop thru the axes.
Solution
You have problem with basic knowledge about Python.
You didn't create list.
To use axIdx[i]
first you have to create list axIdx = [...]
(with correct number of items).
axIdx = [None, None, None, None, None] # list with 5 elements
# Generate additional Y-axes and save into array for easy access
for i in range(5):
axIdx[i] = ax.twinx()
But it is more popular to create empty list and use append()
to add items.
axIdx = [] # empty list
# Generate additional Y-axes and save into array for easy access
for i in range(5):
axIdx.append( ax.twinx() )
Full working code:
import matplotlib.pyplot as plt
# Single canvas and axes in which to put all the data
fig, ax = plt.subplots(1, 1)
#axIdx = [None, None, None, None, None]
# Generate additional Y-axes and save into array for easy access
#for i in range (5):
# axIdx[i] = ax.twinx()
axIdx = []
#Generate additional Y-axes and save into array for easy access
for i in range (5):
axIdx.append(ax.twinx())
# Loop thru each axis and add csv data, positioning the legend and spines appropriately
time = [0,1,2,3]
y1 = [10,11,12,13]
axIdx[0].plot(time,y1,color="green", label='y1')
axIdx[0].legend(loc='upper right', bbox_to_anchor=(1.2, 1), ncol=2, borderaxespad=0)
axIdx[0].set_ylabel("y1",color="black")
axIdx[0].tick_params(axis='y',colors="black")
axIdx[0].spines['right'].set_color("black")
y2 = [7.3,1.2,3.14,88.8]
axIdx[1].plot(time,y2,color="red", label='y2')
axIdx[1].legend(loc='upper right', bbox_to_anchor=(1.4, 1), ncol=2, borderaxespad=0)
axIdx[1].set_ylabel("y1",color="black")
axIdx[1].tick_params(axis='y',colors="black")
axIdx[1].spines['right'].set_color("black")
plt.tight_layout()
plt.show()
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.