Plot two variables in python matplotlib
The Show
Output: When drawing a time-series plot, a quantitative variable is required. The plotting of time series objects is most likely one of the steps of the analysis of time-series data. If the value of a variable is measured at different points in time, the data are referred to as time-series data. If you have different variables, how would you plot time series with that data?Plotting different variables could be challenging, especially displaying the values. In order to do so, you need multiple y-axes, one for each variable, as shown in the image below: Click to enlargeExamples:
Note: This is different from comparing the same variable across two different datasets. There are two ways to approach the problem of two variables on a graph:
Let us look at the first one in this step: Add two lines on a single plot and a second y-axis with another scaleUsing the graph shown below, let’s plot temperature and humidity on the same axes. Since the graph is quite dense (it has data for every hour), we’ll just plot data for the first two days of January. You can see how the list was filtered in the accompanying Jupyter Notebook for this activity. We want to achieve this graph by following the below steps: Click to enlargeFollow the steps below: Step 1Create the plot in the usual way. Code:
Step 2Have two axes sharing the same plot space. Put the name of the first axes as Code:
Step 3Now we need to add another axes that shares its x-axis with our main axes. We do this by calling the twinx method on the main axes. (This will return a new axes that has been configured to have its y-axis ticks and label drawn on the right.) Code:
Set up the labels and tick marks now. Any label that’s shared can be applied to the main axes, in the same method that you’ve already seen: Code:
Output:
Step 5Next, the only label that needs to be set specifically on the humidity axes is its y-axis label. Code:
Output: Step 6Now, set the tick marks position on the x-axis using the ConciseDateFormatter. Code:
Step 7Next, draw the lines with the Code:
Step 8Now let’s plot the temperature line on the Step 9The final step is to draw the legend. We’d have a slight problem if we tried to do it automatically. If we called However, this can be done in two ways: A. The first way is a list of lines to add to the legend. Example: ax.legend([line1, line2, line3], [“Label 1”, “Label 2”, “Label 3”]) Let’s look at an example from the same graph shown above. Since Python allows up to combine lists by adding them together, we can simplify this statement by using the below code: Code:
B. The second way is a list of strings, of the same length, of labels to give these lines. Code:
Output: Click to enlargeHow do you plot two variables in Python?100 XP. Use plt. subplots to create a Figure and Axes objects called fig and ax , respectively.. Plot the carbon dioxide variable in blue using the Axes plot method.. Use the Axes twinx method to create a twin Axes that shares the x-axis.. Plot the relative temperature variable in red on the twin Axes using its plot method.. How do I plot multiple variables in Matplotlib?In Matplotlib, we can draw multiple graphs in a single plot in two ways.. nrows, ncols: These gives the number of rows and columns respectively. ... . sharex, sharey: These parameters specify about the properties that are shared among a and y axis.. How do I plot two graphs in Python Matplotlib?Create x, y1 and y2 data points using numpy.. Add a subplot to the current figure at index 1.. Plot curve 1 using x and y1.. Add a subplot to the current figure at index 2.. Plot curve 2 using x and y2.. To display the figure, use show() method.. |