I would like to make a log-log plot with pandas
import numpy as np
import pandas as pd
x = 10**arange(1, 10)
y = 10** arange(1,10)*2
df1 = pd.DataFrame( data=y, index=x )
df2 = pd.DataFrame(data = {'x': x, 'y': y})
df1.plot(logy=True, logx=True)
How can I make the x-axis logarithmic?
Introduction to Pandas DataFrame.plot () The following article provides an outline for Pandas DataFrame.plot (). On top of extensive data processing the need for data reporting is also among the major factors that drive the data world. For achieving data reporting process from pandas perspective the plot () method in pandas library is used.
From what it seems math.log10 cannot handle neither pandas dataframes nor ndarrays. So one option would be to go with numpy, which also includes a function to compute the base 10 logarithm, np.log10, and reconstruct the dataframe as pointed out in other solutions.
Natural logarithmic value of a column in pandas: To find the natural logarithmic values we can apply numpy.log () function to the columns. In this case, we will be finding the natural logarithm values of the column salary. The computed values are stored in the new column “natural_log”.
Let’s now see the steps to plot a line chart using Pandas. To start, prepare your data for the line chart. Here is an example of a dataset that captures the unemployment rate over time: Now create the DataFrame based on the above data: This is how the DataFrame would look like: Finally, plot the DataFrame by adding the following syntax:
When trying to create a log-log plot using the pandas
plot function you must select loglog=True
rather than logx=True, logy=True
within your keyword-arguments.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
x = 10 ** np.arange(1, 10)
y = 10 ** np.arange(1, 10) * 2
df1 = pd.DataFrame(data=y, index=x)
df1.plot(loglog=True, legend=False)
plt.show()
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