pandas: plot multiple columns to same x value

  • Last Update :
  • Techknowledgy :

I suggest you try using matplotlib to handle the plotting, and manually cycle the colors. You can use something like:

import matplotlib.pyplot as plt
import pandas as pd
import itertools
#data
df = pd.DataFrame({
   'id': [1, 2, 3, 3],
   'labels': ['HPRR1234', 'HPRR4321', 'HPRR2345', 'HPRR2345'],
   'g': ['KRAS', 'KRAS', 'ELK4', 'ELK4'],
   'r1': [15, 9, 15, 1],
   'r2': [14, 8, 7, 0],
   'r3': [14, 16, 9, 12]
})
#extra setup
plt.rcParams['xtick.major.pad'] = 8
#plotting style(s)
marker = itertools.cycle((',', '+', '.', 'o', '*'))
color = itertools.cycle(('b', 'g', 'r', 'c', 'm', 'y', 'k'))
#plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(df['id'], df['r1'], ls = '', ms = 10, mew = 2,
   marker = marker.next(), color = color.next())
ax.plot(df['id'], df['r2'], ls = '', ms = 10, mew = 2,
   marker = marker.next(), color = color.next())
ax.plot(df['id'], df['r3'], ls = '', ms = 10, mew = 2,
   marker = marker.next(), color = color.next())
# set the tick labels
ax.xaxis.set_ticks(df['id'])
ax.xaxis.set_ticklabels(df['labels'])
plt.setp(ax.get_xticklabels(), rotation = 'vertical', fontsize = 12)
plt.tight_layout()
fig.savefig("example.pdf")

I managed to find a way to keep the string names! I thought about what you said about finding numbers for the IDs and figured I could use the index, which worked just fine.

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(df.index, df['r1'], ls = '', marker = marker.next(), color = next(color))
ax.plot(df.index, df['r2'], ls = '', marker = marker.next(), color = next(color))
ax.plot(df.index, df['r3'], ls = '', marker = marker.next(), color = next(color))

ax.xaxis.set_ticks(df.index)
ax.xaxis.set_ticklabels(df['g'])

Suggestion : 2

Pandas Plot Multiple Columns To Same X Value, 1 week ago Pandas : Pandas: plot multiple columns to same x value [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] Pandas : Pandas: plot multiple ... ,All rows with the same ID should be plotted to the same x value / ID, but with another colour,All rows with the same ID should be plotted to the same x value / ID, but with another colour


PrEST ID Gene Sequence Ratio1 Ratio2 Ratio3 HPRR12 ATF1 TTPSAXXXXXXXXXTTTK 6.3222 4.0558 4.958 HPRR23 CREB1 KIXXXXXXXXPGVPR NaN NaN NaN HPRR23 CREB1 ILNXXXXXXXXGVPR 0.22691 2.077 NaN HPRR15 ELK4 IEGDCEXXXXXXXGGK 1.177 NaN 12.073 HPRR15 ELK4 SPXXXXXXXXXXXSVIK 8.66 14.755 NaN HPRR15 ELK4 IEGDCXXXXXXXVSSSSK 15.745 7.9122 9.5966

import matplotlib.pyplot as plt
import pandas as pd
import itertools #data df = pd.DataFrame({
   'id': [1, 2, 3, 3],
   'labels': ['HPRR1234', 'HPRR4321', 'HPRR2345', 'HPRR2345'],
   'g': ['KRAS', 'KRAS', 'ELK4', 'ELK4'],
   'r1': [15, 9, 15, 1],
   'r2': [14, 8, 7, 0],
   'r3': [14, 16, 9, 12]
}) #extra setup plt.rcParams['xtick.major.pad'] = 8 #plotting style(s) marker = itertools.cycle((',', '+', '.', 'o', '*')) color = itertools.cycle(('b', 'g', 'r', 'c', 'm', 'y', 'k')) #plot fig = plt.figure() ax = fig.add_subplot(111) ax.plot(df['id'], df['r1'], ls = '', ms = 10, mew = 2, marker = marker.next(), color = color.next()) ax.plot(df['id'], df['r2'], ls = '', ms = 10, mew = 2, marker = marker.next(), color = color.next()) ax.plot(df['id'], df['r3'], ls = '', ms = 10, mew = 2, marker = marker.next(), color = color.next()) # set the tick labels ax.xaxis.set_ticks(df['id']) ax.xaxis.set_ticklabels(df['labels']) plt.setp(ax.get_xticklabels(), rotation = 'vertical', fontsize = 12) plt.tight_layout() fig.savefig("example.pdf")
rawdat.plot(x = 'ts', y = ['bid', 'ask'], marker = '.', lw = 0) plot.hold() rawdat.plot(x = 'lastTrade', y = 'last', marker = 'x', lw = 0) plt.show()
ax = rawdat.plot(x = 'ts', y = ['bid', 'ask'], marker = '.', lw = 0) plot.hold() rawdat.plot(x = 'lastTrade', y = 'last', marker = 'x', lw = 0, ax = ax) plt.show()

Suggestion : 3

We intend to build more plotting integration with matplotlib as time goes on.,You can plot one column versus another using the x and y keywords in DataFrame.plot:,You can create a stratified boxplot using the by keyword argument to create groupings. For instance,,If you have more than one plot that needs to be suppressed, the use method in pandas.plot_params can be used in a with statement:

In[1223]: import matplotlib.pyplot as plt
In [1224]: ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000))

In [1225]: ts = ts.cumsum()

In [1226]: ts.plot()
Out[1226]: <matplotlib.axes.AxesSubplot at 0x10960cc50>
In [1227]: plt.figure(); ts.plot(style='k--', label='Series'); plt.legend()
Out[1227]: <matplotlib.legend.Legend at 0x11f235490>
In [1228]: df = DataFrame(randn(1000, 4), index=ts.index,
......: columns=['A', 'B', 'C', 'D'])
......:

In [1229]: df = df.cumsum()

In [1230]: plt.figure(); df.plot(); plt.legend(loc='best')
Out[1230]: <matplotlib.legend.Legend at 0x11f2c2050>
In [1231]: df.plot(legend=False)
Out[1231]: <matplotlib.axes.AxesSubplot at 0x11f2bc650>
In [1232]: df.plot(subplots=True, figsize=(8, 8)); plt.legend(loc='best')
Out[1232]: <matplotlib.legend.Legend at 0x11f3ca650>