I have a script which is creating one or two charts, depending on if one specific condition is met or not. Really basically, what I am doing so far is the following:
import matplotlib.pyplot as plt
list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart
ax = plt.subplot(211) #create the first subplot that will ALWAYS be there
ax.plot(list1) #populate the "main" subplot
if somecondition == True:
ax = plt.subplot(212) #create the second subplot, that MIGHT be there
ax.plot(list2) #populate the second subplot
plt.show()
This code (with the proper data, but this simple version that I did is executable anyway) generates two subplots of the same size, one above the other. However, what I would like to get is the following:
I'm pretty sure it's just a matter of sizing the two subplots, probably even by the parameter 211 and 212 (that I don't understand what they stand for, since I'm new to Python and couldn't find a clear explanation on the web yet). Does anyone know how to regulate the size of the subplots in a easy way, proportionally to the number of subplots as well as to the entire size of the figure? To make it easier to understand, could you also please edit my simple code I attached to get the result I'm looking for? Thanks in advance!
Python is commonly used for developing websites and software, task automation, data analysis, and data visualization. Since it's relatively easy to learn, Python has been adopted by many non-programmers such as accountants and scientists, for a variety of everyday tasks, like organizing finances.
The Python += operator lets you add two values together and assign the resultant value to a variable. This operator is often referred to as the addition assignment operator.
Python is written in C (actually the default implementation is called CPython).
Python is widely considered among the easiest programming languages for beginners to learn. If you're interested in learning a programming language, Python is a good place to start. It's also one of the most widely used.
does this solution satisfy?
import matplotlib.pyplot as plt
list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart
if not somecondition:
ax = plt.subplot(111) #create the first subplot that will ALWAYS be there
ax.plot(list1) #populate the "main" subplot
else:
ax = plt.subplot(211)
ax.plot(list1)
ax = plt.subplot(223) #create the second subplot, that MIGHT be there
ax.plot(list2) #populate the second subplot
plt.show()
If you need the same width but with half height, better to use matplotlib.gridspec
, reference here
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
list1 = [1,2,3,4]
list2 = [4,3,2,1]
somecondition = True
plt.figure(1) #create one of the figures that must appear with the chart
gs = gridspec.GridSpec(3,1)
if not somecondition:
ax = plt.subplot(gs[:,:]) #create the first subplot that will ALWAYS be there
ax.plot(list1) #populate the "main" subplot
else:
ax = plt.subplot(gs[:2, :])
ax.plot(list1)
ax = plt.subplot(gs[2, :]) #create the second subplot, that MIGHT be there
ax.plot(list2) #populate the second subplot
plt.show()
It seems you are looking for this:
if somecondition:
ax = plt.subplot(3,1,(1,2))
ax.plot(list1)
ax = plt.subplot(3,1,3)
ax.plot(list2)
else:
plt.plot(list1)
The magic numbers are nrows, ncols, plot_number, see the documentation. So 3,1,3
will create 3 rows, 1 column, and will plot into the third cell. An abbreviation for that is 313
.
It's possible to use tuple as plot_number, so you can create a plot which lives in the first and second cell: 3,1,(1,2)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With