# You may want to adjust the `limit` here. In : x.fillna(method='ffill', limit=2)Īs an example of using this for something similar to your case: import pandas as pd One option is to use pandas fillna with a limited amount of fill values.Īs a quick example of how this works: In : import pandas as pd import numpy as np If I'm understanding you correctly, you have a dataset with lots of small gaps (single NaNs) that you want filled and larger gaps that you don't. Plt.plot_date(date_log, Log_1b, ‘-r*’, markersize=2, markeredgewidth=0, color=’r’) # Log_1a contains raw data with NaN Method-2: plt.plot_date(date_log, Log_1a, ‘-r*’, markersize=2, markeredgewidth=0, color=’r’) # Log_1a contains raw data with NaN Plt.plot_date(date_log, Log_1b, linestyle='-', marker='', color='b') Log_1b_mask = np.isfinite(Log_1b) # Log_1b is column 3 data points Method-1: Log_1a_mask = np.isfinite(Log_1a) # Log_1a is column 2 data points leaving missing data points for longer times as a gap. I like the first chart but I want to create gaps like the second method when there are no sample data points for 24h date range etc. The second example does not join the points. When there is a big gap like the blue points (01/06-) I want to have a gap then the points getting joined. In the example below, I’m just plotting the first three columns. If I use the second code I can see the gaps but due to NaN points the data points are not getting joined. If I plot up using the first code it works nicely, but I want to have gaps where there no logger data for a day or so and do not want the points to be joined. The trouble is sample points are logged using different time even on hourly basis, so every column has at least a couple of NaN. First column is a date ( date_log), and the rest of columns contain different sample points. We can see that both lifeExp and gdpPerCap have increased over the years.I have hourly data consisting of a number of columns. This definitely help us understand the relationship of the two variables against another. A plot with with different y-axis made with twinx in matplotlib. Then we can display the plot with plt.show() as before. # make a plot with different y-axis using second axis objectĪx2.plot(gapminder_us.year, gapminder_us,color="blue",marker="o")Īx2.set_ylabel("gdpPercap",color="blue",fontsize=14)įig.savefig('two_different_y_axis_for_single_python_plot_with_twinx.jpg', # twin object for two different y-axis on the sample plot Now we use the second axis object “ax2” to make plot of the second y-axis variable and update their labels. Next we use twinx() function to create the second axis object “ax2”. And we also set the x and y-axis labels by updating the axis object. In this example, we plot year vs lifeExp. We first create figure and axis objects and make a first plot. The way to make a plot with two different y-axis is to use two different axes objects with the help of twinx() function. One of the solutions is to make the plot with two different y-axes. We don’t see any variation in it because of the scale of gdpPercap values. The line for lifeExp over years is flat and really low. We can immediately see that this is a bad idea. # create figure and axis objects with subplots()Īx.plot(gapminder_us.year, gapminder_us.lifeExp, marker="o")Īx.plot(gapminder_us.year, gapminder_us, marker="o") Naively, let us plot both on the same plot with a single y-axis. lifeExp values are below 100 and gdpPercap values are in thousands. The variable on x-axis is year and on y-axis we are interested in lifeExp & gdpPercap.īoth lifeExp and gdpPercap have different ranges. We are interested in making a plot of how lifeExp & gdpPercap changes over the years. Let us subset gapminder data by using Pandas query() function to filter for rows with United States. #load gapminder data from url as pandas dataframe We will use gapminder data from Carpentries to make the plot with two different y-axis on the same plot.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |