Dataframe Ploting -Plotly Line chart Single X values vs Multiple Y Values - python

I have a data frame as shown below
I need to plot it's line chart using plotly with X axis a "Supply[V]" and Y axis a all the columns
shown in the blue box.
Below is my code ,but no output is coming.
Vcm_Settle_vs_supply_funct = px.line(df_vcm_set_funct_mode1, x = 'Supply[V]', y = df_vcm_set_funct_mode1.columns[5:-9])
Vcm_Settle_vs_supply_funct.show()
But no output is coming may I know where I went wrong

Is the column designation correct? I created a code with the data you presented. It just looks like two lines because the data is almost two different types. I have changed the graph size and added a scale range detail for the y-axis.
import plotly.express as px
fig = px.line(df,x='Supply[V]', y=['VCM_10ms','VCM_20ms','VCM_5s','VCM_DEL1A_10ms','VCNI_DELIA_20ms'])
fig.update_yaxes(tickvals=np.arange(-0.1, 1.5, 0.05))
fig.update_layout(height=600)
fig.show()

Related

How can I plot the number of occurrences of dates from multiple lists in Python?

I'm very new to data science, and I've been trying to do this for about 2 weeks and I haven't got any closer to figuring it out.
I have 4 lists of lists with dates (each date represents a sale):
a = [[2012-6], [2013-5], [2014-5]]
b = [[2015-5], [2017-4], [2019-5]]
etc.
I'm trying to plot the number of occurrences of each date across the x axis on a line plot, with each list represented by a different colour line.
I've tried converting them to np arrays, DataFrames, date time objects etc but I have to admit that I'm finally stuck and I'm not getting any closer.
This was the closest I got:
fig_3 = pd.Series(sale1).value_counts().plot.line()
fig_4 = pd.Series(sal2).value_counts().plot.line()
fig_5 = pd.Series(sale3).value_counts().plot.line()
fig_6 = pd.Series(sale4).value_counts().plot.line()
But when I do this it plots them on different plots, and when I can get them on the same plot I can't figure out how to plot the X axis (I tried using xticks, x labels etc).
Other times the dates end up plotting on the Y axis, and I don't know how to switch that either.
If anyone can help I would greatly appreciate it!
Thanks.
You can add a few changes in your code like this:
import matplotlib.pyplot as plt # <- this row
fig, ax = plt.subplots() # <- this row
pd.Series(sale1).value_counts().plot.line(ax=ax) # <- and this parameter to each your line
pd.Series(sale2).value_counts().plot.line(ax=ax)

How to plot the legend of a set of data with different color label in Mathplotlib

I have a 1:1 plot in which the dot colour are different based on the condition (A-F), which comes from the same data frame column.
df is a data frame with data for every 1 min. df60 is a data frame with data for every 1 hour.
plt.figure()
colors = {'A':'green', 'B':'aqua', 'C':'blue','D':'black','E':'yellow','F':'red'}
x = df['Method1'].loc['2020-01-01 00:00':'2020-01-15 23:59'].resample('h').mean()
y = df['Method2'].loc['2020-01-01 00:00':'2020-01-15 23:59'].resample('h').mean()
plt.scatter(x, y, c=df60['Method1'].loc['2020-01-01 00:00':'2020-01-15 23:59'].map(colors))
plt.show()
I have tried to plot the legend showing that which is A-F. However, since the data comes from the same column, it does not show what I am expecting. Are there any methods which help me to show the legend properly without breaking the column into several columns?
You can define the legend manually by, for instance:
handles=[Line2D([0],[0],label=k,marker="o",markerfacecolor=v,markeredgecolor=v,linestyle="None") for k,v in colors.items()]
plt.legend(handles=handles)
This should produce:
I hope this helps. Not really sure if there is a more elegant solution, though...

My Seaborn heatmap is showing multiple scales

My seaborn heatmap is showing multiple scales (for each column I presume)
Attached an image showing the code, data & chart.
Wondering how I can remove the multiple scales on the right and show only 1.
clustered_heatmap = clustered_points.groupby("Predicted Clusters").sum()
clustered_heatmap = clustered_heatmap.drop(clustered_heatmap.columns[0], axis = 1)
clustered_heatmap
You can try this:
# Create heatmap
plt.figure(figsize=(16,9))
sns.heatmap(clustered_heatmap)

How to make a heatmap in python with aggregated/summarized data?

I'm trying to plot some X and Z coordinates on an image to show which parts of the image have higher counts. Y values are height in this case so I am excluding since I want 2D. Since I have many millions of data points, I have grouped by the combinations of X and Z coordinates and counted how many times that value occurred. The data should contain almost all conbinations of X and Z coordinates. It looks something like this (fake data):
I have experimented with matplotlib.pyplot by using the plt.hist2d(x,y) function but it seems like this takes raw data and not already-summarized data like I've got.
Does anyone know if this is possible?
Note: I can figure out the plotting on an image part later, first I'm trying to get the scatter-plot/heatmap to show aggregated data.
I managed to figure this out. After loading in the data in the format of the original post, step one is pivoting the data so you have x values as columns and z values as rows. Then you plot it using seaborn heatmap. See below:
#pivot columns
values = pd.pivot_table(raw, values='COUNT_TICKS', index=['Z_LOC'], columns = ['X_LOC'], aggfunc=np.sum)
plt.figure(figsize=(20, 20))
sns.set(rc={'axes.facecolor':'cornflowerblue', 'figure.facecolor':'cornflowerblue'})
#ax = sns.heatmap(values, vmin=100, vmax=5000, cmap="Oranges", robust = True, xticklabels = x_labels, yticklabels = y_labels, alpha = 1)
ax = sns.heatmap(values,
#vmin=1,
vmax=1000,
cmap="Greens", #BrBG is also good
robust = True,
alpha = 1)
plt.show()

How to replicate an excel line plot in python

I want to replicate a line plot from excel using x-y data table, but the output from my code looks different than the plot I get from the excel.
I noticed that if I change the plotting style from line to scatter in the excel then both plots looks the same.
This is a sample code for reproduce:
import matplotlib.pyplot as plt
X=[1,5,7,15,20,25,30]
Y=[12,9,10,8,7,9,6]
plt.plot(X,Y)
How to replicate a line plot from excel in to python?
A line chart in Excel has a categorical horizontal (X) axis by default (or time if the data is dates). In a category axis, the data points are spread evenly across the X axis and not according to their value.
In an XY Scatter chart, however, both axes are numerical and the data points are plotted according to their value. A scatter chart series can be formatted to show dots, lines and any combination thereof, so it can look like a line chart.
If the python plot looks the same as the Excel scatter chart, that means that it uses a numeric X axis.
To make the python plot look like an Excel line chart with a categorical axis, change the X values in your python data to consecutive numbers 1 to 7. That will space out the data points evenly on the X axis.

Categories

Resources