I have data like this :
import pandas as pd
import matplotlib.pyplot as plt
index={'A','B','C','D','E'}
d={'typ':[1,2,2,2,1],'value':[10,25,15,17,13]}
df=pd.DataFrame(d,index=index)
I want to plot the dataframe in horizontal bars with different colors reffering to the column 'typ'
You can use the color parameter of matplotlib's barh function:
import pandas as pd
import matplotlib.pyplot as plt
index={'A','B','C','D','E'}
d={'typ':[1,2,2,2,1],'value':[10,25,15,17,13]}
df=pd.DataFrame(d,index=index)
# define the colors for each type
colors = {1:'blue', 2:'red'}
# plot the bars
plt.bar(range(len(df)), df['value'], align='center',
color=[colors[t] for t in df['typ']])
Related
I am on python 2.7, with spyder IDE and this is my data:
Duration ptno
7432.0 X35133502100
7432.0 X35133502100
35255.0 T7956000304
35255.0 T7956000304
17502.0 T7956000304
17502.0 T7956000304
46.0 T7956000304
46.0 T7956000304
The code:
import time
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.read_csv('Nissin_11.09.2018.csv')
bx = df1.plot.bar(x='ptno', y='d', rot=0)
plt.setp(bx.get_xticklabels(),rotation=30,horizontalalignment='right')
plt.show()
I get a nice bar plot as I wanted for each value mentioned in columns Duration & ptno. For reference I am attaching image file of the plot.
But when I try to get a scatter plot with:
df1.plot.scatter(x='ptno', y='d')
It throws a error as :
ValueError: scatter requires x column to be numeric
How can I have a 'scatter' plot for my data ??
As suggested by #Hristo Iliev I used his code:
import seaborn as sns
_ = sns.stripplot(x='ptno', y='d', data=df1)
But It only plot two unique values on axis where I would like to have all values on x axis as my bar plot has x axis values.
One option is to use pure matplotlib. You need to create an array of numbers to use as the x axis, i.e. [1,2,3,4,5,...] and then change the tick labels to the value of the column ptno.
For example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df1 = pd.DataFrame({"Duration":[7432,7432,35255,35255,17502,17502,46,46],
"ptno":["X35", "X35", "T79", "T79", "T79", "T79", "T79", "T79"]})
dummy_x = np.arange(len(df1.ptno))
plt.scatter(dummy_x, df1.Duration)
plt.xticks(dummy_x, df1.ptno)
plt.show()
You cannot make scatter plots with non-numeric values as indicated by the error. In a scatter plot, the position of each point is determined by the location on the real axis of the value of each variable. Categorical or string values such as T7956000304 have no direct mapping to a position on the real axis.
What you can plot though is a series of strip plots, one for each unique value of ptno. That's easiest to do with Seaborn:
import seaborn as sns
_ = sns.stripplot(x='ptno', y='d', data=df1)
I´m trying to create some graphs from a dataframe I imported. The problem is I can create an only image with both graphs. I have this output:
And I´m looking for this Output:
Here is the code:
from pandas_datareader import data
import pandas as pd
import datetime
import matplotlib.pyplot as plt
df = pd.read_csv('csv.csv', index_col = 'Totales', parse_dates=True)
df.head()
df['Subastas'].plot()
plt.title('Subastadas')
plt.xlabel('Fechas')
plt.ylabel('Cant de Subastadas')
plt.subplot()
df['Impresiones_exchange'].plot()
plt.title('Impresiones_exchange')
plt.xlabel('Fechas')
plt.ylabel('Cant de Impresiones_exchange')
plt.subplot()
plt.show()
CSV data:
Totales,Subastas,Impresiones_exchange,Importe_a_pagar_a_medio,Fill_rate,ECPM_medio
Total_07/01/2017,1596260396,30453841,19742.04,3.024863813,0.733696498
Total_07/12/2017,1336604546,57558106,43474.29,9.368463445,0.656716233
Total_07/01/2018,1285872189,33518075,20614.4,4.872889166,0.678244085
Also, I would like to save the output in an xlsx file too!
Use plt.subplots() to define two separate Axes objects, then use the ax argument of df.plot() to associate a plot to an axis:
import pandas as pd
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(2,1,figsize=(5,10))
df['Impresiones_exchange'].plot(ax=ax2)
ax1.set_title('Impresiones_exchange')
ax1.set_xlabel('Fechas')
ax1.set_ylabel('Cant de Impresiones_exchange')
df['Subastas'].plot(ax=ax1)
ax2.set_title('Subastadas')
ax2.set_xlabel('Fechas')
ax2.set_ylabel('Cant de Subastadas')
for info, shape in zip(map.counties_info, map.counties):
if info['FIPS'] in geoids:
x = np.random.rand(1)[0]
c = cmap(x)[:3]
newc = rgb2hex(c)
patches.append(Polygon(np.array(shape), color=newc, closed=True))
ax.add_collection(PatchCollection(patches))
plt.title('Counties with HQ of NYSE-Listed Firms: 1970')
plt.show()
produces this image:
My question is the code specifically asks for random colors in the polygons. If I print the values of newc and display them at a website that converts hex codes to colors, there is a wide range of different colors. But the output has only one. How can I fix this?
In order for a PatchCollection to have different colors for the individual patches, you have two options.
Using the colors of the original patches.
Using a colormap to determine the colors according to some array of values.
Using the colors of the original patches.
This approach is closest to the code from the question. It would require to set the argument match_original=True to the patch collection.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.patches
import matplotlib.collections
ar = np.array([[0,0],[1,0],[1,1],[0,1],[0,0]])
cmap=plt.cm.jet
patches=[]
fig, ax=plt.subplots()
for i in range(5):
x = np.random.rand(1)[0]
c = cmap(x)[:3]
poly = plt.Polygon(ar+i, color=c, closed=True)
patches.append(poly)
collection = matplotlib.collections.PatchCollection(patches,match_original=True)
ax.add_collection(collection)
ax.autoscale()
plt.show()
Using a colormap to determine the colors according to some array of values.
This is probably easier to implement. Instead of giving each individual polygon a color, you would set an array of values to the PatchCollection and specify a colormap according to which the polygons are colorized.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
import matplotlib.patches
import matplotlib.collections
ar = np.array([[0,0],[1,0],[1,1],[0,1],[0,0]])
values = np.random.rand(5)
cmap=plt.cm.jet
patches=[]
fig, ax=plt.subplots()
for i in range(len(values)):
poly = plt.Polygon(ar+i, closed=True)
patches.append(poly)
collection = matplotlib.collections.PatchCollection(patches, cmap=cmap)
collection.set_array(values)
ax.add_collection(collection)
ax.autoscale()
plt.show()
Passing a 2D array to Matplotlib's histogram function with histtype='step' seems to plot the columns in reverse order (at least from my biased, Western perspective of left-to-right).
Here's an illustration:
import matplotlib.pyplot as plt
import numpy as np
X = np.array([
np.random.normal(size=5000),
np.random.uniform(size=5000)*2.0 - 1.0,
np.random.beta(2.0,1.0,size=5000)*3.0,
]).T
trash = plt.hist(X,bins=50,histtype='step')
plt.legend(['Normal','2*Uniform-1','3*Beta(2,1)'],loc='upper left')
Produces this:
Running matplotlib version 2.0.2, python 2.7
From the documentation for legend:
in order to keep the "label" and the legend element instance together,
it is preferable to specify the label either at artist creation, or by
calling the set_label method on the
artist
I recommend to use the label keyword argument to hist:
String, or sequence of strings to match multiple datasets
The result is:
import matplotlib.pyplot as plt
import numpy as np
X = np.array([
np.random.normal(size=5000),
np.random.uniform(size=5000)*2.0 - 1.0,
np.random.beta(2.0,1.0,size=5000)*3.0,
]).T
trash = plt.hist(X,bins=50,histtype='step',
label=['Normal','2*Uniform-1','3*Beta(2,1)'])
plt.legend(loc='upper left')
plt.show()
This question is related to plotting minor tick mars on the y-axis in a Python plot with
matplotlib.
Here is the code that I have:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig, ax = plt.subplots()
fig.set_facecolor('white')
x = [1,2,3]
plt.subplot(211)
plt.plot([1,2,3], label="test1")
plt.plot([3,2,1], label="test2")
plt.xticks()
plt.yticks()
ax.yaxis.set_minor_locator(MultipleLocator(5))
plt.show()
When I generate this plot, I am not getting any minor tick marks.
I have attached here the plot that this code gives me.
Is it possible for me to display the minor tick marks for the y-axis here?
You can set what ticks you want in plt.yticks() , the input can be a numpy array which you generate beforehand
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
fig, ax = plt.subplots()
fig.set_facecolor('white')
yticks = np.arange(1,3,0.2)
x = [1,2,3]
plt.subplot(211)
plt.plot([1,2,3], label="test1")
plt.plot([3,2,1], label="test2")
plt.xticks()
plt.yticks(yticks)
ax.yaxis.set_minor_locator(MultipleLocator(5))
plt.show()
which gives you :