Plot Number Like Categorical In Matplotlib
I have a csv that looks like this: 2 1111 4 926 8 914 16 933 32 911 64 912 128 1010 256 1010 512 1013 1024 1070 1025 921 1026 921 1027 920 1028 918 1029
Solution 1:
Matplotlib can as of the current version not handle the categorical pandas datatype.
Options you have:
use strings (as pointed out in the question) This solution will work in matplotlib 2.2 or higher.
import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({"x" : np.logspace(0,11,12, base=2).astype(int), "y" : np.random.randint(900,1200,12)}) plt.plot(df.x.astype(str),df.y) plt.show()
plot the data index and set the ticklabels according to the values.
import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({"x" : np.logspace(0,11,12, base=2).astype(int), "y" : np.random.randint(900,1200,12)}) plt.plot(df.index,df.y) plt.xticks(df.index, df.x) plt.show()
In both cases the plot will look like
Post a Comment for "Plot Number Like Categorical In Matplotlib"